#include <iostream>
static void help(char** argv)
{
cout << "\n本程序演示了GrabCut图像分割——在一个区域中选择一个对象\n"
"然后GrabCut会尝试将其分割出来。\n"
"调用:\n"
<< argv[0] << " <图像名>\n"
"\n选择您想要分割的对象周围的矩形区域\n" <<
"\n热键:\n"
"\tESC - 退出程序\n"
"\tr - 恢复原始图像\n"
"\tn - 下一次迭代\n"
"\n"
"\t鼠标左键 - 设置矩形\n"
"\n"
"\tCTRL+鼠标左键 - 设置GC_BGD像素\n"
"\tSHIFT+鼠标左键 - 设置GC_FGD像素\n"
"\n"
"\tCTRL+鼠标右键 - 设置GC_PR_BGD像素\n"
"\tSHIFT+鼠标右键 - 设置GC_PR_FGD像素\n" << endl;
}
const int BGD_KEY = EVENT_FLAG_CTRLKEY;
const int FGD_KEY = EVENT_FLAG_SHIFTKEY;
static void getBinMask(
const Mat& comMask,
Mat& binMask )
{
CV_Error( Error::StsBadArg,
"comMask为空或类型不正确(非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 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;
uchar rectState, lblsState, prLblsState;
bool isInitialized;
vector<Point> fgdPxls, bgdPxls, prFgdPxls, prBgdPxls;
int iterCount;
};
void GCApplication::reset()
{
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::setImageAndWinName(
const Mat& _image,
const string& _winName )
{
if( _image.
empty() || _winName.empty() )
return;
image = &_image;
winName = &_winName;
reset();
}
void GCApplication::showImage() const
{
if( image->empty() || winName->empty() )
return;
if( isInitialized ){
getBinMask( mask, binMask);
black.setTo(Scalar::all(255), binMask);
}
vector<Point>::const_iterator it;
for( it = bgdPxls.begin(); it != bgdPxls.end(); ++it )
circle( res, *it, radius, BLUE, thickness );
for( it = fgdPxls.begin(); it != fgdPxls.end(); ++it )
circle( res, *it, radius, RED, thickness );
for( it = prBgdPxls.begin(); it != prBgdPxls.end(); ++it )
circle( res, *it, radius, LIGHTBLUE, thickness );
for( it = prFgdPxls.begin(); it != prFgdPxls.end(); ++it )
circle( res, *it, radius, PINK, thickness );
if( rectState == IN_PROCESS || rectState == SET )
rectangle( res,
Point( rect.x, rect.y ),
Point(rect.x + rect.width, rect.y + rect.height ), GREEN, 2);
}
void GCApplication::setRectInMask()
{
rect.x = max(0, rect.x);
rect.y = max(0, rect.y);
rect.width = min(rect.width, image->cols-rect.x);
rect.height = min(rect.height, image->rows-rect.y);
}
void GCApplication::setLblsInMask(
int flags,
Point p,
bool isPr )
{
vector<Point> *bpxls, *fpxls;
if( !isPr )
{
bpxls = &bgdPxls;
fpxls = &fgdPxls;
}
else
{
bpxls = &prBgdPxls;
fpxls = &prFgdPxls;
}
if( flags & BGD_KEY )
{
bpxls->push_back(p);
circle( mask, p, radius, bvalue, thickness );
}
if( flags & FGD_KEY )
{
fpxls->push_back(p);
circle( mask, p, radius, fvalue, thickness );
}
}
void GCApplication::mouseClick( int event, int x, int y, int flags, void* )
{
switch( event )
{
{
bool isb = (flags & BGD_KEY) != 0,
isf = (flags & FGD_KEY) != 0;
if( rectState == NOT_SET && !isb && !isf )
{
rectState = IN_PROCESS;
rect =
Rect( x, y, 1, 1 );
}
if ( (isb || isf) && rectState == SET )
lblsState = IN_PROCESS;
}
break;
{
bool isb = (flags & BGD_KEY) != 0,
isf = (flags & FGD_KEY) != 0;
if ( (isb || isf) && rectState == SET )
prLblsState = IN_PROCESS;
}
break;
if( rectState == IN_PROCESS )
{
if(rect.x == x || rect.y == y){
rectState = NOT_SET;
}
else{
rectState = SET;
setRectInMask();
CV_Assert( bgdPxls.empty() && fgdPxls.empty() && prBgdPxls.empty() && prFgdPxls.empty() );
}
}
if( lblsState == IN_PROCESS )
{
setLblsInMask(flags,
Point(x,y),
false);
lblsState = SET;
nextIter();
}
else{
if(rectState == SET){
nextIter();
}
}
break;
if( prLblsState == IN_PROCESS )
{
setLblsInMask(flags,
Point(x,y),
true);
prLblsState = SET;
}
if(rectState == SET){
nextIter();
}
break;
if( rectState == IN_PROCESS )
{
CV_Assert( bgdPxls.empty() && fgdPxls.empty() && prBgdPxls.empty() && prFgdPxls.empty() );
}
else if( lblsState == IN_PROCESS )
{
setLblsInMask(flags,
Point(x,y),
false);
}
else if( prLblsState == IN_PROCESS )
{
setLblsInMask(flags,
Point(x,y),
true);
}
break;
}
}
int GCApplication::nextIter()
{
if( isInitialized )
grabCut( *image, mask, rect, bgdModel, fgdModel, 1 );
else
{
if( rectState != SET )
return iterCount;
if( lblsState == SET || prLblsState == SET )
grabCut( *image, mask, rect, bgdModel, fgdModel, 1, GC_INIT_WITH_MASK );
else
grabCut( *image, mask, rect, bgdModel, fgdModel, 1, GC_INIT_WITH_RECT );
isInitialized = true;
}
iterCount++;
bgdPxls.clear(); fgdPxls.clear();
prBgdPxls.clear(); prFgdPxls.clear();
return iterCount;
}
GCApplication gcapp;
static void on_mouse( int event, int x, int y, int flags, void* param )
{
gcapp.mouseClick( event, x, y, flags, param );
}
int main(
int argc,
char** argv )
{
help(argv);
string filename = parser.get<string>("@input");
if( filename.empty() )
{
cout << "\n糟糕,文件名为空" << endl;
return 1;
}
Mat image =
imread(samples::findFile(filename), IMREAD_COLOR);
{
cout << "\n 糟糕,无法读取图像文件:" << filename << endl;
return 1;
}
const string winName = "image";
gcapp.setImageAndWinName( image, winName );
gcapp.showImage();
for(;;)
{
switch( c )
{
case '\x1b'
cout << "正在退出..." << endl;
goto exit_main;
case 'r'
cout << endl;
gcapp.reset();
gcapp.showImage();
break;
case 'n'
int iterCount = gcapp.getIterCount();
cout << "<" << iterCount << "... ";
int newIterCount = gcapp.nextIter();
if( newIterCount > iterCount )
{
gcapp.showImage();
cout << iterCount << ">" << endl;
}
else
cout << "必须确定矩形>" << endl;
break;
}
}
exit_main
return 0;
}
如果数组没有元素,则返回 true。
int64_t int64
MatSize size
定义 mat.hpp:2187
void copyTo(OutputArray m) const
将矩阵复制到另一个矩阵。
void create(int rows, int cols, int type)
如果需要,分配新的数组数据。
cv::getTickFrequency
double getTickFrequency()
int rows
矩阵的行数和列数,或当矩阵维度超过2时为 (-1, -1)
定义 mat.hpp:2165
int type() const
返回矩阵元素的类型。
2D 矩形的模板类。
定义 types.hpp:444
void addWeighted(InputArray src1, double alpha, InputArray src2, double beta, double gamma, OutputArray dst, int dtype=-1)
计算两个数组的加权和。
#define CV_8UC1
定义 interface.h:88
#define CV_Error(code, msg)
调用错误处理程序。
定义 base.hpp:399
#define CV_Assert(expr)
在运行时检查条件,如果失败则抛出异常。
定义 base.hpp:423
@ circle
定义 gr_skig.hpp:62
GMat mask(const GMat &src, const GMat &mask)
将掩码应用于矩阵。
@ EVENT_LBUTTONUP
指示鼠标左键已释放。
定义 highgui.hpp:171
@ EVENT_MOUSEMOVE
指示鼠标指针已在窗口上移动。
定义 highgui.hpp:167
@ EVENT_RBUTTONDOWN
指示鼠标右键已按下。
定义 highgui.hpp:169
@ EVENT_RBUTTONUP
指示鼠标右键已释放。
定义 highgui.hpp:172
@ EVENT_LBUTTONDOWN
指示鼠标左键已按下。
定义 highgui.hpp:168
void imshow(const String &winname, InputArray mat)
在指定窗口中显示图像。
int waitKey(int delay=0)
等待按键按下。
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
创建窗口。
void destroyWindow(const String &winname)
销毁指定窗口。
void setMouseCallback(const String &winname, MouseCallback onMouse, void *userdata=0)
为指定窗口设置鼠标处理程序。
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR_BGR)
从文件加载图像。
void rectangle(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
绘制一个简单、粗或填充的矩形。
@ GC_FGD
明显的(对象)前景像素
定义 imgproc.hpp:353
@ GC_BGD
明显的背景像素
定义 imgproc.hpp:352
@ GC_PR_FGD
可能的(对象)前景像素
定义 imgproc.hpp:355
@ GC_PR_BGD
可能的背景像素
定义 imgproc.hpp:354
void grabCut(InputArray img, InputOutputArray mask, Rect rect, InputOutputArray bgdModel, InputOutputArray fgdModel, int iterCount, int mode=GC_EVAL)
运行 GrabCut 算法。
int main(int argc, char *argv[])
定义 highgui_qt.cpp:3
void showImage(cv::InputArray img, const CallMetaData &data, const char *description, const char *view)