Detect Mouse Clicks and Moves on Image Window

void setMouseCallback(const string& winname, MouseCallback onMouse, void* userdata = 0)

This function sets a callback function to be called every time any mouse events occurs in the specified window. Here is the detailed explanation of the each parameters of the above OpenCV function.
    • winname - Name of the OpenCV window. All mouse events related to this window will be registered
    • onMouse - Name of the callback function. Whenever mouse events related to the above window occur, this callback function will be called. This function should have the signature like the following
      • void FunctionName(int event, int x, int y, int flags, void* userdata)
        • event - Type of the mouse event. These are the entire list of mouse events
          • EVENT_MOUSEMOVE
          • EVENT_LBUTTONDOWN
          • EVENT_RBUTTONDOWN
          • EVENT_MBUTTONDOWN
          • EVENT_LBUTTONUP
          • EVENT_RBUTTONUP
          • EVENT_MBUTTONUP
          • EVENT_LBUTTONDBLCLK
          • EVENT_RBUTTONDBLCLK
          • EVENT_MBUTTONDBLCLK
        • x - x coordinate of the mouse event
        • y - y coordinate of the mouse event
        • flags - Specific condition whenever a mouse event occurs. See the next OpenCV example code for the usage of this parameter. Here is the entire list of enum values which will be possesed by "flags"
          • EVENT_FLAG_LBUTTON
          • EVENT_FLAG_RBUTTON
          • EVENT_FLAG_MBUTTON
          • EVENT_FLAG_CTRLKEY
          • EVENT_FLAG_SHIFTKEY
          • EVENT_FLAG_ALTKEY
        • userdata - Any pointer passes to the "setMouseCallback" function as the 3rd parameter (see below)
    • userdata - This pointer will be passed to the callback function
This program takes care of mouse button press and mouse movements.

This may be helpful while you want to select a region on an image and perform certain task in that region only.

Example:

-----------
#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace std;
using namespace cv;

void CallBackFunc(int event, int x, int y, int flags, void* userdata)
{
    if  ( event == EVENT_LBUTTONDOWN )
    {
        cout << "Left button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
    }
    else if  ( event == EVENT_RBUTTONDOWN )
    {
        cout << "Right button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
    }
    else if  ( event == EVENT_MBUTTONDOWN )
    {
        cout << "Middle button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
    }
    else if ( event == EVENT_MOUSEMOVE )
    {
        cout << "Mouse move over the window - position (" << x << ", " << y << ")" << endl;
    }    
}

int main()
{
    // Read image from file 
    Mat img = imread("lena.JPG");

    //if fail to read the image
    if ( img.empty() ) 
    { 
        cout << "Error loading the image" << endl;
        return -1; 
    }

    //Create a window
    namedWindow("ImageDisplay", 1);

    //set the callback function for any mouse event
    setMouseCallback("ImageDisplay", CallBackFunc, NULL);

    //show the image
    imshow("ImageDisplay", img);

    // Wait until user press some key
    waitKey(0);

    return 0;

}
-----------

Example 2:

This code will detect left mouse clicks while pressing the "CTRL" key , right mouse clicks while pressing the "SHIFT" key and movements of the mouse over the OpenCV window while pressing the "ALT" key.
------------
#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace std;
using namespace cv;

void CallBackFunc(int event, int x, int y, int flags, void* userdata)
{
    if ( flags == (EVENT_FLAG_CTRLKEY + EVENT_FLAG_LBUTTON) )
    {
        cout << "Left mouse button is clicked while pressing CTRL key - position (" << x << ", " << y << ")" << endl;
    }
    else if ( flags == (EVENT_FLAG_RBUTTON + EVENT_FLAG_SHIFTKEY) )
    {
        cout << "Right mouse button is clicked while pressing SHIFT key - position (" << x << ", " << y << ")" << endl;
    }
    else if ( event == EVENT_MOUSEMOVE && flags == EVENT_FLAG_ALTKEY)
    {
        cout << "Mouse is moved over the window while pressing ALT key - position (" << x << ", " << y << ")" << endl;
    }
}

int main(int argc, char** argv)
{
    // Read image from file 
    Mat img = imread("lena.JPG");

    //if fail to read the image
    if ( img.empty() ) 
    { 
        cout << "Error loading the image" << endl;
        return -1; 
    }

    //Create a window
    namedWindow("My Window", 1);

    //set the callback function for any mouse event
    setMouseCallback("My Window", CallBackFunc, NULL);

    //show the image
    imshow("My Window", img);

    // Wait until user press some key
    waitKey(0);

    return 0;
}
------------

Example 3: 

This code will extract color information at a pixel location in an image.
------------
#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace std;
using namespace cv;

void mouseEvent(int evt, int x, int y, int flags, void* param) 
{                    
    Mat* rgb = (Mat*) param;
    if (evt == CV_EVENT_LBUTTONDOWN) 
    { 
        printf("%d %d: %d, %d, %d\n", 
        x, y, 
        (int)(*rgb).at<Vec3b>(y, x)[0], 
        (int)(*rgb).at<Vec3b>(y, x)[1], 
        (int)(*rgb).at<Vec3b>(y, x)[2]); 
    }         
}

