The diffuse fill algorithm is based on the selected seed points, with a custom color to fill the seed point of the Unicom region, by setting the upper and lower limits of the connected pixels and the connection mode to achieve different filling effect. Diffuse fills are often used to mark or separate portions of an image for further processing or analysis. The so-called diffuse fill, in a nutshell, is automatically selecting the area that is connected to the seed point, and then replacing the area with the specified color. A diffuse fill can also be used to get a mask area from an input image, which speeds up the processing or only handles the pixel points specified by the mask.In OpenCV, diffuse water filling is the most common method in the filling algorithm. And in OpenCV 2.X, there are two versions of FloodFill functions that have been rewritten with C + +. A version without masking mask, and a mask version. This masking mask is used to further control which areas will be filled with color (for example, when multiple fills are made to the same image). These two versions of FloodFill must select a seed point in the image and then fill all the similarities in the adjacent area with the same color, but not necessarily the same color as all neighboring pixels, and the result of the diffuse fill operation is always a contiguous area. The FloodFill function will color this point when the neighboring pixel is within the given range (from Lodiff to Updiff) or within the original Seedpoint pixel value range.
in OpenCV, the diffuse fill algorithm is implemented by the FloodFill function, which uses the color we specify to populate a connection domain from the seed point. Connectivity is measured by how close the pixel values are. Opencv2.x has two C + + rewrite versions of FloodFill.
The function prototypes in OpenCV are as follows:
int FloodFill (inputoutputarray image, point seedpoint, scalar newval, rect* rect=0, scalar lodiff=scalar (), scalar updiff= Scalar (), int flags=4)
int FloodFill (inputoutputarray image, point seedpoint, scalar newval, rect* rect=0, scalar lodiff=scalar (), scalar updiff= Scalar (), int flags=4)
Compared to the two versions, there is only the second parameter that differs from each other.
- The first parameter, image of type Inputoutputarray, input/output 1-channel or 3-channel, 8-bit or floating-point image, specific parameters are specified by subsequent parameters.
- The second parameter, the Inputoutputarray type of mask, which is the second version of the FloodFill exclusive parameter, represents the action mask. It should be a single-channel, 8-bit, long-and-wide image that is two pixels larger than the input image. The second version of FloodFill needs to use and update the mask, so this mask parameter must be ready and filled in here. The diffuse fill algorithm does not fill a non-0-pixel area of mask masks. For example, an edge detection operator's output can be used as a mask to prevent padding to the edge. Similarly, you can use the same mask in multiple function calls to ensure that the filled areas do not overlap. It is also important to note that mask masks will be 2 pixels larger than the image that needs to be filled, so the coordinates of the points corresponding to the input image (x, y) pixel points in mask are (x+1,y+1).
- The third parameter, the point type of the Seedpoint, is the start of the diffuse fill algorithm.
- The fourth parameter, the scalar type of newval, is the value of the pixel being dyed, that is, the new value of the pixel in the redraw area.
- The fifth parameter, a Rect of type rect*, has a default value of 0, an optional parameter that sets the minimum bounding rectangle area of the region that the FloodFill function will redraw.
- The sixth parameter, the scalar type of Lodiff, has a default scalar () that represents the negative difference in brightness or color between the current observed pixel value and its part neighborhood pixel values or the seed pixels to be added to the part (lower Brightness/color The maximum value of the difference).
- The seventh parameter, the scalar type of Updiff, has a default scalar () that represents the positive difference in brightness or color between the current observed pixel value and its part neighborhood pixel value or the seed pixel to be added to the part (lower Brightness/color The maximum value of the difference).
- The eighth parameter, the flags of the int type, the action marker, which contains three parts, is more complex and we'll look at it in detail.
A. The low eight-bit (No. 0 to 7th bit) is used to control the connectivity of the algorithm, preferably 4 (4 is the default) or 8. If set to 4, indicates that the fill algorithm only takes into account the current pixel's horizontal and vertical adjacent points, or, if set to 8, contains diagonally adjacent points, in addition to the above adjacent points. B. Middle Eight bitssection, the above about the high eight-bit floodfill_mask_only identifier has been stated very clearly, you need to enter a mask that meets the requirements. The middle eight-bit value of the flags parameter of the FloodFill is used to specify the value of the fill mask image. But if the value of the middle eight bits of the flags is 0, the mask is populated with one. c. eight-bit highthe section (16~23 bit) can be 0 or a combination of the following two option identifiers:Floodfill_fixed_range-If set to this identifier, the difference between the current pixel and the seed pixel is considered, otherwise the difference between the current pixel and its neighboring pixel is considered. In other words, this range is floating. floodfill_mask_only -If set to this identifier, the function will not be filled to change the original image (that is, ignore the third parameter newval), but instead to fill the mask image (mask). This identifier is only useful for the second version of FloodFill, because there is no mask parameter in the first version. all flags can be concatenated with the OR operator, or "|". For example, if you want to populate with 8 neighbors and populate the fixed pixel value range, fill the mask instead of populating the source image, and set the fill value to 47, the input parameter is this:
flags=8 | floodfill_mask_only | Floodfill_fixed_range | (47<<8)
eg
Mat srcimage=imread ("m:/image Processing Experiment/floodfill/test_.bmp"); Rect Ccomp;floodfill (Srcimage, point (1, 1), Cv_rgb (205, 205, 205), &ccomp, scalar (15, 15, 15), scalar, 8 | Floodfill_fixed_range); Imwrite ("m:/image Processing Experiment/floodfill/test_dst.bmp", srcimage);
the seed point is (in). The following is a comparison of the results of the original and diffuse water fills:
eg
Mat srcimage=imread ("m:/image processing Experiment/floodfill/cloud. bmp"); Mat Mask;mask.create (((srcimage). Rows + 2), (((srcimage). cols + 2), cv_8uc1); mask = scalar::all (0); Mat Imageroi;imageroi = Mask (Rect (1, 1, (srcimage). cols, (srcimage). rows)); Mat Dstimage; Mat Dstimage_canny;srcimage.copyto (Dstimage_canny); Cvtcolor (Dstimage_canny, Dstimage_canny, CV_RGB2GRAY); Medianblur (Dstimage_canny, Dstimage_canny, 7); Canny (Dstimage_canny, Dstimage, 3, 3 * 3, 3);d Stimage.copyto (Imageroi); Rect ccomp;//selects three seed points and assigns three fill colors to each one. The first time you call FloodFill, the canny edge detection mask is not added, so the edge of the cloud is corroded off part of it. The result is that some clouds have disappeared. After two calls to FloodFill, added the canny edge detection after the mask, the edge of the cloud has been well preserved, but part of the color changes in the larger area, canny, the edge is tested, the original color of the edge has been retained FloodFill (Srcimage, Point (223, 184), Cv_rgb (88,123,165), &ccomp, scalar (30, 30, 30), 8 | Floodfill_fixed_range); FloodFill (Srcimage, Mask, point (507), Cv_rgb (108,148,184), &ccomp, Scalar (+), S Calar (15, 15, 15), 8 | Floodfill_fixed_range); FloodFill (Srcimage, Mask, point (609, 582), Cv_rgb (137,173,197), &ccomp, ScAlar (+), Scalar (15, 15, 15), 8 | Floodfill_fixed_range); Imwrite ("m:/image processing experiment/floodfill/Cloud _dst.bmp", srcimage);
The following is the output of the original and diffuse water fill:
The output of the canny edge detection is also the mask in the FloodFill function. The black area can be filled and the white part retains its original color.
The function prototypes in EMGUCV are as follows:
Public Shared Function FloodFill (src as Emgu.CV.IInputOutputArray, mask as Emgu.CV.IInputOutputArray, seedpoint as System . Drawing.point, newval as Emgu.CV.Structure.MCvScalar, ByRef rect as System.Drawing.Rectangle, Lodiff as Emgu.CV.Structure.MCvScalar, Updiff as Emgu.CV.Structure.MCvScalar, Optional connectivity as Emgu.CV.CvEnum.Connectivity = fourconnected, Optional flags as Emgu.CV.CvEnum.FloodFillType = Default) as Integer
- The first argument, an image of typeEmgu.CV.IInputOutputArray .
- The second parameter, the mask of type Emgu.CV.IInputOutputArray , represents the action mask. It should be a single-channel, 8-bit, long-and-wide image that is two pixels larger than the input image. Unlike OpenCV, EMGUCV does not seem to have a second version of the FloodFill function, EMGU provides floodfill similar to OpenCV's second function. The diffuse fill algorithm does not fill a non-0-pixel area of mask masks. For example, an edge detection operator's output can be used as a mask to prevent padding to the edge. Similarly, you can use the same mask in multiple function calls to ensure that the filled areas do not overlap. It is also important to note that mask masks will be 2 pixels larger than the image that needs to be filled, so the coordinates of the points corresponding to the input image (x, y) pixel points in mask are (x+1,y+1).
- The third parameter, the point type of the Seedpoint, is the start of the diffuse fill algorithm.
- The fourth parameter, theEmgu.CV.Structure.MCvScalar type of newval, is the value of the pixel being dyed, that is, the new value of the pixel in the redraw area.
- The fifth parameter, rect*, of type Rect, is used to set the minimum bounding rectangle area of the region that the FloodFill function will redraw. Unlike OpenCV, there are no default values.
- The sixth parameter, Lodiff of typeEmgu.CV.Structure.MCvScalar , represents the negative difference in brightness or color between the current observed pixel value and its part neighborhood pixel value or the seed pixel to be added to the part (lower The maximum value of brightness/color difference).
- The seventh parameter, Updiff of typeEmgu.CV.Structure.MCvScalar , represents the positive difference in brightness or color between the current observed pixel value and its part neighborhood pixel value or the seed pixel to be added to the part (lower brightness The maximum value of/color difference).
- The eighth parameter, theEmgu.CV.CvEnum.Connectivity type of Connectivity. Control the connectivity of the algorithm, preferably 4 (4 is the default value) or 8. If set to 4, indicates that the fill algorithm only takes into account the current pixel's horizontal and vertical adjacent points, or, if set to 8, contains diagonally adjacent points, in addition to the above adjacent points.
- the Nineth parameter,the Emgu.CV.CvEnum.FloodFillType type of flags. either 0 (0 is the default) or a combination of the following two option identifiers: Floodfill_fixed_range -If set to this identifier, the difference between the current pixel and the seed pixel is considered, otherwise the difference between the current pixel and its neighboring pixel is considered. In other words, this range is floating. floodfill_mask_only -If set to this identifier, the function will not be filled to change the original image (that is, ignore the third parameter newval), but instead of filling the mask image (mask) 。
eg
Dim bkgraywhite As New Gray (255) Dim bkgrayblack As New Gray (0) Dim img As Image (of Bgr, byte) = new Image (of Bgr, Byte) ("M: \ Image processing Experiment \floodfill\ Cloud 1.bmp ") Dim Img_medianblur as Image (of Bgr, byte) = New image (of Bgr, Byte) (IMG. Width, IMG. Height) img. CopyTo (Img_medianblur) Cvinvoke.medianblur (IMG, Img_medianblur, 7) Dim Mask as Image (of gray, Byte) = New Image (of Gray, Byt e) (IMG. Width + 2, img. Height + 2, bkgrayblack) ' Bgrcvinvoke.floodfill (IMG, Mask, New System.Drawing.Point (2, 2), New Mcvscalar (165, 123, (), new System.Drawing.Rectangle (0, 0, 0, 0), New Mcvscalar (5, 5, 5), New Mcvscalar (5, 5, 5), Emgu.CV.CvEnum.Connectivity.Eig htconnected, Emgu.CV.CvEnum.FloodFillType.FixedRange) Cvinvoke.cvsetimageroi (Mask, New System.Drawing.Rectangle (1, 1, IMG. Width, IMG. Height)) Dim Img_canny as Image (of gray, byte) = New Image (of Gray, Byte) (IMG. WiDTH, IMG. Height, Bkgrayblack) Cvinvoke.canny (Img_medianblur, Img_canny, 5, 5 * 3) Img_canny. CopyTo (Mask) cvinvoke.cvresetimageroi (mask) Cvinvoke.floodfill (IMG, mask, New System.dra Wing. Point (668, 570), New Mcvscalar (197, 173, 137), new System.Drawing.Rectangle (0, 0, 0, 0 ), new Mcvscalar (+), new Mcvscalar (5, 5, 5), Emgu.CV.CvEnum . connectivity.eightconnected, Emgu.CV.CvEnum.FloodFillType.FixedRange) img. Save ("m:\ image processing experiment \floodfill\ Cloud 1_result.bmp")
References: Bradski & Kaebler "Learning OpenCV (Chinese version)" · Tsinghua University Press · 2009
http://www.emgu.com/wiki/files/3.0.0-alpha/document/html/1ebdfe41-1e71-9440-a71b-719b64cc39df.htmhttp:// blog.csdn.net/poem_qianmo/article/details/28261997
OpenCV and EMGUCV filling in the water