フィルターのクリア

How do you find a centroid of an image?

45 ビュー (過去 30 日間)
Rish
Rish 2014 年 8 月 5 日
コメント済み: Image Analyst 2017 年 10 月 25 日
I am trying to find the centroid of a binary image using region props but I keep receiving the error "Field reference for multiple structure elements that is followed by more reference blocks is an error". Here is my code for finding the centroid:
measurements = regionprops(bw, 'Centroid');
hold on;
plot(measurements.Centroid(1), measurements.Centroid(2), 'r+', 'MarkerSize', 120, 'LineWidth', 1);
hold off;
imshow(bw)
Any help would be appreciated thank you
  1 件のコメント
Image Analyst
Image Analyst 2014 年 8 月 6 日
Do you want the centroid of the whole, entire image, or of individual blobs in the image? You asked about the whole image but your code seems to indicate you maybe want centroids of individual blobs. Of course the centroid of the whole image is at the center, and the "Weighted centroid" depends on the gray levels in the image.

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2014 年 8 月 5 日
Maybe try
centroids = [measurements.Centroid];
xCentroids = centroids(1:2:end);
yCentroids = centroids(2:2:end);
plot(xCentroids, yCentroids, .............
  2 件のコメント
Rish
Rish 2014 年 8 月 5 日
Thank you for your help. When I tried this a line graph showed up without the image. I am not sure why though.
Image Analyst
Image Analyst 2014 年 8 月 6 日
編集済み: Image Analyst 2014 年 8 月 6 日
If you want the markers to be over the image, put "hold on" before plot(). You must have removed it for some reason.
hold on; % Prevent plot() from blowing away image.
plot(..........., 'r+'........
It looks like you used 'b-' in your call to plot because I see blue lines instead of red plus signs.

サインインしてコメントする。

その他の回答 (6 件)

Nade Sritanyaratana
Nade Sritanyaratana 2014 年 8 月 5 日
The example at the end of the regionprops documentation has an elegant use of the cat function, which might be able to fix the error you are getting.
measurements = regionprops(bw, 'Centroid');
imshow(bw);
centroids = cat(1,measurements.Centroid);
hold on;
plot(centroids(:,1), centroids(:,2), 'r+', 'MarkerSize', 120, 'LineWidth', 1);
hold off;
  4 件のコメント
Rujal
Rujal 2016 年 2 月 7 日
編集済み: Rujal 2016 年 2 月 7 日
I have used this code
but I want to find mid point between two centroids. for that I have used
mid= mean(centroids); plot(mid,'r*');
and the result is:
but I want to find midpoint between two centroids of objects. I would be thankful if someone helps me.
Image Analyst
Image Analyst 2016 年 2 月 7 日
You can't do that because centroids is [x1,y1,x2,y2,x3,y3,x4,y4,.....] You need to extract x and y separately:
xCenters = centroids(1:2:end);
yCenters = centroids(2:2:end);
Then you can pick out which two blobs you want to find the midpoint between and average them. For example
xMid = (xCenters(1)+xCenters(2))/2;
yMid = (yCenters(1)+yCenters(2))/2;
hold on;
plot(xMid, yMid, 'r+', 'LineWidth', 2, 'MarkerSize', 20);

サインインしてコメントする。


Rish
Rish 2014 年 8 月 6 日
Thank you very much guys. I really appreciate your help and I figured it out.

hallouma amari
hallouma amari 2015 年 5 月 12 日
編集済み: Image Analyst 2015 年 5 月 12 日
help me too please
this is my error
Field reference for multiple structure elements that is followed by more reference blocks is an error.
img2=imread('56.png');
se=strel('disk',3);
J=imclose(img2,se);
J=rgb2gray(J);
M=imadjust(J);
B=0; %initialisation
[R C]=size(J);
measurements = regionprops(J, 'Centroid');
x2=measurements.Centroid(1);
y2=measurements.Centroid(2);
bw = bwboundaries(J);
boundary = bw{1};
x1=boundary(:,1);
y1=boundary(:,2);
x1 = x1-x2;
y1 = y1-y2;
  1 件のコメント
Image Analyst
Image Analyst 2015 年 5 月 12 日
Try like what I showed:
centroids = [measurements.Centroid];
xCentroids = centroids(1:2:end);
yCentroids = centroids(2:2:end);
% Compute distances of perimeter to centroid of blob #1
bw = bwboundaries(J);
boundary = bw{1};
x1=boundary(:,1);
y1=boundary(:,2);
deltaXs = x1 - xCentroids(1);
deltaYs = y1 - yCentroids(1);

サインインしてコメントする。


somia gupta
somia gupta 2016 年 5 月 10 日
i want to find the centroid of an image in matlab ??? need help for the code for head pose detection
  1 件のコメント
Image Analyst
Image Analyst 2016 年 5 月 10 日
See my Image Segmentation Tutorial in my File Exchange http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862 It goes over that plus more.

サインインしてコメントする。


vinoba
vinoba 2017 年 3 月 11 日
How to find centroid of each and every blob(objects) in a binary image?
  1 件のコメント
Image Analyst
Image Analyst 2017 年 3 月 11 日
See my Image Segmentation Tutorial in my File Exchange: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862
Basically
[labledImage, numBlobs] = bwlabel(binaryImage);
props = regionprops(labeledImage, 'Centroid', 'WeightedCentroid');
You get a structure array with both types of centroids for every region.

サインインしてコメントする。


tanuwyot
tanuwyot 2017 年 10 月 25 日
How to locate the nucleus centroid and extract the seven HU moments value ? Please help me
  1 件のコメント
Image Analyst
Image Analyst 2017 年 10 月 25 日
I don't have code for Hu's moments.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImages についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by