OpenCV FloodFill (diffuse water filling) and object selection

Source: Internet
Author: User

Introduction
This article mainly introduces the floodfill of OpenCV (diffuse water filling) and the selection of objects based on it.

FloodFill Use
FloodFill function
C + +: int FloodFill (inputoutputarray image, Inputoutputarray mask, point                 seedpoint, Scalar newval, rect* rect=0, Scala R lodiff=scalar (), Scalar updiff=scalar (), int flags=4);
Inputoutputarray: Input and output images.  Mask:            the input mask image.   Seedpoint:      The starting position where the algorithm begins processing. NewVal:         All points in the image that are selected by the algorithm are populated with this value.      rect: The            minimum bounding matrix. Lodiff:         The difference between the maximum low brightness.      Updiff:         The difference between the maximum high brightness. Flag:           Select the algorithm connection method.
To put it simply, select Point Seedpoint, then select the dots around it that have little difference in color and change their values to newval. If the selected point encounters a mask mask, it discards the spread fill for that direction.

Specific Code
#include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" #include <iostream> using namespace cv;using namespace Std; int Lodiff = Updiff = 20; Mat image0, image, mask;int newmaskval = 255; static void Onmouse (int event, int x, int y, int, void*) {Mat DST = image; Point seed = Point (x, y); Scalar newval = scalar (0, 0, 0); Rect ccomp;int lo = lodiff;int up = Updiff;int flags = 8 + (Newmaskval << 8) + Cv_floodfill_fixed_range; FloodFill (DST, mask, seed, newval, &ccomp, scalar (lo, lo, lo), scalar (up, up, up), flags), Imshow ("One", DST);} int main (int argc, char** argv) {char* filename = ARGV[1];IMAGE0 = imread (filename, 1); Image0.copyto (image); Mask.create ( Image0.rows+2, image0.cols+2, CV_8UC1); Cv::rectangle (mask,cvpoint (0, 0), Cvpoint (255,255,255), Cvscalar (8)  ; Namedwindow ("image", 0); Createtrackbar ("Lo_diff", "image", &lodiff, 255, 0); Createtrackbar ("Up_diff", "image", &A Mp;updiff, 255, 0); Imshow ("One", image); Imshow ("A", mask); SetmoUsecallback ("One", Onmouse, 0); Waitkey (0); return 0;}

Code explanation
1. Load the picture that needs to be processed to IMAGE0, then copy the picture information to image.
       char* filename = argv[1];image0 = imread (filename, 1); Image0.copyto (image);
2, set the mask, note that the mask size needs to be +2 of the picture length and width. The mask area of a rectangle is then selected.
        
3, set two TrackBar, used to dynamically set the size of Lodiff and Updiff.
        Namedwindow ("image", 0); Createtrackbar ("Lo_diff", "image", &lodiff, 255, 0); Createtrackbar ("Up_diff", "image", &A Mp;updiff, 255, 0);
4, display the original image and mask image, and register the mouse processing function.
        Imshow ("One", "image"), Imshow ("0", mask), Setmousecallback ("One", "onmouse");
5, the mouse selects the reference point, sets up the floodfill uses the dynamic spread 8 to connect the algorithm, displays the picture which is processed by the FloodFill.
static void Onmouse (int event, int x, int y, int, void*) {Mat DST = image; Point seed = Point (x, y); Scalar newval = scalar (0, 0, 0); Rect ccomp;int lo = lodiff;int up = Updiff;int flags = 8 + (Newmaskval << 8) + Cv_floodfill_fixed_range; FloodFill (DST, mask, seed, newval, &ccomp, scalar (lo, lo, lo), scalar (up, up, up), flags), Imshow ("One", DST);}


Effect Demo
1. Original Image:                      
2. Mask Image:                      
3, select the image generated after the sky:                      



Object Selection

The so-called object selection is to detect the edge of the image first, then use the edge as a mask, and then use FloodFill to process the selected points.


Specific Code
#include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" #include <opencv2/core/core.hpp > #include <opencv2/core/mat.hpp> #include <math.h> #include <string.h> #include <opencv/cv.h > #include <stdio.h> #include "xihua_2.h" #include <iostream> using namespace cv;using namespace std; int Lodiff = Updiff = 20; Mat image0, image, mask;int Newmaskval = 255;int ratio = 3;int Kernel_size = 3;int Lowthreshold = 43; Class Morphofeatures{private:cv::mat Cross;cv::mat diamond;cv::mat square;cv::mat x; public:cv::mat getEdges (const CV: : Mat &image) {Cv::mat Result;cv::morphologyex (Image,result,cv::morph_gradient,cv::mat ()); ApplyThreshold ( result); return result;} void Applythreshold (Cv::mat & result) {Cv::threshold (Result,result, 40,255,cv::thresh_binary);}}; Morphofeatures Morpho; void Myresize (mat& mat1, mat& mat2, int width, int height) {iplimage pi_1 = mat1, pi_2; mat2 = Cv::mat (width, heigh T, CV_8UC1, 1);p i_2 = MAT2; Cvresize (&pi_1, &pi_2, 1);} static void Onmouse (int event, int x, int y, int, void*) {Mat DST = image; Point seed = Point (x, y); Scalar newval = scalar (0, 0, 0); Rect ccomp;int lo = lodiff;int up = Updiff;int flags = 8 + (Newmaskval << 8) + Cv_floodfill_fixed_range; if (event = = Cv_event_lbuttondown) {FloodFill (DST, mask, seed, newval, &ccomp, scalar (lo, lo, lo), scalar (up, up, up), F lags); Imshow ("n", DST);}} int main (int argc, char** argv) {char* filename = ARGV[1];IMAGE0 = imread (filename, 1); if (Image0.empty ()) {cout <&lt ; "Image empty." Usage:ffilldemo <image_name>\n "; return 0;} Image0.copyto (image); Mask.create (Image0.rows, Image0.cols, cv_8uc1); mask = morpho.getedges (image); Cvtcolor (mask, mask, color_bgr2gray);  Myresize (Mask, Mask, image0.rows+2, image0.cols+2); Namedwindow ("image", 0); Createtrackbar ("Lo_diff", "image", &lodiff, 255, 0); Createtrackbar ("Up_diff", "image", &A Mp;updiff, 255, 0); Imshow ("xx", mask); Imshow ("one", image); Setmousecallback ("One", onMouse, 0); Waitkey (0); return 0;}

Code explanation
Many of them are similar to the previous FloodFill using the example code, here only the mask mask related parts. First create a mask image, and then use Morpho.getedges to get to the original image of the edge, and save to the mask image, and then more function Flooffill requirements, the mask image size to the original image length +2.
Mask.create (Image0.rows, Image0.cols, cv_8uc1); mask = morpho.getedges (image); Cvtcolor (mask, mask, color_bgr2gray); Myresize (Mask, Mask, image0.rows+2, image0.cols+2);

Effect Demo
1. Original Image:                      
2. Mask Image:                      
3. Select the generated image after the flowerpot:                      

OpenCV FloodFill (diffuse water filling) and object selection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.