Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ OpenCV:存储鼠标左键单击的点的坐标_C++_Opencv - Fatal编程技术网

C++ OpenCV:存储鼠标左键单击的点的坐标

C++ OpenCV:存储鼠标左键单击的点的坐标,c++,opencv,C++,Opencv,下面的程序将给出鼠标左键单击的位置 void onMouse(int evt, int x, int y, int flags, void* param) { if(evt == CV_EVENT_LBUTTONDOWN) { cv::Point* ptPtr = (cv::Point*)param; ptPtr->x = x; ptPtr->y = y; } } int main() { cv::Point

下面的程序将给出鼠标左键单击的位置

 void onMouse(int evt, int x, int y, int flags, void* param) {
    if(evt == CV_EVENT_LBUTTONDOWN) {
        cv::Point* ptPtr = (cv::Point*)param;
        ptPtr->x = x;
        ptPtr->y = y;
    }
}

int main() {
    cv::Point2i pt(-1,-1);//assume initial point
    cv::namedWindow("Output Window");

    Mat frame = cv::imread("chhhha.png");

    cv::setMouseCallback("Output Window", onMouse, (void*)&pt);
   int X, Y; 

    while(1)
    {
    cv::imshow("Output Window", frame);

    X=pt.x; 

    Y=pt.y; 

    cout<<"X and Y coordinates are given below"<<endl;
   cout<<X<<'\t'<<Y<<endl; 


 waitKey(10);

    }


    getch(); 
}
但问题是,我必须为此函数提供两点,但我前面的一点丢失了,如以下代码所示:

   void onMouse(int evt, int x, int y, int flags, void* param) {
        if(evt == CV_EVENT_LBUTTONDOWN) {
            cv::Point* ptPtr = (cv::Point*)param;
            ptPtr->x = x;
            ptPtr->y = y;
        }
    }

    int main() {
        cv::Point2i pt(-1,-1);
        cv::namedWindow("Output Window");

        Mat frame = cv::imread("chhhha.png");

        cv::setMouseCallback("Output Window", onMouse, (void*)&pt);
       int X, Y; 

        while(1)
        {
        cv::imshow("Output Window", frame);

        X=pt.x; 

        Y=pt.y; 

        cout<<"X and Y coordinates are given below"<<endl;
       cout<<X<<'\t'<<Y<<endl; 



 line(frame,  pt1,  pt2, 'r',  1,  8,  0); //here I am having only one point. This is the issue  


     waitKey(10);

        }


        getch(); 
    }
它还说:

表达式:向量下标超出范围

2-如何走出while循环?
在其他类似的程序中,我注意到它永远保持在while循环中

我建议不要传递给
cv::Point*
,而是传递一个
std::vector*
。由于
cv::Point
有一个复制构造函数,因此可以通过
向后推
将点存储在其中

我所说的代码:

std::vector<cv:Point> points;
cv::namedWindow("Output Window");

Mat frame = cv::imread("chhhha.png");

cv::setMouseCallback("Output Window", onMouse, (void*)&points);
int X, Y; 

while(1)
{
    cv::imshow("Output Window", frame);

    if (points.size() > 2) //we have 2 points
    {
        for (auto it = points.begin(); it != points.end(); ++it)
        {

            cout<<"X and Y coordinates are given below"<<endl;
            cout<<(*it).x<<'\t'<<(*it).y<<endl; 
        }
        //draw points
    }
...
std::向量点;
cv::namedWindow(“输出窗口”);
Mat frame=cv::imread(“chhhha.png”);
cv::setMouseCallback(“输出窗口”、onMouse、(void*)和points);
int X,Y;
而(1)
{
cv::imshow(“输出窗口”,帧);
如果(points.size()>2)//我们有2个点
{
对于(自动it=点。开始();it!=点。结束();+it)
{

cout我建议不要传递给
cv::Point*
,而是传递一个
std::vector*
。因为
cv::Point
有一个复制构造函数,你可以通过
向后推
将点存储在那里

我所说的代码:

std::vector<cv:Point> points;
cv::namedWindow("Output Window");

Mat frame = cv::imread("chhhha.png");

cv::setMouseCallback("Output Window", onMouse, (void*)&points);
int X, Y; 

while(1)
{
    cv::imshow("Output Window", frame);

    if (points.size() > 2) //we have 2 points
    {
        for (auto it = points.begin(); it != points.end(); ++it)
        {

            cout<<"X and Y coordinates are given below"<<endl;
            cout<<(*it).x<<'\t'<<(*it).y<<endl; 
        }
        //draw points
    }
...
std::向量点;
cv::namedWindow(“输出窗口”);
Mat frame=cv::imread(“chhhha.png”);
cv::setMouseCallback(“输出窗口”、onMouse、(void*)和points);
int X,Y;
而(1)
{
cv::imshow(“输出窗口”,帧);
如果(points.size()>2)//我们有2个点
{
对于(自动it=点。开始();it!=点。结束();+it)
{

coutgreattt它起作用了!!现在唯一的问题是我可以存储这些值并在while循环之外访问。类似这样的内容:X1=点[0]。x;X2=点[1].x等等。这是我的主要困难。你完全可以做到!试试看。请记住,你必须确保有足够的点数。例如,运行while循环直到
points.size()>2
,然后是
中断
。然后您可以访问。我想我可以让它工作。但我仍然被困在while循环中。如何退出while循环,返回控制台并打印我的值?我在for循环中设置了一个中断。它工作得很好。谢谢(有一个小问题,如果我在一段时间后“不重新发送错误”,它将挂起,我将在另一个问题中询问。非常感谢您的帮助)Greattt成功了!!现在唯一的问题是我可以存储这些值并在while循环外访问。类似这样的内容:X1=点[0]。x;X2=点[1].x等等。这是我的主要困难。你完全可以做到!试试看。请记住,你必须确保有足够的点数。例如,运行while循环直到
points.size()>2
,然后是
中断
。然后您可以访问。我想我可以让它工作。但我仍然被困在while循环中。如何退出while循环,返回控制台并打印我的值?我在for循环中设置了一个中断。它工作得很好。谢谢(有一个小问题,如果我在一段时间后“没有回复错误”,它将挂起,我将在另一个问题中询问。非常感谢您的帮助)
X=points[0].x; 
Y=points[0].y; 
std::vector<cv:Point> points;
cv::namedWindow("Output Window");

Mat frame = cv::imread("chhhha.png");

cv::setMouseCallback("Output Window", onMouse, (void*)&points);
int X, Y; 

while(1)
{
    cv::imshow("Output Window", frame);

    if (points.size() > 2) //we have 2 points
    {
        for (auto it = points.begin(); it != points.end(); ++it)
        {

            cout<<"X and Y coordinates are given below"<<endl;
            cout<<(*it).x<<'\t'<<(*it).y<<endl; 
        }
        //draw points
    }
...
void onMouse(int evt, int x, int y, int flags, void* param) {
    if(evt == CV_EVENT_LBUTTONDOWN) {
        std::vector<cv::Point>* ptPtr = (std::vector<cv::Point>*)param;
        ptPtr->push_back(cv::Point(x,y));
    }
}