如何使用鼠标事件使用setmousecallback在opencv中获取循环

时间:2014-07-25 05:55:53

标签: opencv

我正在使用openCV的setmousecallback()函数,一旦用户按下鼠标右键,就会从for(视频帧)的for循环中断。 但该计划正在变得悬而未决。有人可以帮帮我..

以下是我正在使用的代码,请原谅我任何编译错误

//mouse callback funtion
void getMouseLoc(int event, int xCor, int yCor, int flags, void* userInput)
{
Point3_<int> *mouseInputs = (Point3_<int>*)userInput;
mouseInputs->x= xCor;
mouseInputs->y = yCor;
mouseInputs->z = event;
}

// for loop
for( ; ; )
{
camera >> frame;
imshow("averageFrame", aver`enter code here`ageFrame);
setMouseCallback("averageFrame", getMouseLoc, &mouseInputs);
if( mouseInputs.z == EVENT_RBUTTONDOWN)
{
cout << "topmost cordinates of ROI selected by user" << endl;
count << mouseInputs.x << " and " << mouseInputs.y << endl;
break;
}
}

1 个答案:

答案 0 :(得分:1)

//mouse callback function
void getMouseLoc(int event, int xCor, int yCor, int flags, void* userInput)
{
    Point3_<int> *mouseInputs = (Point3_<int>*)userInput;
    mouseInputs->x= xCor;
    mouseInputs->y = yCor;
    mouseInputs->z = event;
}

// this does not have to be inside the loop, once is enough:        
Point3_<int> mouseInputs;
setMouseCallback("averageFrame", getMouseLoc, &mouseInputs);

// for loop
for( ; ; )
{
    camera >> frame;
    imshow("averageFrame", aver`enter code here`ageFrame);

    // you're  missing a call to waitKey(), else your image won't get updated !!
    int k = waitKey(40);
    if ( k == 27 ) break; // esc pressed.

    if( mouseInputs.z == EVENT_RBUTTONDOWN)
    {
        cout << "topmost cordinates of ROI selected by user" << endl;
        count << mouseInputs.x << " and " << mouseInputs.y << endl;
        break;
    }
}