Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Find pixel coordinates value of a centroid ?

Asked by danny agus on 13 May 2011

[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 :

Image

2 Comments

Doug Hull on 13 May 2011

please clarify your question.

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

danny agus

Products

No products are associated with this question.

3 Answers

Answer by Sean de Wolski on 13 May 2011
Accepted answer
centroids = round(centroids);
cent_vals = Inotbw(sub2ind(size(Inotbw),centroids(:,2),centroids(:,1)))

13 Comments

danny agus on 13 May 2011

i have revise the question
i mean the pixel coordinates
sorry for the missunderstanding..

Walter Roberson on 13 May 2011

Then that should just be

round(centroids)

to within the nearest pixel.

danny agus on 13 May 2011

thx for the help

Sean de Wolski
Answer by Walter Roberson on 13 May 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 Comment

danny agus on 13 May 2011

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

Walter Roberson
Answer by 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
%}

4 Comments

Sean de Wolski on 13 May 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 on 13 May 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 on 13 May 2011

Danny, what is

round(centroids)

showing for you?

Sean de Wolski

Contact us