Extracting image coordinates from binary image
6 ビュー (過去 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!