opencv set mouse callback

丶灬走出姿态 提交于 2019-12-08 03:11:41

问题


I'm a beginner in OpenCV and would like to print in my console the specific value of a pixel (RGB format) that I define by clicking on it.

After some searches I managed to prind the coordinates of the click I make on the image.

If anyone knows how to do that please modify this code that I am using:

void mouseEvent (int evt, int x, int y, int flags, void* param)
{                    
     if (evt == CV_EVENT_LBUTTONDOWN)
     { 
          printf("%d %d\n",x ,y );  
     }         
}

and this is what I use to call the function:

cvSetMouseCallback("blah blah", mouseEvent, 0);

回答1:


Place your image in a Mat called frame, then:

namedWindow("test");
cvSetMouseCallback("test", mouseEvent, &frame);

char key = 0;
while ((int)key != 27) {
    imshow("test", frame);
    key =  waitKey(1);
}

where mouseEvent is defined as:

void mouseEvent(int evt, int x, int y, int flags, void* param) {                    
    Mat* rgb = (Mat*) param;
    if (evt == CV_EVENT_LBUTTONDOWN) { 
        printf("%d %d: %d, %d, %d\n", 
        x, y, 
        (int)(*rgb).at<Vec3b>(y, x)[0], 
        (int)(*rgb).at<Vec3b>(y, x)[1], 
        (int)(*rgb).at<Vec3b>(y, x)[2]); 
    }         
}


来源:https://stackoverflow.com/questions/14874449/opencv-set-mouse-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!