How can i find the Majoraxislength and Minoraxislength of each cell present in binary image ?
11 views (last 30 days)
Show older comments
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1211958/image.png)
0 Comments
Accepted Answer
Jonas
on 29 Nov 2022
Edited: Jonas
on 29 Nov 2022
a call to regionprops should suffice:
clear;
im=imread("image.png");
im=rgb2gray(im); % to grayscale
im=im~=0; % make binary image
imshow(im)
stats = regionprops('table',im,'Centroid','MajorAxisLength','MinorAxisLength');
% check if the results make sence
centers = stats.Centroid;
diametersMax = max([stats.MajorAxisLength stats.MinorAxisLength],[],2);
diametersMin = min([stats.MajorAxisLength stats.MinorAxisLength],[],2);
radiiMax = diametersMax/2;
radiiMin = diametersMin/2;
hold on
viscircles(centers,radiiMax,'Color','red');
viscircles(centers,radiiMin,'Color','blue');
hold off
4 Comments
Jonas
on 29 Nov 2022
maxbe using the min and max feret is a better possibility since the axes sometimes seem not to be correct:
clear;
im=imread("image.png");
im=rgb2gray(im);
im=im~=0;
im=bwconvhull(im,'objects');
imshow(im)
stats = regionprops('table',im,'Centroid','MajorAxisLength','MinorAxisLength','Orientation');
% check if the results are correct
centers = stats.Centroid;
diametersMax = max([stats.MajorAxisLength stats.MinorAxisLength],[],2);
diametersMin = min([stats.MajorAxisLength stats.MinorAxisLength],[],2);
dxMajor=stats.MajorAxisLength.*cosd(stats.Orientation)./2;
dyMajor=stats.MajorAxisLength.*sind(stats.Orientation)./2;
dxMinor=stats.MinorAxisLength.*sind(stats.Orientation)./2;
dyMinor=stats.MinorAxisLength.*cosd(stats.Orientation)./2;
for nr=1:height(stats)
if stats.Orientation(nr)>0
xcordsMaj(:,nr)=(centers(nr,1)+[+1 -1]*dxMajor(nr))';
ycordsMaj(:,nr)=(centers(nr,2)+[-1 +1]*dyMajor(nr))';
xcordsMin(:,nr)=(centers(nr,1)+[+1 -1]*dxMinor(nr))';
ycordsMin(:,nr)=(centers(nr,2)+[+1 -1]*dyMinor(nr))';
else
xcordsMaj(:,nr)=(centers(nr,1)+[+1 -1]*dxMajor(nr))';
ycordsMaj(:,nr)=(centers(nr,2)+[-1 +1]*dyMajor(nr))';
xcordsMin(:,nr)=(centers(nr,1)+[+1 -1]*dxMinor(nr))';
ycordsMin(:,nr)=(centers(nr,2)+[-1 +1]*dyMinor(nr))';
end
end
line([xcordsMaj xcordsMaj ],[ycordsMaj ycordsMin],'Color','red')
the feret looks like that:
clear;
im=imread("image.png");
im=rgb2gray(im);
im=im~=0;
im=bwconvhull(im,'objects'); % not needed here because ferets are calculated over conv hull; neverthless here for better line visibility
imshow(im)
stats = regionprops('table',im,'MaxFeretProperties','MinFeretProperties');
cellfun(@(in)line(in(:,1),in(:,2),'Color','red'),[stats.MinFeretCoordinates; stats.MaxFeretCoordinates])
More Answers (0)
See Also
Categories
Find more on Image Segmentation and Analysis 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!