How to create a polygon using edges?

So I have the image as shown. The edges are of color yellow and red (just ignore the small yellow circle). How can I create a polygon using the edges? please help me! This is the image with edges.
This is the image with the polygon colored black.
This is the resulting image with the white part connoting the removed polygon.

 Accepted Answer

Why is your "removed" region white with some black islands in it? And, if your polygons are always convex like that, just pass them into convhull() then pass the convex vertices into poly2mask()
[rows, columns, numColorChannels] = size(binaryImage);
vertexIndexes = convhull(x, y);
cx = x(vertexIndexes); % Extract the convex vertices only.
cy = y(vertexIndexes);
mask = poly2mask(cx, cy, rows, columns); % Create mask from convex hull
binaryImage(mask) = true; % Make all white.

2 Comments

Just ignore the black portions. I just used Paint in order to show my intended result. It must be white only, and later on that portion must be removed from the image.
What if the polygon are not always convex? Can I still use convhull()?
If it's not convex but you still have an ordered list with no overlap (like figure eights) then you can skip the convhull part and just go directly to poly2mask. If you do have figure eights, or a non-ordered list, then you'll just have to see what the mask looks like. If some parts of the mask are negative/blank/not-filled, then you might have to call imfill().

Sign in to comment.

More Answers (1)

There are other implementations of the same kind of routine; look in the File Exchange

4 Comments

Well, the purpose of the polygon is to exclude the polygon from the picture. Basically, what I want to do is to create a polygon using the edges and then remove that polygon from the image. Is that possible with what you've suggested?
I do not see any closed polygon(s) in your image. Please show the polygon and the final image where you masked out the inside (made it black).
I edited my question with other pictures. Please help!
Threshold on the color channels to find the boundaries of the color portion. Scribble a line in the binary image along the edge of the picture, between the two endpoints on the edge. imfill(). You now have a mask to use to set (or clear) the original image.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!