OpenCV Learning (grabcut) segmentation algorithm

Source: Internet
Author: User
Tags scalar

Http://www.cnblogs.com/mikewolf2002/p/3330390.html

OpenCV Learning (grabcut) segmentation algorithm

In the OpenCV, the Grabcut segmentation algorithm is realized, the algorithm can easily divide the foreground image, the operation is simple, and the effect of segmentation is very good. The principle of the algorithm is described in Papaer: "Grabcut"-interactive Foreground Extraction using iterated Graph Cuts

For example, the following diagram, we just select a quadrilateral box, the image in the box as an input parameter of Grabcut, indicating that the pixel in the box may belong to the foreground, but the outside of the box must belong to the background .

Then call the Grabcut function and you can split the castle. The specific code is as follows:

Open another image
Cv::mat image= Cv::imread (".. /tower.jpg ");
if (!image.data)
{
cout<< "Can't open the image! "<<endl;

}


Cv::rect rectangle (50,70,image.cols-150,image.rows-180);

Cv::mat result;
Two temporary matrix variables, used as intermediate variables of the algorithm, without care

Double TT = Cv::gettickcount ();
Grabcut segment
Cv::grabcut (image, //input images
result, //segmented results

Bgmodel,fgmodel,//foreground, background
1, //Iteration Count
Cv::gc_init_with_rect); With a rectangle
tt = Cv::gettickcount ()-TT;
printf ("Algorithm execution time:%g ms\n", tt/cv::gettickfrequency () *1000);
Get the pixels that might be the foreground
The comparison function retains a pixel with a value of GC_PR_FGD
Cv::compare (RESULT,CV::GC_PR_FGD,RESULT,CV::CMP_EQ);
Generate output image
Cv::mat foreground (Image.size (), Cv_8uc3,cv::scalar (255,255,255));
The background value is gc_bgd=0, as a mask

The first parameter of the Grabcut function is the image we want to process, the image must be of the type: CV_8UC3

The second parameter is the mask image, which is the same size as the image, but its format is CV_8UC1 and can only be single-channel, and the result of the grabcut algorithm is saved in the image.

In the preceding code, we did not initialize the mask image (result) because the 6th parameter is Cv::gc_init_with_rect, which indicates that the algorithm generates an initialized mask image based on the range of rectangle.

Cv::grabcut (image, //input images
result, //segmented results
Rectangle, //rectangle with foreground
Bgmodel,fgmodel, //foreground, background
1, //Iteration Count
Cv::gc_init_with_rect); //With rectangular

The value of the mask image can only be the following 4 values (pr,probably):

GC_BGD = 0,//background

GC_FGD = 1,//foreground
GC_PR_BGD = 2,//possible background

GC_PR_FGD = 3//possible foreground

The mask image rule generated according to rectangle is: The outer part of the quadrilateral must be the background, so the corresponding pixel value in the mask is GC_BGD, and the value inside the quadrilateral may be the foreground, so the corresponding pixel value is GC_PR_FGD. So we should use the mask image as shown in our program.


If the 7th parameter is Gc_init_with_mask, then the third parameter rectangle is not used, we must manually set the MASK image (the variable result) before calling the Grabcut function, if we set the result to the grayscale image shown. The Calling function

Cv::grabcut (Image1, //input image
RESULT1, //segmented results
Rectangle, //rectangle with foreground
Bgmodel,fgmodel, //foreground, background
1, //Iteration Count
Cv::gc_init_with_mask); //With rectangular

can get the same result.
You can refer to the following code:

Cv::mat result1= Cv::mat (image1.rows, IMAGE1.COLS,CV_8UC1, Cv::scalar (CV::GC_BGD));
Notice the method of assigning a value to a sub-matrix
Cv::mat ROI (RESULT1, Cv::rect (50,70,result1.cols-150,result.rows-180));
ROI = Cv::scalar (CV::GC_PR_FGD);
tt = Cv::gettickcount ();
Grabcut segment
Cv::grabcut (Image1, //input image
RESULT1, //segmented results

Bgmodel,fgmodel,//foreground, background
1, //Iteration Count
Cv::gc_init_with_mask); With a rectangle
tt = Cv::gettickcount ()-TT;
printf ("Algorithm execution time:%g ms\n", tt/cv::gettickfrequency () *1000);

Get the pixels that might be the foreground
The comparison function retains a pixel with a value of GC_PR_FGD
Cv::compare (RESULT1,CV::GC_PR_FGD,RESULT,CV::CMP_EQ);
Generate output image
Cv::mat Foreground1 (Image1.size (), Cv_8uc3,cv::scalar (255,255,255));
The background value is gc_bgd=0, as a mask

The 3rd parameter is the size position of the rectangle, and if the 7th argument is gc_init_with_mask, the parameter has no effect.

4th, 5 parameters are two algorithms that use temporal matrix variables during execution without care for their contents.

The 6th parameter is the number of iterations, the more iterations, the better the effect, but the longer the stroke time.

The 7th parameter is the operating mode, typically gc_init_with_rect and gc_init_with_mask.

From the above figure, we can see that the grabcut algorithm works very well, but it takes a long time and the image on it takes 4.4 seconds on my notebook.

Program source code: Engineering FirstOpenCV13

OpenCV Learning (grabcut) segmentation algorithm

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.