MATLAB: How i know the value of a pixel in a centroid of image

none

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

Best 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.