原文:http://blogs.mathworks.com/steve/2006/06/10/determining-point-position-in-mri-phantom/
Blog reader Jonathan from St. Jude Children's Research Hospital sent me an image derived from an MRI peg phantom:
bw = imread('http://blogs.mathworks.com/images/steve/62/mri_peg_phantom.png');
imshow(bw)
Jonathan wanted to know how to determine the position of each point. The functions
bwlabel and
regionprops do the trick.
The function bwlabel takes a binary image and figures which groups of white pixels are connected to each other.
L = bwlabel(bw);
The output
L is called a
label matrix. It has the same size as
bw and contains nonnegative integers. Each positive integer value corresponds to a particular object. For example, to display the 10th object, just compare
L to 10:
imshow(L == 10)
The function
regionprops computes a number of different geometric properties of all the different regions contained in a label matrix. All we need for this application is the centroid:
s = regionprops(L, 'Centroid')
The size of the structure array
s tells you the number of labeled objects: 205. The centroid of the 10th object is:
centroid_10 = s(10).Centroid
Here's a simple way to superimpose the centroid locations onto the original image:
imshow(bw)
hold on
for k = 1:numel(s)
plot(s(k).Centroid(1), s(k).Centroid(2), 'x')
end
hold off