grabcut mask

时间:2024-07-06 13:34:20
#include "opencv2/opencv.hpp"
using namespace cv; void main()
{
Mat src = imread("E:\\智能检测与图像处理\\3.jpg");
//Rect rect(84, 84, 406, 318);
Rect rect;
Mat bgModel, fgModel;
Mat result(src.size(), CV_8U, Scalar());
Mat ROI(result(Rect(, , , )));
ROI.setTo(GC_PR_FGD);//ROI设置为可能是前景 grabCut(src, result, rect, bgModel, fgModel, , GC_INIT_WITH_MASK);
//threshold(result, result, 2, 255, CV_THRESH_BINARY);
imshow("grab", result);
compare(result, GC_PR_FGD, result, CMP_EQ);
//result = result&1;
imshow("result", result);
Mat foreground(src.size(), CV_8UC3, Scalar(, , ));
src.copyTo(foreground, result);//意思是可以得到一个附加掩膜result的矩阵foreground

imshow("foreground", foreground); waitKey(); }