#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(0)); Mat ROI(result(Rect(84, 84, 406, 700))); ROI.setTo(GC_PR_FGD);//ROI设置为可能是前景 grabCut(src, result, rect, bgModel, fgModel, 1, 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(255, 255, 255)); src.copyTo(foreground, result);//意思是可以得到一个附加掩膜result的矩阵foreground
imshow("foreground", foreground); waitKey(0); }