Opencv无法在之前的早期捕获点击

时间:2013-02-22 23:24:30

标签: c++ performance events opencv

我为开放式CV编写了一个非常简单的UI类(基于OpenCV矩阵上的绘图按钮),但似乎OpenCV有某种事件处理程序错误:
当我单击下来时,如果我点击太快,则不会捕获第二次向下点击事件。但是,如果我点击,向上,向下和向上,虽然第二个向下仍然被忽略,但无论我尝试点击多快,都会捕获第二个向上事件。
调试输出(1 =向下,-1 =向上):

Button: 1    //slow clicks
Button: -1
Button: 1
Button: -1
Button: 1
Button: -1
Button: 1
Button: -1
Button: 1   //started clicking fast
Button: -1
Button: -1
Button: 1
Button: -1
Button: -1
Button: 1
Button: -1
Button: -1
Button: 1

因为在没有代码的情况下发布问题我已经被贬低了,所以这里有一些代码:

void Opencv_UI::event_process(int evt, int x, int y, int flags) 
{
    int button = 0;
    switch(evt) 
    {
        case CV_EVENT_LBUTTONDOWN : button=1; break;
        case CV_EVENT_LBUTTONUP : button=-1; break;
        case CV_EVENT_RBUTTONDOWN : button=3; break;
        case CV_EVENT_RBUTTONUP : button=-3; break;
    }
    if(button==1||button==-1)
      std::cout<<"Button: "<<button<<"\n";
        //Code below does not affect event performance, I tryed the script with all this commented out
    int button_count = buttons.size();
    for(int i=0; i<button_count; i++) //Informs buttons about mouse event
    {
        buttons[i]->mouse(x, y, button);
    }
    this->render();    //Renders buttons affected by click/mousemove
}

1 个答案:

答案 0 :(得分:0)

事实证明,当检测到双击时会调度不同的事件,因此会发送CV_EVENT_LBUTTONDBLCLK常量。
这就是开关应该是这样的:

switch(evt) 
{
       case CV_EVENT_LBUTTONDBLCLK :  //Capture doubleclick too
       case CV_EVENT_LBUTTONDOWN : button=1; break;
       case CV_EVENT_LBUTTONUP : button=-1; break;
       case CV_EVENT_RBUTTONDOWN : button=3; break;
       case CV_EVENT_RBUTTONUP : button=-3; break;
}