使用openCV输入图像的坐标

时间:2016-04-26 13:43:11

标签: opencv

我正在尝试使用openCV输入用户点击的图像的一组坐标。我无法弄清楚如何使用callBack函数重复给出不同点的坐标。我有点击的点数。

1 个答案:

答案 0 :(得分:1)

鼠标回调的C ++代码:

#include<opencv2/core/core.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<iostream>

using namespace cv;
using namespace std;
Mat img(400,400,CV_8U,Scalar(120,120,120));//global for mouse callback
vector<Point> points; // to store clicked points
void CallBackFunc(int event, int x, int y, int flags, void* userdata)
{
     if ( event == EVENT_LBUTTONDOWN )
     {
       circle(img,Point(x,y),5,Scalar(255,255,255),1);//global Mat is never refreshed after 1st imread
       cout << "Mouse move over the window - position (" << x << ", " << y << ")" << endl;
       points.push_back(Point(x,y)); // store clicked point

     }

     if( points.size() == 3 )
     {
     line( img, points[0], points[1], Scalar(0,255,0),1);
     line( img, points[0], points[2], Scalar(0,255,0),1);
     line( img, points[1], points[2], Scalar(0,255,0),1);
     points.clear();
     }       
}

int main()
{

    namedWindow("img");//for mousecallback
        int event;

    for(;;)
        {   

            setMouseCallback("img", CallBackFunc, NULL);

            imshow("img",img);


            char c=waitKey(10);
            if(c=='b')
                {
                    break;
                }
        }

    return 1;
}