S
S
SergeySerge112022-03-15 17:55:23
Java
SergeySerge11, 2022-03-15 17:55:23

How to cut image area under contour, Algorithm?

Or Easier what is the algorithm of the fillpolygon method?
There is a program that gives the contour of the image, let's say it finds a face, and you need to cut it out on a white background, the contour is an array of consecutive points clockwise. Need to overlay this path on the image, and cut out all the pixels below it? The circuit can be very very very complex. For example, in the form of a snake.
No matter how I tried, I can't.
My algorithm.
1. I create a new array [y][x] and put the contour points into it. (I think this for any implementation)
2. Here I go from top to bottom, from left to right, and wait for the first point of the contour to be found, then I paint over them to the next point.
3. As soon as the point is again, I stop coloring, and so on until the next one.
Problems when Peak, ^ V shaped and when dots follow each other.
There is a function in the FillPolygon libraries How does it work??? In fact, my contour is a polygon of consecutive points. And I need to paint them.
6230a90d257bd817554540.png
Such a thing (and I already tried to complicate it at times did not help), does not work with consecutive points, for example (10.11) (10.12) (10.13) and with peak points.

for (int i = 0; i < contour.Lenght; i++)
            {
                pixelsContour[contour.Points[i].Y - contour.Y1][contour.Points[i].X - contour.X1] = color;
            } 
            for (int y = 0; y < dy; y++)
            {
                bool isOpenX = false;
               
                for (int x = 0; x < dx; x++)
                {

                    if (isOpenX)
                    {
                        if(pixelsContour[y][x] == color)
                        {
                                isOpenX = false;          /// закрыть         
                        }
                        else
                        {
                           
                                pixelsContour[y][x] = color;
                        }

                    }
                    else
                    {
                        if (pixelsContour[y][x] == color)      
                            isOpenX = true;  
                }
            }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2022-03-15
@SergeySerge11

FillPolygon works via breadth-first traversal .
First, paint all the points of the contour in one cycle.
Then take any point inside the polygon, paint it and put it in the queue. While the queue is not empty, take a point from there, take 4 or 8 of its neighbors, and if they are not painted yet, paint them and put them in the queue.
It can be difficult to select the very first point inside the polygon. But here you can carefully walk along the polygon counterclockwise and take the first unpainted point to the left of the bypass. Here, for each point, you can find vectors for two neighbors along the contour, then go through 4-8 neighboring pixels and take the uncolored one, the vector for which lies between the two vectors along the contour.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question