OpenCV_(Using GrabCut extract the foreground object) 使用 GrabCut 算法提取前景物体

来源:互联网 发布:2009年网络歌曲排行榜 编辑:程序博客网 时间:2024/06/03 15:54
//4.使用GrabCut算法提取前景物体---------------------------------cv::Mat imagecow = cv::imread("../../aTestImage/cow.jpg", 1); //cv::IMREAD_GRAYSCALE | 0cv::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);

0 0