OpenCV中的工作示例:
//Fill with the background value
Mat mask = cv::Mat::ones(src.size(), CV_8U) * cv::GC_BGD;
//Fill a smaller rectangle with the probably-foreground value.
Rect area;
area.x=10; area.y=10;
area.width=250; area.height=250;
rectangle(mask, area , cv::Scalar(cv::GC_PR_FGD),-1,8,0);
//Fill a smaller rectangle with the foreground value.
area.x=50; area.y=50;
area.width=20; area.height=20;
rectangle(mask, area , cv::Scalar(cv::GC_FGD),-1,8,0);
Mat bgd,fgd;
cv::grabCut(src, mask, area, bgd, fgd, 1, cv::GC_INIT_WITH_MASK);
//Visualize results.
compare(mask,cv::GC_FGD,mask,cv::CMP_EQ);
Mat foreground(src.size(),CV_8UC3, cv::Scalar(0,0,0));
src.copyTo(foreground,mask);
imshow("segm",foreground);