Finding vertices of a polygon binary mask

8 views (last 30 days)
Yin Lok
Yin Lok on 23 Mar 2014
Answered: Image Analyst on 28 Mar 2014
Hi, all. I have a question on finding the vertices of a polygon binary mask. I want to obtain the x,y coordinates of the polygon. However, my code doesn't work. is there any problem?
if true
mask = createMask(h);
structBoundaries = bwboundaries(mask);
xy=structBoundaries{1}; % Get n by 2 array of x,y coordinates.
x = xy(:, 2); % Columns.
y = xy(:, 1); % Rows
[topLine, indice1] = min(x);
x1 = topLine;
y1 = y(indice1);
[bottomLine, indice2] = max(x);
x2 = bottomLine;
y2 = y(indice2);
[leftColumn, indice3] = min(y);
x3 = x(indice3);
y3 = leftColumn;
[rightColumn, indice4] = max(y);
x4 = x(indice4);
y4 = rightColumn;
width = bottomLine - topLine + 1;
height = rightColumn - leftColumn + 1;
polygon = int32([x1 y1 x2 y2 x3 y3 x4 y4])
end

Answers (1)

Image Analyst
Image Analyst on 28 Mar 2014
x is not line. y is not column. You got it reversed - a very common mistake. (x,y) IS NOT the same as (row, column). You need to say y1 = topLine, not x1=topLine. So swap x and y and it should be fine, though you really only need x1,x2,y1,y2 not all the way up to 4 each of them.

Categories

Find more on Elementary Polygons in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!