资源简介
用于图像分割之基于图论的图像分割,实现图像的前景和背景的分离,具有较高的效率和可行性。
代码片段和文件信息
// WebProjectImage.cpp : 定义控制台应用程序的入口点。
//
#include “stdafx.h“
#include “opencv2/highgui/highgui.hpp“
#include “opencv2/imgproc/imgproc.hpp“
#include
#include
#include “math.h“
#include “highgui.h“
#include
using namespace std;
using namespace cv;
void help()
{
cout << “\nThis program demonstrates GrabCut segmentation -- select an object in a region\n“
“and then grabcut will attempt to segment it out.\n“
“Call:\n“
“./grabcut \n“
“\nSelect a rectangular area around the object you want to segment\n“ <<
“\nHot keys: \n“
“\tESC - quit the program\n“
“\tr - restore the original image\n“
“\tn - next iteration\n“
“\n“
“\tleft mouse button - set rectangle\n“
“\n“
“\tCTRL+left mouse button - set GC_BGD pixels\n“
“\tSHIFT+left mouse button - set CG_FGD pixels\n“
“\n“
“\tCTRL+right mouse button - set GC_PR_BGD pixels\n“
“\tSHIFT+right mouse button - set CG_PR_FGD pixels\n“ << endl;
}
const Scalar RED = Scalar(00255);
const Scalar PINK = Scalar(230130255);
const Scalar BLUE = Scalar(25500);
const Scalar LIGHTBLUE = Scalar(255255160);
const Scalar GREEN = Scalar(02550);
const int BGD_KEY = CV_EVENT_FLAG_CTRLKEY;
const int FGD_KEY = CV_EVENT_FLAG_SHIFTKEY;
//把comMask赋给binMak
void getBinMask( const Mat& comMask Mat& binMask )
{
if( comMask.empty() || comMask.type()!=CV_8UC1 )
CV_Error( CV_StsBadArg “comMask is empty or has incorrect type (not CV_8UC1)“ );
if( binMask.empty() || binMask.rows!=comMask.rows || binMask.cols!=comMask.cols )
//设置Mat大小★★★★
binMask.create( comMask.size() CV_8UC1 );
binMask = comMask & 1;//★★★★很重要
}
class GCApplication
{
public:
enum{ NOT_SET = 0 IN_PROCESS = 1 SET = 2 };
static const int radius = 2;
static const int thickness = -1;
void reset();
void setImageAndWinName( const Mat& _image const string& _winName );
void showImage() const;
void mouseClick( int event int x int y int flags void* param );
int nextIter();
int getIterCount() const { return iterCount; }
private:
void setRectInMask();
void setLblsInMask( int flags Point p bool isPr );
const string* winName;
const Mat* image;
Mat mask;
Mat bgdModel fgdModel;
uchar rectState lblsState prLblsState;
bool isInitialized;
Rect rect;
vector fgdPxls bgdPxls prFgdPxls prBgdPxls;
int iterCount;
};
void GCApplication::reset()
{
if( !mask.empty() )
//mask设定为GC_BGD,GC_FGD,GC_PR_BGD中的一种;
mask.setTo(Scalar::all(GC_BGD));
bgdPxls.clear(); fgdPxls.clear();
prBgdPxls.clear(); prFgdPxls.clear();
isInitialized = false;
rectState = NOT_SET;
lblsState = NOT_SET;
prLblsState = NOT_SET;
iterCount = 0;
}
void GCApplication::setImageAndWinNa
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 10306 2013-10-10 20:40 WebProjectImage.cpp
文件 21730 2013-09-29 13:51 图像分割之(一)概述.docx
文件 210190 2013-09-29 21:17 图像分割之(三)从Graph Cut到Grab Cut.docx
文件 176327 2013-09-29 20:35 图像分割之(二)Graph Cut(图割).docx
文件 57335 2013-09-29 13:56 图像分割之(四)OpenCV的GrabCut函数使用和源码解读.docx
- 上一篇:zabbix IIS监控模板
- 下一篇:基于FPGA的verilog红外遥控设计代码
评论
共有 条评论