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

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

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 :

*No products are associated with this question.*

Answer by Sean de Wolski
on 13 May 2011

Accepted answer

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

Show 10 older 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.

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.

danny agus
on 13 May 2011

because i can't get the coordinates ..

so i think i can get the coordinates use the pixelinfo

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 %}

Show 1 older comment

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

## 2 Comments

## Doug Hull

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/7394#comment_15530

please clarify your question.

## Image Analyst

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/7394#comment_119667

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