2016-04-26 63 views
-3

我想輸入用戶使用openCV點擊的圖像座標的集合。我無法弄清楚如何使用callBack函數重複給出不同點的座標。我有點被點擊的數量。鼠標回調使用openCV輸入圖像的座標

+0

你在找什麼樣的座標?如果您可以共享某些數據和預期結果,這將會很有幫助 –

+0

我需要點擊像素的x和y座標來進行三角測量。 –

回答

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; 
}