OpenCV(40): Image segmentation—flood filling

1. Flood filling principle

        The Flood Fill algorithm in image segmentation is a pixel classification method based on region growth. The principle is to start from the seed point in the image, gradually expand to the surroundings, and decide whether to attribute adjacent pixels to the same area according to certain conditions.

The basic principles of flood filling are as follows:

  • Select a seed point.
  • Taking the seed point as the center, determine the difference between the pixel value of the 4-neighborhood or the 8-neighborhood and the pixel value of the seed point, and add pixels with a difference less than the threshold into the area.
  • Use the newly added pixels as new seed points and repeat Step 2 until no new pixels are added to the area.

2. Flood filling segmentation method function floodFill() 

int floodFill(InputOutputArray image,

InputOutputArray mask,

Point seedPoint,

Scalar newVal,

CV_OUT Rect* rect = 0,

Scalar loDiff = Scalar(),

Scalar upDiff = Scalar(),

int flags = 4);

Parameter Description:

  • image: Input and output images, the image data type can be single-channel or three-channel images of CV_8U or CV_32F.

  • mask: Mask matrix, a single-channel image with dimensions such as 2 greater than the width and height of the input image, used to mark areas filled with water.

  • seedPoint: seed point.

  • newVal: the new pixel value classified into the pixels in the seed point area.

  • rect: The minimum rectangular boundary of the flood-filled area of ​​the seed point. The default value is 0, which means no boundary is output.

  • loDiff: The lower bound difference value added to the seed point area condition. When the difference between the pixel value of a pixel in the neighborhood and the pixel value of the seed point is greater than this value, the pixel is added to the area where the seed point is located.

  • upDiff: The upper bound difference value added to the seed point area condition. When the difference between the pixel value of the seed point and the pixel value of a pixel in the neighborhood is less than this value, the pixel is added to the area where the seed point is located.

  • flags: The operation flag of the flood filling method, which consists of three parts, respectively representing the neighborhood type, mask pixel value and the rules of the filling algorithm.

return value:

  • Returns an integer value representing the actual number of pixels filled.

Sample code:

void floodFill_f(Mat mat){
    // 如果是四通道图像,则要把四通道图像转换成三通道
    Mat image;
    cv::cvtColor(mat, image, cv::COLOR_BGRA2BGR);

    RNG rng(10086);//随机数,用于随机生成像素
    //设置操作标志flags
    int connectivity=4;//连接领域方式
    int  maskVal=255;//掩码图像的数值
    int  flags=connectivity|(maskVal<<8)|FLOODFILL_FIXED_RANGE;//漫水填充操作方式标志
    Rect rect;                     // 输出的填充区域
    //设置与选中像素点的差值
    Scalar loDiff=Scalar (20,20,20);
    Scalar upDiff=Scalar (20,20,20);
    //声明掩摸矩阵变量
    Mat mask=Mat::zeros(image.rows+2,image.cols+2,CV_8UC1);
    ostringstream ss;
    for(int i=0;i<20;i++){
        //随机产生图像中某一像素值
        int py=rng.uniform(0,image.rows-1);
        int px=rng.uniform(0,image.cols-1);
        Point point=Point (px,py);
        //彩色图像中填充的像素值
        Scalar newVal=Scalar (rng.uniform(0,255),rng.uniform(0,255),rng.uniform(0,255));
        //浸水填充函数
        int area= floodFill(image,mask,point,newVal,&rect,loDiff,upDiff,flags);
        //输出像素点和填充的像素数目
        ss<<"像素点x:"<<point.x<<"   y:"<<point.y<<"   填充像素数目"<<area<<endl;
    }
    LOGD("%s",ss.str().c_str());
    //输出填充的图像结果
    imwrite("/sdcard/DCIM/img.png",image);
    imwrite("/sdcard/DCIM/mask.png",mask);
}

Output image:

Mask image:

Guess you like

Origin blog.csdn.net/weixin_63357306/article/details/132855968