필터 지우기
필터 지우기

how to find centroid?

조회 수: 3 (최근 30일)
Zeeshan
Zeeshan 2014년 9월 21일
댓글: Image Analyst 2014년 9월 22일
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

채택된 답변

Guillaume
Guillaume 2014년 9월 22일
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.

추가 답변 (1개)

Image Analyst
Image Analyst 2014년 9월 21일
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
Zeeshan
Zeeshan 2014년 9월 22일
편집: Zeeshan 2014년 9월 22일
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 2014년 9월 22일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by