title: Drawing rectangle on image opencv?
body: <p>I have loaded an image using opencv libraries in python and now i want to draw a rectangle using mouse but while drawing the rectangle the rectangle should be visible so that the person drawing it can place it in a way so that the object of interest is completely in rectangle but my code is producing not the desired output</p>
  
  <p>my code is</p>
  
  <pre><code>import os
  import sys
  import numpy as np
  import cv2
  
  baseDir = '/home/aman/Downloads/shirt/'
  
  filenames = next(os.walk(baseDir))[2]
  
  drawing = False # true if mouse is pressed
  mode = True # if True, draw rectangle. Press 'm' to toggle to curve
  ix,iy = -1,-1
  tx,ty = -1,-1
  
  def draw_circle(event,x,y,flags,param):
      global ix,iy,drawing,mode
      print 'aman'
      if event == cv2.EVENT_LBUTTONDOWN:
      drawing = True
      ix,iy = x,y
  
      elif event == cv2.EVENT_MOUSEMOVE:
      if drawing == True:
          if mode == True:
              #cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
              cv2.line(img, (ix,iy),(ix,y), 255, 1, 8, 0)
              cv2.line(img, (ix,iy),(x,iy), 255, 1, 8, 0)
  
              cv2.line(img, (ix,y),(x,y), 255, 1, 8, 0)
              cv2.line(img, (x,iy),(x,y), 255, 1, 8, 0)
          else:
              cv2.circle(img,(x,y),5,(0,0,255),-1)
  
      elif event == cv2.EVENT_LBUTTONUP:
      drawing = False
      if mode == True:
          #cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
          cv2.rectangle(img,(ix,y),(x,y),(0,255,0),-1)
          cv2.rectangle(img,(x,iy),(x,y),(0,255,0),-1)
      else:
          cv2.circle(img,(x,y),5,(0,0,255),-1)
  
  img = np.zeros((512,512,3), np.uint8)
  cv2.namedWindow('image')
  cv2.setMouseCallback('image',draw_circle)
  
  while(1):
      cv2.imshow('image',img)
      k = cv2.waitKey(1) &amp; 0xFF
      if k == ord('m'):
      mode = not mode
      elif k == 27:
      break
  
  cv2.destroyAllWindows()        
  </code></pre>
  
  <p>`</p>
  
  <p>but the output produced is something like
  <img src="https://i.stack.imgur.com/1WLlj.png" alt="enter image description here"></p>
  
  <p>I dont want this kind of behaviour i just want a single rectangle not for every movement. I know i should modify the code in mouse move event but the problem is how to do that so that when the person is moving the mouse he is able to see the complete rectangle and when he releases the button the rectangle should be fixed ?</p>