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

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

  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

thx for the quick reply :)
1. what's the mean by round the coordinates ?
and how i can do that ?
2. when i try to threshold and next i want to find the centroids
it always fail..
any suggest ?
sorry for so many questions..
i'm still learning in here :)
CMIIW
1) so the centroid could be say: 37.7 and 49.2. No data sample exists at this point since it's non-integer. The easy method would be to get the data at the closest point i.e. round([37.7, 49.2]) which is 38 and 49 so then I(38,49) would be the value. The more difficult method would be to interpolate using the data in points (37,49),(37,50),(38,49),(38,50).
2) I can't help you on this without seeing your code. Edit your original question to include sample code.
umm
sorry
first how i can get those number ?
the 37.7 and 49.2
those number are x and y right ?
thx for the quick reply :)
Yes x,y NOTE the difference between x/y and row/column (they're swapped). By running the code you have provided above I get blue dots on the centroids. Good job! What else do you need help with?
the image is still on the orginal image..
i want the image is on the "threshold" image
i mean, after i threshold then i want to add the centroid
most likely like that
btw thx very much for the reply :)
are u sure u get the bluedot ?
i just got this error message on my matlab command window
??? Error using ==> regionprops>ParseInputs
Use bwlabel(BW) or double(BW) convert binary image to
a label matrix before calling regionprops.
Error in ==> regionprops at 114
[L, requestedStats] = ParseInputs(officialStats, varargin{:});
Error in ==> tes1 at 4
s = regionprops(BW, 'centroid');
is any relation about the version ?
i still use 7.1
It did work me, though not sure why.
CC = bwconncomp(BW);
s = regionprops(CC,'centroid');
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 :(.
Yes. Copying and pasting your EXACT code above. See my next answer.

Sign in to comment.

More Answers (1)

All figures were closed, all variables cleared. Perhaps you need to clear your figure?

5 Comments

[URL=http://imageshack.us/photo/my-images/148/zzzzn.png/][IMG]http://img148.imageshack.us/img148/461/zzzzn.png[/IMG][/URL]
Uploaded with [URL=http://imageshack.us]ImageShack.us[/URL]
now i'm more confuse :$
how can the same code
but different result
i know that these codes will work..
You're probably running on an older (or newer) version. I'm on 2009b Mac. Use the bwconncomp code as I suggested:
I = imread('coins.png');
level = graythresh(I);
BW = im2bw(I,level);
CC = bwconncomp(BW);
s = regionprops(CC, 'centroid');
centroids = cat(1, s.Centroid);
imshow(BW)
hold on
plot(centroids(:,1), centroids(:,2), 'b*')
hold off
i'm running on version 7
release on 2005 :(..
i'm still check the variables..
emm..
i still wondering
u said about "coordinates returned" by the centroid
how i could find the coordinates ?
or maybe how i can get the value returned by centroid ?
axes2pix syntax ?
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
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!