使用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 )
#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. |
---|
相关文章推荐
- overlay
- android Gallery模拟左右滑动的listView
- 如何防止表单重复提交
- systemctl开机启动wpa_supplicant
- iOS经典讲解之Objective-C属性的内存管理原理
- 大锤老湿教您如何配置TP-Link路由器组建wifi上网
- SQL Server 2012基本语句(带实例)
- JavaScript高级程序设计之面向对象的程序设计之理解对象第6.1讲笔记
- 一个折腾的笔记系统
- CodeForces 377A
- 【bzoj3224】Tyvj 1728 普通平衡树
- Opencv Mat的数据读取
- 算法练习:URAL 1495 One-two, One-two 2
- android环境大全
- android多线程详解之Handler
- [读书笔记]九月读书笔记
- CSU1005 Binary Search Tree analog
- 杭电1002(可能是输出格式的问题,尚未查明WA原因)
- Automatically Grab Images From a Website With C#
- 调试ASP.NET2005/2008时,端口不正确的解决三套方案