I have boundaries of objects in my code. I want to use those boundaries to calculate the features of those objects in my preceding loop. please help fix

9 views (last 30 days)
edges = edge(imgContrast,'Canny');
se = strel('disk', 1);
edgesClean = imclose(edges, se);
edgesClean = imfill(edgesClean, 'holes');
B = bwboundaries(edgesClean);
[X, Y] = meshgrid(1:columns, 1:rows);
areaOfEachZone = zeros(1, numZones);
profileCounts = nan(1, numZones);
totalArea = nan(1, numZones);
avgSize = nan(1, numZones);
zoneArea = nan(1, numZones);
avgCircularity = nan(1, numZones);
avgFeret = nan(1, numZones);
avgMinFeret = nan(1, numZones);
for k = 1:numZones
% Create a binary mask for this zone
zoneMask = (sqrt((X - x).^2 + (Y - y).^2) >= radius(k)) & (sqrt((X - x).^2 + (Y - y).^2) < radius(k+1));
areaOfEachZone(k) = sum(zoneMask, 'all') * pixelSize^2;
% Calculate connected components in this zone
cc = bwconncomp(zoneMask);
% Calculate region properties for each connected component in this zone
statscc = regionprops(cc, 'Area', 'Centroid', 'Eccentricity', 'Perimeter', 'MajorAxisLength', 'MinorAxisLength','Circularity');
% Calculate region properties for each object and store in arrays
% areas = [];
areaOfEachZone = [];
feretDiams = [];
circularities = NaN(size(B,1),1); % initialize circularities array to NaN
for j = 1:size(B,1)
boundary = B{j};
objectMask = poly2mask(boundary(:,2), boundary(:,1), size(AI,1), size(AI,2));
regionProps = regionprops(objectMask, 'Area', 'Perimeter', 'MaxFeretProperties');
% Calculate circularity
if ~isempty(regionProps) && size(regionProps, 1) == 1 && regionProps.Area > 0 % exclude regions with zero area
circularity = 4*pi*regionProps.Area/regionProps.Perimeter^2;
if circularity >= 0 && circularity <= 1 % circularity should be between 0 and 1
circularities(j) = circularity;
else
circularities(j) = NaN;
end
areaOfEachZone(j) = regionProps.Area;
feretDiams(j) = regionProps.MaxFeretDiameter;
else
areaOfEachZone(j) = 0;
circularities(j) = NaN;
feretDiams(j) = 0;
end
end
% Calculate features for this zone
numObjects = cc.NumObjects;
if numObjects > 0
areas = [statscc.Area];
profileCounts(k) = numObjects;
totalArea(k) = sum(areas) * pixelSize^2;
% zoneArea(k) = areaOfEachZone(k);
% Calculate average size of mitochondria for this zone
avgSize(k) = mean(areas) * pixelSize^2;
% circularities = [statscc.Circularity];
avgCircularity(k) = mean(circularities(isfinite(circularities)));
% ferets = [statscc.MajorAxisLength];
feretDiams(j) = regionProps.MaxFeretDiameter;
avgFeret(k) = mean(feretsDiams) * pixelSize;
minFerets = [statscc.MinorAxisLength];
avgMinFeret(k) = mean(minFerets) * pixelSize;
end
end
So I am novice to matlab, some others in the group before me were working on this but left adn now i have to fix some errors i am getting. I run into this wall because the larger loop is supposed to be for a particular area in the image but then i have another loop that indixes for the boundaries found within that area but it never results successfully such that the features within that area with those boundaries are indexed properly. What do you suggest to correct this?
  3 Comments
Chanille
Chanille on 6 Apr 2023
BI = imread(MyRGBImage,1); %%AI channel or (MyRGBImage); for other images
bg = imopen(BI, strel('disk', 25));
imgNoBg = BI - bg;
imgContrast = imadjust(imgNoBg);
I haven't finished the rest of the code because I am trying to fix the above issue but it would be something like this:
%% % Display average region properties for current image
fprintf('Avg Area: %f, Avg Circularity: %f, Avg Feret Diameter: %f, Num Objects: %d\n', ...
avgArea, avgCircularity, avgFeretDiam, numObjects);
Chanille
Chanille on 6 Apr 2023
@Image Analyst I still have a question even after reading your really cool demo. How can i ensure that the boundaries detected are what the regionprops is calculating within my for k loop? Thank you! I look forward to contributing to this forum some day. :)

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 6 Apr 2023
You don't need that inside loop over size(B, 1). You can simply get all the values like
% Calculate region properties for each connected component in this zone
statscc = regionprops(cc, 'Area', 'Centroid', 'Eccentricity', 'Perimeter', 'MajorAxisLength', 'MinorAxisLength','Circularity');
meanArea = mean([statscc.Area])
xyCentroids = vertcat(statscc.Centroid); % Not sure what you want to do with these.
meanEccentricity = mean([statscc.Eccentricity])
meanMajorAxisLength = mean([statscc.MajorAxisLength])
meanMinorAxisLength = mean([statscc.MinorAxisLength])
meanCircularity = mean([statscc.Circularity])
% and so on.
  52 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Recognition, Object Detection, and Semantic Segmentation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!