Find pixel coordinates value of a centroid ?
14 views (last 30 days)
Show older comments
danny agus
on 13 May 2011
Commented: Areeba Naseer
on 26 Apr 2021
[EDIT: Fri May 13 20:58:34 UTC 2011 - Reformat - MKF]
I want to find the x n y coordinates and pixel value of the centroid. Here is the code I have:
I = imread('mobil.png');
%I2 = imtophat(I,strel('disk',15));
level = graythresh(I);
bw = im2bw(I,level);
I2 = 1 - bw;
I2 = bwareaopen(I2, 200);
I3 = imfill(I2, 'holes');
s = regionprops(I3, 'centroid');
centroids = cat(1, s.Centroid);
imshow(I3)
hold on
plot(centroids(:,1), centroids(:,2), 'b*')
hold off
impixelinfo
The car is :
2 Comments
Image Analyst
on 29 Dec 2012
Not sure how I messed this question (a year and a half ago), but I have a tutorial in my File Exchange where I find centroids: BlobsDemo
Accepted Answer
Sean de Wolski
on 13 May 2011
centroids = round(centroids);
cent_vals = Inotbw(sub2ind(size(Inotbw),centroids(:,2),centroids(:,1)))
13 Comments
More Answers (2)
Walter Roberson
on 13 May 2011
round the coordinates returned from regionprops and use those as indices to get the centroid.
Sean de Wolski
on 13 May 2011
It works perfectly for me; I only had to modify the extraction to I instead of I3, as Walter and I had hypothesized about.
I = imread('ans513.png'); %your image
level = graythresh(I);
bw = im2bw(I,level);
I2 = 1 - bw;
I2 = bwareaopen(I2, 200);
I3 = imfill(I2, 'holes');
s = regionprops(I3, 'centroid');
centroids = cat(1, s.Centroid);
imshow(I3)
hold on
plot(centroids(:,1), centroids(:,2), 'b*')
hold off
centroids = round(centroids);
idxr = sub2ind(size(I3),centroids(:,2),centroids(:,1));
pixPerPlane = size(I3,1)*size(I3,2);
idxg = idxr+pixPerPlane;
idxb = idxg+pixPerPlane;
rgbCents = reshape(I([idxr;idxg;idxb]),[],3);
%{
rgbCents =
147 146 144
145 145 143
66 62 63
11 79 102 %%%This is the value of the pixel at the center of your car
86 77 72
83 70 64
%}
6 Comments
Raka Mukherjee
on 3 Feb 2020
Hi,
The x and y values that the centroid property of regionprops is returning, is it the centroid of the connected components? For eg., I have a binary image and I have detected the connected pixels and found out the centroid of those connected pixels with regionprops. now i want to convert those centroid values into geographic coordinates (latitudes and longitudes). If I use the following will it give what I need?
u=r2.Centroid;
x= u(:,1); % the x centroid value
y= u(:,2); % the y centroid value
[lat1, lon1]=pix2latlong(Ref,x,y);
[Lat, Lon] = projinv(info, lon1, lat1);
Please guide me in this as I am not being able to figure out what is the mistake since the coordinates are not matching with the original image.
Areeba Naseer
on 26 Apr 2021
hey,
@Raka Mukherjee did you get any solution regarding this?....i intent to perform the same task...please help me with this.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!