您的位置:首页 > 其它

使用GrabCut算法提取前景物体

2015-09-27 12:04 225 查看
例子一

#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.

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: