Convert binary ROI to polygon

I have a binary image with a concave polygon drawn in a paint application. Using find() I get a set of points, but they aren't ordered so all the MATLAB polygon functions choke on them. How do I go about turning my 2D line-mask into a proper polygon variable?
Edit: added example
Edit2: It was drawn with 1px pencil in a continuous motion, then 'cleaned' with bwareaopen(). So each pixel should have at most 2 neighbours.

Más respuestas (2)

Alessandro Masullo
Alessandro Masullo el 22 de Abr. de 2016
Editada: Alessandro Masullo el 22 de Abr. de 2016

0 votos

You can use bwboundaries to get the boundaries of a binary image.
If you want to sort them, you can evaluate the centre of your points, the angle between each point and the centre and then sort all the points by the angle.
If you just want a rough estimation of the polygon, instead, you can use the douglas-peucker simplification algorithm (already coded in matlab or from the file exchange)

1 comentario

Jurgen
Jurgen el 22 de Abr. de 2016
I think the centering trick only works if the reference point is inside the polygon, which in my case is true, but it isn't true for all concave polygons.

Iniciar sesión para comentar.

Image Analyst
Image Analyst el 22 de Abr. de 2016
bwboundaries() gives you an ordered set of coordinates as you move along the perimeter of your shape. I don't think you should need to do any other kind of sorting. For example, a list of coordinates from bwboundaries can be used in poly2mask() and polyarea(), etc. Here's a snippet from my tutorial:
hold on;
boundaries = bwboundaries(binaryImage);
numberOfBoundaries = size(boundaries, 1);
for k = 1 : numberOfBoundaries
thisBoundary = boundaries{k};
plot(thisBoundary(:,2), thisBoundary(:,1), 'g', 'LineWidth', 2);
end
hold off;
Note that x = thisBoundary(:,2), and y = thisBoundary(:,1) -- perhaps backward from what you might think, but it gives (row, column), which is (y,x), not (x,y).

4 comentarios

Jurgen
Jurgen el 22 de Abr. de 2016
got it thanks, how would you go about adding/subtracting N pixels from the border or the polygon? All I can find is scaling the area, but my shape is irregular so I can't calculate the scaling factor. I was thinking erosion, but would prefer to work with polygon data instead of binary mask (more exact).
Image Analyst
Image Analyst el 22 de Abr. de 2016
To enlarge an image by padding it with extra pixels along the edges, use padarray().
To enlarge a blob in a binary image, use imdilate(). This will keep the image the same size but just enlarge the blob(s) inside the image.
Jurgen
Jurgen el 23 de Abr. de 2016
But how can I erode an N pixel layer from a blob e.g. the outer 10px, is it possible to control the amount N?
Image Analyst
Image Analyst el 23 de Abr. de 2016
Yes, to erode, use imerode(). Also look at strel() to see the different shapes you can have for the structuring element.

Iniciar sesión para comentar.

Preguntada:

el 22 de Abr. de 2016

Comentada:

el 23 de Abr. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by