how to find centroid?
4 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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.
0 个评论
更多回答(1 个)
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 个评论
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 Center 和 File Exchange 中查找有关 Geographic Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!