Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

how can i get each coordinates of binary image

Asked by Adhi on 23 Nov 2012

i have this image http://www.freeimagehosting.net/n59z8

how can i get the each coordinates that shown in this image http://www.freeimagehosting.net/pd85c (up, down, right, left and center)

0 Comments

Adhi

1 Answer

Answer by Image Analyst on 23 Nov 2012
Edited by Image Analyst on 25 Nov 2012
Accepted answer

Find the centroid with regionprops(). See my Image Segmentation Tutorial if you don't know how. Then convert the centroid to integer to get the row and column.

centroidColumn = int32(centroid(1)); % "X" value
centroidRow = int32(centroid(2)); "Y" value.

Then extract a row or column and use find to find the first and last element that's set:

middleColumn = binaryImage(:, centroidColumn);
topRowY = find(middleColumn, 1, 'first'); % Find top.
bottomRowY = find(middleColumn, 1, 'last'); % Find bottom.
middleRow = binaryImage(centroidRow, :);
leftColumnX = find(middleRow, 1, 'first'); % Find left.
rightColumnX= find(middleRow, 1, 'last'); % Find right.

5 Comments

Adhi on 25 Nov 2012

this is the result: http://www.freeimagehosting.net/twva2

its working for top and bottom, but why its not working for right and left.? always get the wrong result. i got the centroid from your image segmentation tutorial as well.

centroidColumn = int32(blobCentroid(1)); % "X" value
centroidRow = int32(blobCentroid(2)); %"Y" value.

Then extract a row or column and use find to find the first and last element that's set:

middleColumn = J(:, centroidColumn);
topRowY = find(middleColumn, 1, 'first') % Find top.
bottomRowY = find(middleColumn, 1, 'last') % Find bottom.
middleRow = J(:, centroidRow);
leftColumnX = find(middleRow, 1, 'first') % Find left.
rightColumnX= find(middleRow, 1, 'last') % Find right.

and i added this code for mark the coordinate:

line(centroidColumn, centroidRow, 'Marker', '*', 'MarkerEdgeColor', 'r')
line(centroidColumn, topRowY, 'Marker', '*', 'MarkerEdgeColor', 'r')
line(centroidColumn, bottomRowY, 'Marker', '*', 'MarkerEdgeColor', 'r')
line(leftColumnX, centroidRow, 'Marker', '*', 'MarkerEdgeColor', 'r')
line(rightColumnX, centroidRow, 'Marker', '*', 'MarkerEdgeColor', 'r')
Adhi on 25 Nov 2012

i dont know how to move my post to comments, so i deleted and moved my last post to this comments section. but i can't move yours.

and your answers works perfectly, thanks.

Image Analyst on 25 Nov 2012

middleRow should be defined like this:

middleRow = binaryImage(centroidRow, :);

and line() takes 2 x and 2 y coords:

line([x1, x2], [y1 y2]........ Or line([col1, col2], [row1, row2], ......

and I'd recommend using more descriptive names than single letters, for maintainability of the code.

Glad it's finally working for you.

Image Analyst

Contact us