This is the third day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021
1 the introduction
Polygon drawing generally includes four steps, namely: intersection, sorting, matching, color filling
In the stage of color filling, 4- connected boundary seed filling algorithm is adopted
Seed: Boundary/interior point represents any point within a region
Seed filling algorithm is mostly used for polygon filling, polygon can be customized drawing, or matrix input
In this paper, we also consider taking a picture as the research object and changing its filling color to realize the advanced application of seed filling algorithm ~
2 ideas
The general idea of the algorithm is as follows, loop the following operations, when the top of the stack is empty exit loop:
-
Start by specifying a seed pixel and pushing it off the stack
-
Set the stack pixels to fill color
-
Check out the 4-adjacent points of the stack pixel, and if one of them is a boundary color and not polygonal color, push that pixel onto the stack
3 process
Seed_fill4 (X,Y,Pic, VM, VN)
The stack is initialized and the graph to be filled is displayed
figure(1);
imshow(Pic); % displays the graph to be filled for comparison with the filled graph
a=[];
b=[];
top=0;
top=top+1;
a(1,top)=x;
b(1,top)=y;
Pic(x,y)=0;
Copy the code
Enter the loop again, judge the 4-adjacent point of seed pixel, and update the polygon matrix Pic. Exit the loop when top>0
Finally, the filled polygon is displayed
figure(2)
imshow(Pic);
Copy the code
See seed_fill4(gitee.com) for the full code
4 the result
4.1 Custom polygons and their filling results
Custom polygons can be drawn in the drawing software, or you can define polygons directly in the matrix way. In this paper, the directly defined matrix is adopted, i.e
Pic([3:99], [3:99]) =1;
Pic([2:99], [99:100]) =0;
Pic([99:100], [1:100]) =0;
seed_fill4(95.80,Pic,100.100);
Copy the code
Since the computer treated the white pixel point as “1” and the black pixel point as “0”, the initial polygon was set with black boundary and white content:
Set the filling color to black and the filling effect is as follows:
4.2 Pictures and filling results
Image filling steps are as follows:
-
Use imread() to read the image
-
Grayscale
-
Extract the matrix with a pixel value greater than 125 and get the length and width
-
Call the seed fill function, and the matrix in the input parameter is the matrix obtained in step 3
The initial image is:
Set the filling color to black and the filling effect is as follows: