MFC與opencv的cvSetMouseCallback用起來感覺很不兼容。
大部分時候,用cvSetMouseCallback也許只是為了獲取一個矩形框,或者繪制一個點,或者其它什么簡易的圖形,通過調用該函數來得到鼠標交互的參數信息。
然而,這么一個簡單的要求,在MFC框架中並不是很方便調用。
通過閱讀 opencv 官方提供的samples源碼,其中在grabcut上面有個GCApplication類,用來控制或者獲取繪制信息,我覺得很方便,將其精簡一下,如下:
#define GC_BGD 0
#define GC_FGD 1
#define GC_PR_BGD 2
#define GC_PR_FGD 3
const Scalar GREEN = Scalar(0,255,0);
class GCApp
{
public:
enum{ NOT_SET = 0, IN_PROCESS = 1, SET = 2 };
public:
void mouseClick( int event, int x, int y, int flags, void* param );
void setImageAndWinName( const Mat& _image, const string& _winName);
void reset();
void showImage() const;
public:
Rect rect;
uchar rectState;
const Mat* image;
Mat mask;
const string* winName;
};
void GCApp::reset()
{
if( !mask.empty() )
mask.setTo(Scalar::all(GC_BGD));
rectState = NOT_SET;
}
void GCApp::setImageAndWinName( const Mat& _image, const string& _winName )
{
if( _image.empty() || _winName.empty() )
return;
image = &_image;
winName = &_winName;
mask.create( image->size(), CV_8UC1);
reset();
}
void GCApp::showImage() const
{
if( image->empty() || winName->empty() )
return;
Mat res;
image->copyTo( res );
if( rectState == IN_PROCESS || rectState == SET )
rectangle( res, Point( rect.x, rect.y ), Point(rect.x + rect.width, rect.y + rect.height ), GREEN, 2);
imshow( *winName, res );
}
void GCApp::mouseClick( int event, int x, int y, int flags, void* )
{
switch( event )
{
case CV_EVENT_LBUTTONDOWN: // set rect or GC_BGD(GC_FGD) labels
{
if( rectState == NOT_SET )
{
rectState = IN_PROCESS;
rect = Rect( x, y, 1, 1 );
}
showImage();
}
break;
case CV_EVENT_MOUSEMOVE:
if( rectState == IN_PROCESS )
{
rect = Rect( Point(rect.x, rect.y), Point(x,y) );
showImage();
}
break;
case CV_EVENT_LBUTTONUP:
if( rectState == IN_PROCESS )
{
rect = Rect( Point(rect.x, rect.y), Point(x,y) );
rectState = SET;
showImage();
}
break;
}
}
由於cvSetMouseCallback需要回調on_mouse函數,聲明一個on_mouse函數,注意,得是 static void
GCApp gcapp;
static void on_mouse( int event, int x, int y, int flags, void* param )
{
gcapp.mouseClick( event, x, y, flags, param );
}
然后,在需要的地方,加上如下的代碼,即完成了對鼠標繪制信息的獲取,在這里,是針對矩形繪制的獲取:
const string winName = "DrawRegion";
cvNamedWindow( winName.c_str(), CV_WINDOW_AUTOSIZE );
cvSetMouseCallback( winName.c_str(), on_mouse, 0 );
gcapp.setImageAndWinName( img, winName );
gcapp.showImage();
for (;;)
{
int c = cvWaitKey(0);
////// ESC 鍵 結束
if (char(c) == '\x1b' && gcapp.rectState == gcapp.SET)
{
break;
}
}
Rect rect = gcapp.rect;