首页 > 解决方案 > 使用 SetMouseCallback、c++、OpenCV 获取像素信息的问题

问题描述

我一直在尝试获取有关我单击的像素的信息。我一直在使用函数 SetMouceCallback,老实说我不知道​​它是如何工作的。


void startGUI()
{
    VideoCapture video;
    Mat frame1;
    video.open("/Users/Dominik/Desktop/Legia22.mov");
    video.read(frame1);

    Mat img = frame1.clone();

    namedWindow("My Window", 1);
    setMouseCallback("My Window", onMouse, &img);
    imshow("My Window", img);
    //********* I want to use leftClicks[] here 
    waitKey(0); 
}


static void onMouse(int event, int x, int y, int, void* param)  
{

    Mat &img = *((Mat*)param); 
    static int counter=0;
    Point leftClicks[4];

     if  ( event == EVENT_LBUTTONDOWN && counter<4)
     {
         Vec3b val = img.at< Vec3b >(y,x);
         cout << "left button clicked pos -  (" << x << ", " << y << ")" <<"\t colour bgr: "<< val << endl;
         leftClicks[counter].x = x;
         leftClicks[counter].y = y;
         circle(img, Point(x,y), 4, SCALAR_BLUE, -1);
           imshow("My Window", img);
         counter++;
     }

}


以下代码工作正常。显示窗口,在第一帧我可以画 4 个圆圈。这些点打印在终端中。所以基本上我想要做的是将数组 leftClicks[4] 从函数中取出,以便在其他函数中进行进一步处理,例如。****** 在哪里。问题是函数 onMouse 的类型必须是 void 并且参数不能更改。

我注意到函数 setMouseCallback 已经很好地指定了 onMouse 函数必须是什么,并且不能对函数 onMouse 进行任何更改......

那么如何从 onMouse 函数中获取数组 leftClicks[] 呢?

标签: c++imageopencv

解决方案


推荐阅读