how to find centroid?

4 views (last 30 days)
Zeeshan
Zeeshan on 21 Sep 2014
Commented: Image Analyst on 22 Sep 2014
hi, i've an image having three objects. i was able to detect one of the object. now i need to find the centroid of the detected object. i used s=regionprops(bw,'centroid'); centroid=cat(1,s.centroid);
it give me an error : Reference to non-existent field 'majoraxislength'. please help. mail me at aliz.inam@gmail.com

Accepted Answer

Guillaume
Guillaume on 22 Sep 2014
Welcome to matlab absurd handling of case sensitivity.
While the arguments you pass to regionprops are not case sensitive, accessing the fields of a structure is. The fields of the structure returned by your regionprops are 'Area', 'Centroid' and 'BoundingBox', thus you need to access the fields with:
s(x).Centroid
s(x).Area
s(x).BoundingBox
I'm not sure what you're doing with your plot commands though, plot(s(x).Centroid, s(x).Area, s(x).BoundingBox); wouldn't have worked anyway, so you may have to ask another question for that.

More Answers (1)

Image Analyst
Image Analyst on 21 Sep 2014
You forgot to include the error message! You just snipped out a small part of it and told us that, but the crucial part - the line of code that caused the error - you forgot to include. Please include all the red text . Don't snip out any of it. Somewhere you are trying to reference s(index).majoraxislength. MATLAB is case sensitive so there could be a field called MajorAxisLength on s if you asked for it. But you did not, you just asked for 'Centroid'. Perhaps you want:
s = regionprops(bw, 'centroid', 'MajorAxisLength');
And if you post it here, we'll solve it here. No one is going to email you, and just leave this thread hanging unfinished.
  2 Comments
Zeeshan
Zeeshan on 22 Sep 2014
Edited: Zeeshan on 22 Sep 2014
this is my complete code
>> a=imread('C:\Users\basharat ahmad\Desktop\thesis code\1.jpg');
>> r=a(:,:,1);
>> g=a(:,:,2);
>> b=a(:,:,3);
>> gr=g-r/2-b/2;
>> imshow(a);
>> bw=gr>40;
>> imshow(bw);
>> s=regionprops(bw,'centroid','area','boundingbox');
>> imshow(a);hold on;
>> for x=1:numel(s)
plot (s(x).centroid, s(x).area, s(x).boundingbox);
end
Reference to non-existent field 'centroid'.
>> plot (s(x).area, s(x).boundingbox);
Reference to non-existent field 'area'.
>> plot (s(x).boundingbox);
Reference to non-existent field 'boundingbox' .
the error is in bold.
please tell me what i'm doing wrong
Image Analyst
Image Analyst on 22 Sep 2014
As I said above, MATLAB is case sensitive - note how I changed the case of MajorAxisLength vs. what you had. Anyway, I don't see you asking for that in your code above. Did you see my suggestions in my answer above? You must have asked tried to reference majoraxislength (with wrong capitalization) but failed to ask for it in regionprops like I suggested. Also, when trying to reference the Centroid, Area, and BoundingBox fields, you didn't pay any attention to the case of the letters like I suggested. Please read my answer again, as well as Guillaume's, and you should be able to fix the problem. And like he said, we have no idea what you're trying to plot or display - maybe you want fprintf('%f ', s(x).centroid, s(x).area, s(x).boundingbox); instead of plot.

Sign in to comment.

Categories

Find more on Geographic Plots 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!