2017-10-16 91 views
0

我現在有兩個鼠標回調函數在一個單一的OpenCV窗口不同的鼠標操作使用單個或多個mousecallback都在做不同的東西

// 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 
    } 
} 

第二回調吸引了來自一個點的直線單擊窗口而上鼠標左鍵按住至其新版本發佈

// 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回調和運行DrawLine的回調函數

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 
    } 

} 

回答

0

只需使用一個回調函數onMouse

setMouseCallback("WinName", onMouse, 0); 

然後,您可以撥打DrawLineonMouse

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等修飾符做不同的事情。

+0

這是我基於按鍵的原創想法。我希望有一個內置的opencv函數來處理這個 – MaskedAfrican

+0

做什麼,到底是什麼? – Miki

+0

我決定改用這個方法。少混亂。 – MaskedAfrican