如何優雅的在MFC中使用cvSetMouseCallback?

MFC與opencv的cvSetMouseCallback用起來感受很不兼容。app

大部分時候,用cvSetMouseCallback也許只是爲了獲取一個矩形框,或者繪製一個點,或者其它什麼簡易的圖形,經過調用該函數來獲得鼠標交互的參數信息。框架

然而,這麼一個簡單的要求,在MFC框架中並非很方便調用。函數

經過閱讀 opencv 官方提供的samples源碼,其中在grabcut上面有個GCApplication類,用來控制或者獲取繪製信息,我以爲很方便,將其精簡一下,以下:code

#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 );
}

而後,在須要的地方,加上以下的代碼,即完成了對鼠標繪製信息的獲取,在這裏,是針對矩形繪製的獲取:string

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;
相關文章
相關標籤/搜索