在单个opencv中对不同的鼠标操作使用单个或多个mousecallback window

Use single or multiple mousecallback for different mouse operations in a single opencv window

我目前有两个鼠标回调函数,它们都在做不同的事情

 // Function to choose midpoint of a circle
static void onMouse( int event, int x, int y, int, void* )
{
    //Detect Mouse button event
    if( event == EVENT_LBUTTONDOWN )
    {
         //store point clicked
    }
}

第二个回调从 window 上单击的点绘制一条线,同时按住鼠标左键到它被释放的点

 // Draws a line from the beginning of a point to another.
 // This line is the diameter of a circle 
 // The first point isn't the coordinates stored by onMouse
static void DrawLine( int event, int x, int y, int, void* ) 
        {
            switch (event)
            {
             case EVENT_LBUTTONDOWN:
                 // start point
              case EVENT_LBUTTONUP:
                //endpoint
                break;
              case EVENT_MOUSEMOVE:
                  if(clicked)
                  {
                    // store point
                  }
                break;
              default    : break;
              }
         }

如何按顺序分别调用每个函数。即 运行 onMouse Callback 首先和 运行 DrawLine Callback 函数

Main()
{
   setMouseCallback("WinName", onMouse, 0);

   setMouseCallback("WinName", DrawLine, 0);

// capture first frame of video
   cap >> frame;

 // set midpoint when  onMouse is called

 // set coordinates of the line  when DrawLine is called

  while (true)
  {
       cap >> frame;

      // do the rest of your stuff here 
  }

}

只需使用一个回调函数onMouse:

setMouseCallback("WinName", onMouse, 0);

然后您可以从 onMouse:

调用 DrawLine
static void DrawLine( int event, int x, int y) 
{
    // Do something...
}

static void onMouse( int event, int x, int y, int, void* )
{
    //Detect Mouse button event
    if( SOME_CONDITION )
    {
         // Do something...
    }
    else
    {
        DrawLine( event, x, y )
    }
}

你通常会根据 CTRL, ALT, SHIFT.

这样的修饰语来做不同的事情