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 commentaires

Doug Hull
Doug Hull le 13 Mai 2011
please clarify your question.
Image Analyst
Image Analyst le 29 Déc 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

Connectez-vous pour commenter.

 Réponse acceptée

Sean de Wolski
Sean de Wolski le 13 Mai 2011
centroids = round(centroids);
cent_vals = Inotbw(sub2ind(size(Inotbw),centroids(:,2),centroids(:,1)))

13 commentaires

Walter Roberson
Walter Roberson le 13 Mai 2011
In this context I think Sean means
cent_vals = I3(sub2ind(size(I3),centroids(:,2),centroids(:,2)))
Sean de Wolski
Sean de Wolski le 13 Mai 2011
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...
Walter Roberson
Walter Roberson le 13 Mai 2011
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.
danny agus
danny agus le 13 Mai 2011
i have try the code
but i still don't know how to use it..
i don't get any pixel value ?
Sean de Wolski
Sean de Wolski le 13 Mai 2011
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);
Sean de Wolski
Sean de Wolski le 13 Mai 2011
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?
danny agus
danny agus le 13 Mai 2011
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
Sean de Wolski
Sean de Wolski le 13 Mai 2011
Edit your question to contain EVERYTHING in your script. We can't diagnose it without seeing it...
danny agus
danny agus le 13 Mai 2011
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)
Walter Roberson
Walter Roberson le 13 Mai 2011
Those look like pixel coordinates, not pixel values. What value is shown after the ")" when you position to those positions ?
danny agus
danny agus le 13 Mai 2011
i have revise the question
i mean the pixel coordinates
sorry for the missunderstanding..
Walter Roberson
Walter Roberson le 13 Mai 2011
Then that should just be
round(centroids)
to within the nearest pixel.
danny agus
danny agus le 13 Mai 2011
thx for the help

Connectez-vous pour commenter.

Plus de réponses (2)

Walter Roberson
Walter Roberson le 13 Mai 2011
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 commentaire

danny agus
danny agus le 13 Mai 2011
because i can't get the coordinates ..
so i think i can get the coordinates use the pixelinfo

Connectez-vous pour commenter.

Sean de Wolski
Sean de Wolski le 13 Mai 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 commentaires

danny agus
danny agus le 13 Mai 2011
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
Sean de Wolski
Sean de Wolski le 13 Mai 2011
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)
danny agus
danny agus le 13 Mai 2011
i'm sorry for the wrong question
what i search is the
pixel coordinates from my centroid
sorry for the wrong question
Walter Roberson
Walter Roberson le 13 Mai 2011
Danny, what is
round(centroids)
showing for you?
Raka Mukherjee
Raka Mukherjee le 3 Fév 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
Areeba Naseer le 26 Avr 2021
hey,
@Raka Mukherjee did you get any solution regarding this?....i intent to perform the same task...please help me with this.

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by