例子一
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
int main()
{
Mat srcImage=imread("E:\\图片\\images\\tower.jpg");
Rect rectangle1(50,70,srcImage.cols-150,srcImage.rows-180);
Mat result;
Mat bgModel,fgModel;
grabCut(srcImage,result,rectangle1,bgModel,fgModel,1,GC_INIT_WITH_RECT);
compare(result,GC_PR_FGD,result,CMP_EQ);
Mat foreground(srcImage.size(),CV_8UC3,Scalar(255,255,255));
srcImage.copyTo(foreground,result);
cv::rectangle(srcImage,rectangle1,Scalar(255,255,255));
namedWindow("srcImage");
imshow("srcImage",srcImage);
namedWindow("segmented Image");
imshow("segmented Image",foreground);
waitKey(0);
return 0;
}
例子二
#include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/highgui/highgui.hpp> using namespace cv; using namespace std; int main() { Mat srcImage=imread("E:\\图片\\images\\group.jpg"); if(!srcImage.data) cout<<"图片读取失败"<<endl; Rect rectangle2(10,100,380,180); Mat result; Mat bgModel,fgModel; grabCut(srcImage,result,rectangle2,bgModel,fgModel,5,GC_INIT_WITH_RECT); compare(result,GC_PR_FGD,result,CMP_EQ); result=result&1; Mat foreground; foreground.create(srcImage.size(),CV_8UC3); foreground.setTo(Scalar(255,255,255)); srcImage.copyTo(foreground,result); cv::rectangle(srcImage,rectangle2,Scalar(255,255,255),1); namedWindow("srcImage"); imshow("srcImage",srcImage); namedWindow("Foreground Objects"); imshow("Foreground Objects",foreground); waitKey(0); return 0; }
C++: void grabCut(InputArray img, InputOutputArray mask, Rect rect, InputOutputArray bgdModel, InputOutputArrayfgdModel, int iterCount, int mode=GC_EVAL )
Parameters: |
|
---|