Finding vertices of a polygon binary mask
8 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
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.
0 Comments
See Also
Categories
Find more on Elementary Polygons in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!