Find pixel coordinates value of a centroid ?

[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

please clarify your question.
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

Sign in to comment.

 Accepted Answer

centroids = round(centroids);
cent_vals = Inotbw(sub2ind(size(Inotbw),centroids(:,2),centroids(:,1)))

13 Comments

In this context I think Sean means
cent_vals = I3(sub2ind(size(I3),centroids(:,2),centroids(:,2)))
I was assuming I3 was a label||binary image. Thus he'd have to use the original non-modified image to get the pixel value. It's hard to tell though...
Good point. Though if he wants the RGB values then the mechanism would need to be modified some in order to get all three planes.
i have try the code
but i still don't know how to use it..
i don't get any pixel value ?
Yes, something like this.
idxr = sub2ind(size(I3),centroids(:,2),centroids(:,1));
pixPerPlane = size(I3,1)*size(I3,2);
idxg = idxr+pixPerPlane;
idxb = idxg+pixPerPlane;
rgbCents = reshape(I3([idxr;idxg;idxb]),[],3);
What doesn't work for you Danny? Does it error out? Does it return incorrect numbers? Is there an m-lint error because of a parenthesis?
not return incorrect numbers..
but no numbers are shown
i'm using 2 version of matlab
to crosscheck the codes
but still no numbers are shown
Edit your question to contain EVERYTHING in your script. We can't diagnose it without seeing it...
146
181
96
170
162
144
the value above are the number that shown
but why the pixel value didn't match ?
here is the pixel value that i check with impixelinfo
(135,85)
(133,311)
(341,214)
(341,298)
(593,68)
(528,289)
Those look like pixel coordinates, not pixel values. What value is shown after the ")" when you position to those positions ?
i have revise the question
i mean the pixel coordinates
sorry for the missunderstanding..
Then that should just be
round(centroids)
to within the nearest pixel.
thx for the help

Sign in to comment.

More Answers (2)

Is there a reason you are using the Pixel List instead of doing as Sean suggested to you before:
round the coordinates returned from regionprops and use those as indices to get the centroid.

1 Comment

because i can't get the coordinates ..
so i think i can get the coordinates use the pixelinfo

Sign in to comment.

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

sean
11 79 102
is that come from the original image ?
if i point my mouse to the centroid of my car ( after segmentation )
in impixelinfo show (344,214) 1
which the 1 is represent the pixel
and 344,214 represent X and Y
can i get those x n y value
well it's a BINARY IMAGE!!!!!!!! That means only 1s and 0s and since your car is white it's a 1.
11 79 102 are the rgb intensity components of the original image.
imtool(I)
%hold your mouse near the centroid...
%or
imshow(I)
hold on
plot(centroids(:,1), centroids(:,2), 'b*')
hold off
Have a great weekend!
(hopefully away from the computer screen)
i'm sorry for the wrong question
what i search is the
pixel coordinates from my centroid
sorry for the wrong question
Danny, what is
round(centroids)
showing for you?
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.
hey,
@Raka Mukherjee did you get any solution regarding this?....i intent to perform the same task...please help me with this.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!