12/29/2016

opencv mouse event example code


...
void CallBackFunc(int event, int x, int y, int flags, void* userdata)
{

    if (event == EVENT_LBUTTONDOWN)
    {
        printf("lLBUTTONDOWN down %d, %d \n", x, y);

        circle((*(Mat*)userdata), Point(x, y), 2, CV_RGB(255, 0, 0), 3);
    }
    else if (event == EVENT_RBUTTONDOWN)
    {
        printf("RBUTTONDOWN down %d, %d \n", x, y);
    }
    else if (event == EVENT_MBUTTONDOWN)
    {
        printf("MBUTTONDOWN down %d, %d \n", x, y);
    }
    else if (event == EVENT_MOUSEMOVE)
    {
        printf("move %d, %d \n", x, y);
    }

    //imshow("img", (*(Mat*)userdata)); //show
}


int main(int, char)
{
    namedWindow("img", 0);
    Mat img = imread("gh.jpg");
    setMouseCallback("img", CallBackFunc, &img);

    while (1)
    {
        imshow("img", img); //show
        if (waitKey(10) > 0)
            break;
    }

    destroyAllWindows();

    return 0;
}
...