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可提高准确度。
**********参数说明****************
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可提高准确度。
相关文章推荐
- python中使用OpenCV进行人脸检测的例子
- opencv 做人脸识别 opencv 人脸匹配分析
- 使用opencv拉伸图像扩大分辨率示例
- OpenCV 2.4.3 C++ 平滑处理分析
- 利用Python和OpenCV库将URL转换为OpenCV格式的方法
- python结合opencv实现人脸检测与跟踪
- 在树莓派2或树莓派B+上安装Python和OpenCV的教程
- OpenCV配置,从来没有这么简单!
- ubuntu下opencv和qt的安装配置
- OpenCV学习笔记(二十五)——OpenCV图形界面设计Qt+VS2008
- 分享一些OpenCV实现立体视觉的经验
- 关于OpenCv图像变换与基本图形检测
- "应用程序正常初始化失败"-0xc0150002 解决办法
- OpenCV->HSV色彩空间
- opencv 内存泄露
- OpenCV函数cvFindContours
- OpenCV 2.3.1图像文件的读入和显示
- opencv2 矩阵方式 resize图像缩放代码
- OpenCV 灰度直方图
- 彩色图转为灰度图