Extracting image coordinates from binary image

20 Ansichten (letzte 30 Tage)
SimonW
SimonW am 1 Feb. 2015
Kommentiert: David Young am 2 Feb. 2015
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

Akzeptierte Antwort

David Young
David Young am 1 Feb. 2015
Bearbeitet: David Young am 1 Feb. 2015
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 Kommentare
Image Analyst
Image Analyst am 1 Feb. 2015
Bearbeitet: Image Analyst am 1 Feb. 2015
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.
David Young
David Young am 2 Feb. 2015
Thanks!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 1 Feb. 2015
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 Kommentare
SimonW
SimonW am 1 Feb. 2015
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.
Image Analyst
Image Analyst am 1 Feb. 2015
Yep, you got it. To add measurements, list them in the arg list:
blobMeasurements = regionprops(labeledImage, 'Centroid',...
'MajorAxisLength', 'MinorAxisLength');

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by