//4.使用GrabCut算法提取前景物体---------------------------------
cv::Mat imagecow = cv::imread("../../aTestImage/cow.jpg", 1); //cv::IMREAD_GRAYSCALE | 0
cv::Rect rectangle(10,100,380,180);
cv::Mat result;
cv::Mat bgModel, fgModel;
//GrabCut分割
//(src,结果,包含前景物体的矩形,内部使用模型bg,内部使用模型fg,迭代次数,使用矩形进行初始化)
cv::grabCut(imagecow, result, rectangle, bgModel, fgModel, 5, cv::GC_INIT_WITH_RECT);//提取矩形目标物
cv::compare(result, cv::GC_PR_FGD, result, cv::CMP_EQ);//得到可能为前景的像素
cv::Mat foreground(imagecow.size(), CV_8UC3, cv::Scalar(255, 255, 255));//定义输出图像 前景白
//result = result &1;
imagecow.copyTo(foreground, result);
cv::namedWindow("foreground", 1);
cv::imshow("foreground", foreground);
OpenCV_(Using GrabCut extract the foreground object) 使用 GrabCut 算法提取前景物体
关注
打赏