title: OpenCV: Storing the coordinates of the points clicked by mouse left button
body: <p>The following program would give the location of left mouse button click.  </p>
  
  <pre><code> void onMouse(int evt, int x, int y, int flags, void* param) {
      if(evt == CV_EVENT_LBUTTONDOWN) {
          cv::Point* ptPtr = (cv::Point*)param;
          ptPtr-&gt;x = x;
          ptPtr-&gt;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*)&amp;pt);
     int X, Y; 
  
      while(1)
      {
      cv::imshow("Output Window", frame);
  
      X=pt.x; 
  
      Y=pt.y; 
  
      cout&lt;&lt;"X and Y coordinates are given below"&lt;&lt;endl;
     cout&lt;&lt;X&lt;&lt;'\t'&lt;&lt;Y&lt;&lt;endl; 
  
  
   waitKey(10);
  
      }
  
  
      getch(); 
  }
  </code></pre>
  
  <p>I want to draw a line connecting the two points user clicks. I know line can be drawn by the function:</p>
  
  <pre><code>C++: void line(Mat&amp; img, Point pt1, Point pt2, const Scalar&amp; color, int thickness=1, int lineType=8, int shift=0)
  </code></pre>
  
  <p>But the problem is I have to provide two points to this function, but my previous point is lost as can be seen in the following code:</p>
  
  <pre><code>   void onMouse(int evt, int x, int y, int flags, void* param) {
          if(evt == CV_EVENT_LBUTTONDOWN) {
              cv::Point* ptPtr = (cv::Point*)param;
              ptPtr-&gt;x = x;
              ptPtr-&gt;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*)&amp;pt);
         int X, Y; 
  
          while(1)
          {
          cv::imshow("Output Window", frame);
  
          X=pt.x; 
  
          Y=pt.y; 
  
          cout&lt;&lt;"X and Y coordinates are given below"&lt;&lt;endl;
         cout&lt;&lt;X&lt;&lt;'\t'&lt;&lt;Y&lt;&lt;endl; 
  
  
  
   line(frame,  pt1,  pt2, 'r',  1,  8,  0); //here I am having only one point. This is the issue  
  
  
       waitKey(10);
  
          }
  
  
          getch(); 
      }
  </code></pre>
  
  <p><strong>Edit</strong></p>
  
  <p>So is there anyway to store the coordinates of the point clicked by user. So let's say user clicks two points on the image, and we would store x coordinates of the two clicks in X[0] and X[1], and similarly Y[0] and Y[1] for y coordinates. </p>
  
  <p>Then I can easily use the function for drawing a line.  Kindly help me proceed in this direction.      </p>
  
  <p>Thanks in advance for your suggestions.   </p>
  
  <p><strong>My latest code</strong> </p>
  
  <pre><code>using namespace cv;
  using namespace std;
  
  
  void onMouse(int evt, int x, int y, int flags, void* param) {
      if(evt == CV_EVENT_LBUTTONDOWN) {
          std::vector&lt;cv::Point&gt;* ptPtr = (std::vector&lt;cv::Point&gt;*)param;
          ptPtr-&gt;push_back(cv::Point(x,y));
      }
  }
  
  int main()
  
  {
  std::vector&lt;Point&gt; points;
  
  cv::namedWindow("Output Window");
  
  Mat frame = cv::imread("chhha.png");
  
  cv::setMouseCallback("Output Window", onMouse, (void*)&amp;points);
  int X=0, Y=0; 
  
  
  while(1)
  {
      cv::imshow("Output Window", frame);
  
  X=points[0].x; 
  Y=points[0].y; 
  
  cout&lt;&lt;"First X and Y coordinates are given below"&lt;&lt;endl;
  cout&lt;&lt;X&lt;&lt;'\t'&lt;&lt;Y&lt;&lt;endl; 
  
  
      waitKey(10);
  }
  
  
  getch(); 
  
  }
  </code></pre>
  
  <p><strong>This has two major issues:</strong></p>
  
  <p>1- This would compile well, but during run time it gives <code>Debug Assertion Failed!</code> error,  </p>
  
  <p>at the following lines when I debugged by putting breakpoints: </p>
  
  <pre><code>X=points[0].x; 
  Y=points[0].y; 
  </code></pre>
  
  <p>It further says: </p>
  
  <blockquote>
    <p>Expression: Vector subscript out of range</p>
  </blockquote>
  
  <p>2- How do I come out of the while loop? 
  In other similar programs I noticed that it remains in while loop forever.   </p>