简体   繁体   中英

Access pixel values by clicking on a point in an image with OpenCV

有没有办法在打开的CV上单击显示图像的指定点,以便稍后再打印其值?

Simple Googling gave this http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/ - look at the Input handling section, it defines how to make a mouse input handler callback, and you will get coordinates of mouse, that you can use on the image matrix. Should solve the issue.

From the link above:

Insert this function definition:

void mouseHandler(int event, int x, int y, int flags, void* param)
{
    switch(event){
    case CV_EVENT_LBUTTONDOWN:
        if(flags & CV_EVENT_FLAG_CTRLKEY) 
            printf("Left button down with CTRL pressed\n");
        break;
    case CV_EVENT_LBUTTONUP:
        printf("Left button up\n");
        break;
    }
}

And in the main or event loop or wherever you do initialization etc.,

int mouseParam= CV_EVENT_FLAG_LBUTTON;
cvSetMouseCallback("win1",mouseHandler,&mouseParam);

where "win1" should be replaced with the name of your window. More details on cvSetMouseCallback can be found in http://opencv.willowgarage.com/documentation/user_interface.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM