How to track mouse actions with OPENCV

Source: Internet
Author: User

Reprint: How to use OpenCV tracking mouse operation http://blog.skyoung.org/2014/05/01/how-to-track-mouse/

Manually marking the target position in the first frame of the video is one of the most basic operations in online vision tracking, which requires the detection of mouse movement and click events. OPENCV provides setMouseCallback this function in response to the mouse action and returns the coordinates of the mouse on the bound window. Here is a simple introduction to the use of this function.

First, setMouseCallback the C + + function is declared as follows:

  

1 void onmouse (inteventintint int. intvoid *param)

    • Winname is the name of the window
    • Onmouse is a function that responds to events
    • The userdata option is a onMouse pointer to the response function provided by the user.

This function is responsible for binding the mouse response window, so that the function will only respond to the mouse operation about the window. onMousedefines the actions that the user needs to make for different actions of the mouse.

onMouseThe Declaration of the response function is:

1 void onmouse (inteventintint int. intvoid *param)

    • Event is a mouse response event that is:
      • event_mousemove slide
      • event_lbuttondown left click
      • event_rbuttondown Right-click
      • event_mbuttondown Middle Click
      • event_lbuttonup Left click
      • event_rbuttonup right click to release
      • event_mbuttonup Middle key release
      • EVENT_LBUTTONDBLCLK left-click
      • event_rbuttondblclk Right-click
      • EV ENT_MBUTTONDBLCLK Middle-click
    • x is the horizontal axis of the mouse on the image
    • y is the vertical axis of the mouse over the image
    • Flag is an event tagged with the mouse, its attached state, such as Event_mousemove, Flag Event_flag_lbutton, left-drag state. FLAG has the following status:
      • Event_flag_lbutton Left-click
      • Event_flag_rbutton Right-drag
      • Event_flag_mbutton Middle-click Drag-and-drop
      • Event_flag_ctrlkey Ctrl + Hold
      • event_flag_shiftkey Shift-Hold
      • event_flag_altkey alt-Hold
    • param is a user-passed pointer, which is userdata in setmousecallback .

Here is a code example of a rectangular frame to explain in detail. First MouseCapture , you define a class that is stored in MouseCapture.h . Defines a private member that has a stored image, the name of the img window, the drawn box, winName rect and whether to end the frame's marker isMarked . member functions are the function of reading the image loadImg() , the box function drawRect() and the mouse response event function, onMouse() Note that this onMouse is defined as a static member function, because it is setMouseCallBack() not a member function MouseCapture of the class, if Onmouse is a normal member function, is not available because the function declaration is different, and when declared as a static member function, you can directly MouseCapture:: access the static member function directly in the scope.onMouse

MouseCapture.h
123456789101112131415161718192021222324
#ifndef Mousecapture_h#define Mousecapture_h#include "opencv2/highgui/highgui.hpp"#include "opencv2/core/core.hpp"#include <string>class mousecapture{public:mousecapture (); void Loadimg (std::string imgfile); void DrawRect (); static void Onmouse (int event, int x, int y, int flag, void *parma);  Private:cv::mat img; std::string winname; Cv::rect Rect; bool ismarked;}; #endif //mousecapture_h               

The specific definition of the class is listed below MouseCapture , in the file MouseCapture.cpp . Defines the point in the upper onMouse -left corner of the rectangle when the mouse is left-clicked, and when the mouse is lifted, the width of the rectangle is recorded, and the width of the rectangle is recorded when the mouse slides and the left-click is dragged. Note the use of flag here. The pointer param is the setMouseCallBack() incoming this pointer at the time of the call (see Line 50), which is responsible for writing the position size information of the box that tracks the rectangle that the mouse draws to the member of the class rect .

MouseCapture.cpp
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 666768
#include "MouseCapture.h"#include <iostream>Usingnamespace CV;UsingNamespaceStdvoid Mousecapture::onmouse (int event,int x,int y,int flag,void *param) {MouseCapture *MC = (MouseCapture *) param;Switch (event) {Case event_lbuttondown:mc->rect.x = x; Mc->rect.y = y;BreakCase event_lbuttonup:mc->rect.width = x-mc->rect.x; Mc->rect.height = y-mc->rect.y;BreakCase event_rbuttondown:mc->ismarked =TrueBreakCase Event_mousemove:if (flag & Event_flag_lbutton) {mc->rect.width = x-mc->rect.x; Mc->rect.height = y-mc->rect.y; }BreakDefaultBreak }}mousecapture::mousecapture (): Winname ("Mousecontrol"), Rect (Rect (0,0,0,0)), ismarked (False) {}void Mousecapture::loadimg (String imgfile) {img = Imread (imgfile);}void MouseCapture::d rawrect () {Namedwindow (winname); Setmousecallback (Winname, Onmouse,this); Mat tmpimg;while (!ismarked) {tmpimg = Img.clone (); Rectangle (tmpimg, rect, Scalar (0,0,255), 2); string str; stringstream strstream; Strstream<< "Marked region:" << Rect.x<< ":" <<rect.height; str = STRSTREAM.STR (); Puttext (tmpimg, str, point (20,20), Cv_font_hershey_complex, .8,scalar (0,255,0), 2); Imshow (Winname, tmpimg); if (waitkey (10) = =  ' Q ')  Break } cout<< "Mark finished!" <<endl;}                

Finally, the invocation of the main function:

Main.cpp
12345678910
#include <iostream>#include "MouseCapture.h"int main () {    MouseCapture MC;    Mc.loadimg ("lena.jpg");    Mc.drawrect ();    0;}   

Finally, onMouse() there is another solution to the declaration of the Mouse response event function, which is to declare the onMouse() friend function of the class without declaring it as a MouseCapture onMouse member function of the class.

How to track mouse actions with OPENCV

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.