使用GrabCut算法提取前景物体

例子一

#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:
  • img – Input 8-bit 3-channel image.
  • mask –

    Input/output 8-bit single-channel mask. The mask is initialized by the function when mode is set to GC_INIT_WITH_RECT. Its elements may have one of following values:

    • GC_BGD defines an obvious background pixels.
    • GC_FGD defines an obvious foreground (object) pixel.
    • GC_PR_BGD defines a possible background pixel.
    • GC_PR_FGD defines a possible foreground pixel.
  • rect – ROI containing a segmented object. The pixels outside of the ROI are marked as “obvious background”. The parameter is only used when mode==GC_INIT_WITH_RECT .
  • bgdModel – Temporary array for the background model. Do not modify it while you are processing the same image.
  • fgdModel – Temporary arrays for the foreground model. Do not modify it while you are processing the same image.
  • iterCount – Number of iterations the algorithm should make before returning the result. Note that the result can be refined with further calls with mode==GC_INIT_WITH_MASK or mode==GC_EVAL .
  • mode –

    Operation mode that could be one of the following:

    • GC_INIT_WITH_RECT The function initializes the state and the mask using the provided rectangle. After that it runs iterCount iterations of the algorithm.
    • GC_INIT_WITH_MASK The function initializes the state using the provided mask. Note thatGC_INIT_WITH_RECT and GC_INIT_WITH_MASK can be combined. Then, all the pixels outside of the ROI are automatically initialized with GC_BGD .
    • GC_EVAL The value means that the algorithm should just resume.

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值