您的位置:首页 > 运维架构

opencv使用GrabCut算法提取前景物体

2015-10-13 09:48 281 查看
void grabCut(InputArray image, InputOutputArray mask, Rect rect, InputOutputArray bgdModel, InputOutputArray fgdModel, int iterCount, int mode)

**********参数说明****************

image –输入8位3通道图像

mask
               输入\输出的8位单通道mask。当mode设为GC_INIT_WITH_RECT时,由函数初始化,  当mode设为GC_INIT_WITH_MASK,允许用户输入自己标记的掩码。
mask的取值为以下四种:
GC_BGD (=0)defines an obvious background pixels.
GC_FGD(=1) defines an obvious foreground (object) pixel.
GC_PR_BGD(=2) defines a possible background pixel.
GC_PR_BGD (=3)defines a possible foreground pixel.

rect – 包含前景的矩形

bgdModel – 背景模型. Do not modify it while you are processing the same image.
fgdModel – 前景模型. Do not modify it while you are processing the same image.
iterCount – 迭代次数
mode –操作代码,可能为以下值:

GC_INIT_WITH_RECT -----矩形框初始化函数;
GC_INIT_WITH_MASK ------mask初始化函数;
GC_EVAL ---执行分割。

简单的例子:

#include"highgui.h"
#include"cv.h"
using namespace cv;

Rect selection;
Mat img,img0;
Point prePt(-1,-1);
void onMouse(int event,int x,int y,int flags,void* param){
if (event==CV_EVENT_LBUTTONDOWN){
prePt = Point(x, y);
}
else if (event == CV_EVENT_MOUSEMOVE && (flags && CV_EVENT_FLAG_LBUTTON)){
img = img0.clone();
rectangle(img, Rect(prePt.x, prePt.y, abs(x - prePt.x), abs(y - prePt.y)), Scalar(0,0,255),3);
selection = Rect(prePt.x, prePt.y, abs(x - prePt.x), abs(y - prePt.y));

}
else if (event == CV_EVENT_LBUTTONUP || !(flags & CV_EVENT_FLAG_LBUTTON)){

prePt=Point(-1, -1);
}
imshow("原图", img);
}

int main(){
img = imread("D:\\nvshen.jpg",1);
img0 = img.clone();
namedWindow("原图",1);
imshow("原图", img);
setMouseCallback("原图", onMouse);
Mat result; // 4种可能结果
Mat bgModel, fgModel; // 背景/前景
for (;;){
int c = waitKey(10);
if ((char)c == 'p'){
grabCut(img0, result, selection, bgModel, fgModel, 5, GC_INIT_WITH_RECT);
compare(result,GC_PR_FGD,result,CMP_EQ);//得到前景mask
Mat foreground(img.size(),CV_8UC3,Scalar::all(255));
img0.copyTo(foreground,result);
imshow("grabcut",foreground);
}
if (char(c) == 'q') return 0;

}
return 0;
}

笔记:
compare(result,GC_PR_FGD,result,CMP_EQ);本例的结果标记像素包含可能属于前景的像素(GC_PR_FGD)和可能属于背景的像素(GC_PR_BGD),矩形之外的为确定属于背景的像素(GC_BGD),不包含GC_FGD。
通过result与可能属于前景的像素作比较,得到前景mask即可提取前景。

另一种方法是 result&=1;替代上句。

四种可能的结果值分别为0,1,2,3.即00000000,00000001,00000010,00000011.因为结果不包含1,所以核对第一位即可。

本例只是大致提取了前景,通过输入带标记的mask可提高准确度。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  opencv