Extracting image coordinates from binary image
18 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
David Young
2015-2-1
编辑:David Young
2015-2-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
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.
更多回答(1 个)
Image Analyst
2015-2-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 个评论
Image Analyst
2015-2-1
Yep, you got it. To add measurements, list them in the arg list:
blobMeasurements = regionprops(labeledImage, 'Centroid',...
'MajorAxisLength', 'MinorAxisLength');
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Segmentation and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!