Extracting image coordinates from binary image

I'm new to matlab so apologies if this has been asked before.
I have a grayscsale image (of a tooth) that I need to extract the following data from;
Image centroid Max/Min x Max/Min y
I read the image into matlab and converted it to binary as below
im = 'C:\users\simon\desktop\w14.png' imread(im) bw = im2bw(imread(im),0.98)
inverted the data values so the tooth pixels have a value of 1 and the background is 0
Wrote the x and y values out by
[x y]=find([bw])
If i then take the mean of x and y, or use stats=regionprops(bw) to get the same and then plot the corresponding point back on the image it plots outside of the tooth.
Am i doing something wrong, or is this likely to be something wrong with the data itself?
Thanks!
Simon

 채택된 답변

David Young
David Young 2015년 2월 1일
편집: David Young 2015년 2월 1일

1 개 추천

Normally, one needs to write
[y, x] = find(bw); % y before x (array ordering)
because the row indices are returned before the column indices. (Note that you don't need the extra [] round the argument.)
Then, provided there was only one non-zero region in bw, the means of x and y will plot at the centroid, using
plot(mean(x), mean(y), '*'); % x before y (image ordering)
However, you say you also have a problem with the results from regionprops. To go further, you may need to attach the image to your question, and show more of your code.

댓글 수: 2

Image Analyst
Image Analyst 2015년 2월 1일
편집: Image Analyst 2015년 2월 1일
Simon, you don't say "thanks" to David as your own "Answer" and then accept your own answer - David doesn't get any reputation points for that. So I've deleted your "Answer" moved it here since it's a reply to David rather than an "Answer" to your original question, and you now can accept his answer:
Thanks David - works perfectly - I hadn't realized that y was returned first (note to self - read manual first!)
P.S. if you want a tutorial on how to use regionprops() to find the centroid and weighted centroid of an image, see my Image Segmentation Tutorial in my File Exchange.
Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2015년 2월 1일

1 개 추천

Try this:
labeledImage = bwlabel(bw);
blobMeasurements = regionprops(labeledImage, 'Centroid');
% We can get the centroids of ALL the blobs into 2 arrays,
% one for the centroid x values and one for the centroid y values.
allBlobCentroids = [blobMeasurements.Centroid];
centroidsX = allBlobCentroids(1:2:end-1);
centroidsY = allBlobCentroids(2:2:end);
% Put the labels on the rgb labeled image also.
for k = 1 : numberOfBlobs % Loop through all blobs.
plot(centroidsX(k), centroidsY(k), 'b*', 'MarkerSize', 15);
if k == 1
hold on;
end
text(centroidsX(k) + 10, centroidsY(k),...
num2str(k), 'FontSize', fontSize, 'FontWeight', 'Bold');
end

댓글 수: 2

SimonW
SimonW 2015년 2월 1일
Thanks ..
I have a bit of a follow up ...
Is there any way to do this on a 3D image stack to give the centroid as x,y,z? I guess I would need to loop through each image in turn?
I also need to get the Major and Minor Axes which regionprops gives for the stack in the x/y, x/z and y/z planes.
Yep, you got it. To add measurements, list them in the arg list:
blobMeasurements = regionprops(labeledImage, 'Centroid',...
'MajorAxisLength', 'MinorAxisLength');

댓글을 달려면 로그인하십시오.

질문:

2015년 2월 1일

댓글:

2015년 2월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by