Ask Your Question

Values returned by MouseCallBack?

asked 2014-11-07 11:51:13 -0500

HandsomeJoe gravatar image

How do I get values defined within my MouseCallBack function to be accessible to separate Functions, Objects & Modules that are part of larger application? e.g. I tried putting my MouseCallback function within an upper level function and tried to use Return to get the values I wanted. I also tried putting it within a Class. Both scenarios had Global variables defined for the values I needed but I am not able to get the values I need to pass later on to other functions or objects in the larger application.

edit retag flag offensive close merge delete


I should have shared that I am using openCV in Python.

HandsomeJoe gravatar imageHandsomeJoe ( 2014-11-07 18:45:37 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2014-11-07 14:29:42 -0500

juanmanpr gravatar image

Hello, I am not sure I understand well your problem, but I will give you an example on how I deal with this:

void Manager::init(){
  cv::setMouseCallback( "SELECT BOX", onMouse, &tinfo1 );    

void Manager::onMouse( int event, int x, int y, int , void* p ){
    trackerinfo* info = (trackerinfo*) p;
    switch ( event ){
    //set origin of the bounding box
    info->startSelection = true;
    info->bbox.x = x;
    info->bbox.y = y;
        ... /*other events...*/

So, as you can see, you can use information that is modified inside the "onMouse" event method by passing a pointer to your data (In the example a "trackerinfo" object). As the callback expects a (void*) you have to apply proper casting for your own data types. You can also see that there is no inconvenient on using a class method as callback. Is this what you are looking for?

edit flag offensive delete link more


Hello and thank you for your reply. Unfortunately I am using openCV in Python as I don't know C.

HandsomeJoe gravatar imageHandsomeJoe ( 2014-11-07 18:44:36 -0500 )edit
Login/Signup to Answer

Question Tools


Asked: 2014-11-07 11:51:13 -0500

Seen: 62 times

Last updated: Nov 07 '14