How to find the X axis length and Yaxis length of the object

I have to find the shape of the object so i have to find the length of the x and y axis length of the shape. So please help me

 Accepted Answer

doc regionprops

4 Comments

there is lot's of peoperties in there, which one i have to use to get X & Y axis length of object
'MajorAxisLength', 'MinorAxisLength'
But i think there is major difference between X&Y axis length and Major&Minor axis length
Then use 'Orientation' property to convert between the direction of the ellipse and x/y.

Sign in to comment.

More Answers (1)

You could use bounding box, or MajorAxisLength and MinorAxisLength, which is the axes of an ellipse fitted to the blob. There is no farthest end-to-end measurement available. And there is no definitive crossways width. If you had an elongated, irregularly-shaped blob and the major axis was where you think it would be, then what is the minor axis? The narrowest one? The cross distance at the midpoint (which may be at the widest point, the narrowest point, or something else)?

11 Comments

How we use BoundingBox to find the length?
If the object is aligned with the edges of the image you can. If it's tilted at 45 degrees, then obviously you can't assume the bounding box width or height is the length of the object.
Looks like you've decided to go with Sean's answer and go with major and minor axis of the fitted ellipse.
The end-to-end length of an object can be approximated from the geodesic distance transform.
I = imread('rice.png');
rice = imclearborder(stdfilt(I)<15);
skel = bwmorph(rice,'skel',inf);
endpoints = bwmorph(skel,'endpoints');
L = bwlabel(rice);
L(~endpoints) = 0;
for ii = 2:max(L(:))
idx = find(L == ii);
endpoints(idx(2:end)) = 0; %keep only one end point per object
end
D = bwdistgeodesic(rice,endpoints,'quasi-euclidean');
imshow(D,[]);
colormap(jet);
Get the maximum of each region and you have the distance.
Sean, did you actually try it? D is all 100% NaN's for me.
It's NaN outside of the mask (or rice I guess), but who cares about that part! The values at the mask are the shortest quasi-Euclidean distance to an end point of the object.
Take a look at that last part:
imshow(D,[]);
colormap(jet);
Sean, it can't do that line. Just try it. Copy and paste your code and you'll see this:
Error using imageDisplayValidateParams
Expected input number 2, [LOW HIGH], to be non-NaN.
Error in checkDisplayRange (line 10)
validateattributes(display_range, {'numeric'},...
Error in imageDisplayValidateParams (line 53)
common_args.DisplayRange = checkDisplayRange(common_args.DisplayRange,mfilename);
Error in imageDisplayParseInputs (line 78)
common_args = imageDisplayValidateParams(common_args);
Error in imshow (line 219)
[common_args,specific_args] = ...
Error in test3 (line 13)
imshow(D,[]);
I have no clue why you're seeing that, IA. I run the exact same code (tried 13a and 13b), and I get the following:
  • .
The error message doesn't even make sense, the second argument passed into imshow is [] or empty, which is not nan as the message is suggesting.
Either way, you could just replace the nan's with zeros.
D(isnan(D)) = 0;
Sorry Sean, I found the problem. I had a file called rice.png that was a binary mask file in a folder (that I had created for a project I was working on) and that was earlier on the path. It used that instead. That's the danger of not specifying the full path. It's not your fault - how could you know that I'd just happen to have a different image called that? - but I'm showing the more robust code below:
% Define folder where rice.png lives.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'rice.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
I = imread(fullFileName);
imshow(I);
rice = imclearborder(stdfilt(I)<15);
skel = bwmorph(rice,'skel',inf);
endpoints = bwmorph(skel,'endpoints');
L = bwlabel(rice);
L(~endpoints) = 0;
for ii = 2:max(L(:))
idx = find(L == ii);
endpoints(idx(2:end)) = 0; %keep only one end point per object
end
D = bwdistgeodesic(rice,endpoints,'quasi-euclidean');
imshow(D,[]);
colormap(jet);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!