0

I have a problem with OpenCV. I have tried the implementation from in other questions but problem remains again. I have a class called MainWindow and at some point in that class it creates a NamedWindow called "rectified". In my main function Im trying to add a listener to catch click events on that cvNamedWindow instance. The problem is that on_mouse function has no response. Here is my main method:

static void on_mouse( int event, int x, int y, int flags, void *param)
{
     qDebug("hjhv00");
     MainWindow * mw=(MainWindow*)param;
     if(mw->getMyVision()->getCalibrationDone())
     {
        if(event==CV_EVENT_LBUTTONDOWN){
           qDebug("%d<---->%d",x,y);
        }
     }
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    int mouseParam=5;

    w.show();
    cvSetMouseCallback("rectified",on_mouse,&w);

    return a.exec();
    // test svn
    // test from google
}
karlphillip
  • 92,053
  • 36
  • 243
  • 426
dramaticlook
  • 653
  • 1
  • 12
  • 39
  • Check: [Draw on webcam using OpenCV](http://stackoverflow.com/questions/5490655/draw-on-webcam-using-opencv/5493633#5493633) – karlphillip Jan 31 '13 at 18:28
  • Check: [free form image selection (preferably in c++)](http://stackoverflow.com/a/8196180/176769) – karlphillip Jan 31 '13 at 18:28
  • Check: [Setting ROI with mouse from a rectangle on a video](http://stackoverflow.com/questions/10881397/setting-roi-with-mouse-from-a-rectangle-on-a-video/10883266#10883266) – karlphillip Jan 31 '13 at 18:29

1 Answers1

1

Using as reference my previous posts on this subject:

I was able to assemble a quick C++/Qt/OpenCV demo that creates an OpenCV window and writes a message on the console every time the left mouse button is pressed.

I believe the code is self-explanatory:

main.cpp:

#include <cv.h>
#include <highgui.h>

#include <iostream>

#include <QtWidgets/QApplication>


void on_mouse(int event, int x, int y, int flags, void* param)
{
    if (event == CV_EVENT_LBUTTONDOWN)
    {
        std::cout << "@ Left mouse button pressed at: " << x << "," << y << std::endl;
    }
}


int main(int argc, char* argv[])
{
    QApplication app(argc, argv);

    IplImage* img = cvLoadImage("/Users/karlphillip/workspace/opencv/qt_mouse/LeafGlyph.jpg");
    if (!img)
    {
        std::cout << "!!! Failed to load image" << std::endl;
        return -1;
    }

    cvNamedWindow("result", CV_WINDOW_AUTOSIZE);

    cvSetMouseCallback("result",&on_mouse, 0);

    cvShowImage("result", img);
    cvWaitKey(0);

    return app.exec();
}

project.pro (used on Mac OS X):

TEMPLATE = app

QT      += widgets

## OpenCV settings for Mac OS X
macx {
    INCLUDEPATH += /usr/local/include/opencv

    LIBS += -L/usr/local/lib/ \
        -lopencv_core \
        -lopencv_highgui \
        -lopencv_imgproc
}

SOURCES += \
    main.cpp 

Notes about your implementation:

I suggest you move the call to cvSetMouseCallback() to wherever you are calling cvNamedWindow(). I suspect that the right place to do it is in the constructor of MainWindow which should create the window, right?! Then you would also have to define on_mouse() as a static member of MainWindow and implement it.

If you do that your code would be similar to:

void MainWindow::on_mouse(int event, int x, int y, int flags, void* param)
{
    if (event == CV_EVENT_LBUTTONDOWN)
    {
        std::cout << "@ Left mouse button pressed at: " << x << "," << y << std::endl;
    }
}

MainWindow::MainWindow()
{
    IplImage* img = cvLoadImage("/Users/karlphillip/workspace/opencv/qt_mouse/LeafGlyph.jpg");
    if (!img)
    {
        std::cout << "!!! Failed to load image" << std::endl;
        return;
    }

    cvNamedWindow("result", CV_WINDOW_AUTOSIZE);

    cvSetMouseCallback("result",&on_mouse, 0);

    cvShowImage("result", img);
    cvWaitKey(0);
}
Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426