int main(int argc, char** argv)
{
    // Read image from file
    Mat img = imread("lena.JPG");

    //if fail to read the image
    if ( img.empty() )
    {
        cout << "Error loading the image" << endl;
        return -1;
    }

    //Create a window
    namedWindow("My Window", 1);

    //set the callback function for any mouse event
    setMouseCallback("My Window", mouseEvent, &img);

    //show the image
    imshow("My Window", img);

    // Wait until user press some key
    waitKey(0);

    return 0;
} 
------------

Example 4:

------------
#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace std;
using namespace cv;

void on_mouse( int e, int x, int y, int d, void *ptr )
{
    Point*p = (Point*)ptr;
    p->x = x;
    p->y = y;
    cout<<*p;
}

int main(int argc, char** argv)
{
    // Read image from file
    Mat img = imread("lena.JPG");

    //if fail to read the image
    if ( img.empty() )
    {
        cout << "Error loading the image" << endl;
        return -1;
    }

    //Create a window
    namedWindow("My Window", 1);

    //set the callback function for any mouse event
    Point p;
    setMouseCallback("My Window", on_mouse, &p );

    //show the image
    imshow("My Window", img);
    
    // Wait until user press some key
    waitKey(0);
    return 0;
}
------------
Sources:
http://opencv-srf.blogspot.in/2011/11/mouse-events.html
http://stackoverflow.com/questions/14874449/opencv-set-mouse-callback
http://stackoverflow.com/questions/15570431/opencv-return-value-from-mouse-callback-function

21 comments:

  1. What kind of user data can I pass to my mouse call back?

    ReplyDelete
    Replies
    1. You can pass pointers of any data type to the callBackFun.

      For example, you can pass pointers to Mat, or Point. You can get a hint from example 3 and 4.

      Delete
    2. Investment is one of the best ways to achieve financial freedom. For a beginner there are so many challenges you face. It's hard to know how to get started. Trading on the Cryptocurrency market has really been a life changer for me. I almost gave up on crypto at some point not until saw a recommendation on Elon musk successfully success story and I got a proficient trader/broker Mr Bernie Doran , he gave me all the information required to succeed in trading. I made more profit than I could ever imagine. I'm not here to converse much but to share my testimony, I recovered my losses and I have made a total profit returns of $20,500 from an investment of just $2000 within 1 week. Thanks to Mr Bernie I'm really grateful,I have been able to make a great returns trading with his signals and strategies .I urge anyone interested in INVESTMENT to take bold step in investing in the Cryptocurrency Market, he can also help you recover your lost funds, you can reach him on WhatsApp : +1(424) 285-0682 or his Gmail : BERNIEDORANSIGNALS@GMAIL.COM tell him I referred you









      Counselors. Our counselors have been logging in hours to connect with our families and also learn new skills. Tiffany has been working hard to fast track her College Counseling know how to b ready to support the Class of 2025. JJR has been working furiously to resolve and smooth out the final programming bumps and glitches - and both them are now shouldering larger counseling caseloads now that our guidance department is half the size!

      Delete
  2. If i want to return value x, y to main. How could i do ?

    Sorry but i just a newbie on opencv

    ReplyDelete
    Replies
    1. See example 4. It will return the mouse coordinates to main function as pointer.

      Delete
  3. HI, thank you very much for your codes, I just have one question, why do you put first the y and then the x in these instructions:
    (int)(*rgb).at(y, x)[0],
    (int)(*rgb).at(y, x)[1],
    (int)(*rgb).at(y, x)[2]);
    Is it the right order?

    ReplyDelete
    Replies
    1. Image axis convention: x->columns, y->rows

      Delete
  4. How can I blur faces by drawing a rectangle on them by opencv (c++) ?

    Thanks

    ReplyDelete
  5. I tried to use the example 4, but I still cant access the x and y in main. How can I do it?

    ReplyDelete
  6. in example 4,what's the meaning of "Point*p = (Point*)ptr;"?

    ReplyDelete
  7. Thanks a lot man! You saved my day!

    ReplyDelete
  8. قطع غيار اصلية باقل الاسعار في
    توكيل ويرلبول واعمال صيانه على اعلى مستوى في صيانه ويرلبول ولدينا خدمات لطلبات الصيانه من خلال رقم صيانه ويرلبول المعتمد موقعنا الالكتروني:

    http://www.maintenanceg.com/Whirlpool-Center-Agent-Egypt.html

    ReplyDelete
  9. https://imgur.com/Ps6pZJm

    Why does it will create 2 instances of window?
    Its very strange...

    I copy pasted from tutorial and still generating 2 windows...instead of one

    ReplyDelete
  10. just get lost from this blog u idiot

    ReplyDelete
  11. Very informative post! There is a lot of information here that can help any business get started with a successful social networking campaign. eta加拿大申請

    ReplyDelete
  12. Thanks and that i have a neat offer you: Can You Hire Someone To Renovate A House cost to gut and renovate a house

    ReplyDelete
  13. Thanks and that i have a dandy supply: House Renovation What Order house renovations near me

    ReplyDelete