title: Drawing rectangle or line using mouse events in open cv using python
body: <p>I am trying to draw a straight line between two coordinates which would be obtained by clicking on the image or by mouse events. I am able to draw individual circles on clicking the mouse, but cannot figure out how to draw line between those points. When I am using this code, I can only print the start and end coordinates, not draw a line between them.</p>
  
  <pre><code>import numpy as np
  import cv2
  import cv2.cv as cv
  
  boxes = []
  
  def on_mouse(event, x, y, flags, params):
      if event == cv.CV_EVENT_LBUTTONDOWN:
           print 'Start Mouse Position: '+str(x)+', '+str(y)
           sbox = [x, y]
           boxes.append(sbox)
  
      elif event == cv.CV_EVENT_LBUTTONUP:
          print 'End Mouse Position: '+str(x)+', '+str(y)
          ebox = [x, y]
          boxes.append(ebox)
  
  
  count = 0
  while(1):
      count += 1
      img = cv2.imread('img.jpg',0)
      img = cv2.blur(img, (3,3))
  
      cv2.namedWindow('real image')
      cv.SetMouseCallback('real image', on_mouse, 0)
      cv2.imshow('real image', img)
      if count &lt; 50:
          if cv2.waitKey(33) == 27:
              cv2.destroyAllWindows()
              break
      elif count &gt;= 50:
          if cv2.waitKey(0) == 27:
              cv2.destroyAllWindows()
              break
          count = 0
  </code></pre>
  
  <p>Somehow, I am not able to extract the coordinates outside the loop. Can someone please suggest how to draw lines or rectangles between the points I click on the image?</p>