how i know the value of a pixel in a centroid of image

3 views (last 30 days)
i wanna ask, could we see the value of a centroid of an image
for example ; s = regionprops(BW, 'centroid');
can i know the pixel value of s ?
and 2nd i wanna ask.. i always try to combine 2 function or 2 jobs in 1 m.file but i always fail
for example i want to threshold picture and then i want to do some imageprocessing after threshold could i doing that in 1 m.file or i should seperate it into few m.file ?
can anyone help ? i'm a newbie in matlab :)
any help is appreciate
the code for the 2nd question :
I = imread('coins.png');
level = graythresh(I);
BW = im2bw(I,level);
s = regionprops(BW, 'centroid');
centroids = cat(1, s.Centroid);
imshow(BW)
hold on
plot(centroids(:,1), centroids(:,2), 'b*')
hold off

Accepted Answer

Sean de Wolski
Sean de Wolski on 5 May 2011
  1. Easy way: round the coordinates returned from regionprops and use those as indices to get the centroid. Harder way is to interpolate that point based on its 4 nearest neighbors.
  2. You absolutely can do it all in one m-file. It really depends on the application whether you want to or not. If it's a simple one line threshold (eg. bw = x>50) then definitely do it in one file. If it's something more complicated (eg. distance to the nearest location on the convex hull of an image of connected tauri) then you probably want a second m-file designed specifically for that.
  10 Comments
danny agus
danny agus on 6 May 2011
Error in ==> tes1 at 4
CC = bwconncomp(BW);
hmm :-?
confuse :$
are u sure u can get the centroid ?
the codes are same, but it can't work on my comp..
hmm..
i have searched, but i can't fix it :(.
Sean de Wolski
Sean de Wolski on 6 May 2011
Yes. Copying and pasting your EXACT code above. See my next answer.

Sign in to comment.

More Answers (1)

Sean de Wolski
Sean de Wolski on 6 May 2011
All figures were closed, all variables cleared. Perhaps you need to clear your figure?
  5 Comments
Sean de Wolski
Sean de Wolski on 6 May 2011
That explains the difference. Try this:
I = imread('coins.png');
level = graythresh(I);
BW = im2bw(I,level);
L = bwlabel(BW);
s = regionprops(L, 'centroid');
centroids = cat(1, s.Centroid);
imshow(BW)
hold on
plot(centroids(:,1), centroids(:,2), 'b*')
hold off
danny agus
danny agus on 6 May 2011
still failed :(
but i have try on the newest version
and it works
the codes are working
thx very much :)

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!