title: Augmented Reality line that moves with an image in OpenCV
body: <p>I want to do the following things in openCV. The problem statement that I have is with a bottle, which needs a line on the image and the line needs to rotate as per the movement of bottle.</p>
  <p><a href="https://i.stack.imgur.com/2VbmI.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2VbmI.jpg" alt="The first image should look like this, where the red lines are the borders" /></a></p>
  <p><a href="https://i.stack.imgur.com/oeIZk.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oeIZk.jpg" alt="The second image should look like this, where the red lines don't move but the green line moves as the bottle rotates" /></a></p>
  <p><a href="https://i.stack.imgur.com/SrPps.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SrPps.jpg" alt="Finally the application needs to be killed when the green line gets aligned to the left red line" /></a></p>
  <p>The first image needs to have red lines as the borders and initiate a green line</p>
  <p>The second image needs to have the green line in the middle when the bottle gets rotated. That is the green line has to follow the rotation of the bottle</p>
  <p>Finally as per the third image, the application needs to kill itself or save the picture when the green line gets aligned to the red line</p>
  <p>I tried doing this in OpenCV using template matching. I tried keeping a template image and then tracking the template image using template matching algorithm. But it does not seem to work properly in this case.</p>
  <pre><code>import cv2
  from time import sleep
  import numpy as np
  
  vid = cv2.VideoCapture(0)
  sleep(2)
  line_show = False
  save_reference = False
  template_compare_method = cv2.TM_SQDIFF_NORMED
  i = 0
  
  while True:
  
      check, frame = vid.read()
  
      print(check)
      frame1 = cv2.line(frame, (500, 0), (500, 720), (255, 0, 0), 7)
      frame1 = cv2.line(frame1, (800, 0), (800, 720), (255, 0, 0), 7)
  
      if line_show:
          h, w = frame1.shape[:2]
          if not save_reference:
              reference = frame1[200:500, 780:790]
              cv2.imwrite(&quot;../../images/white_image.jpg&quot;, reference)
              save_reference = True
          if save_reference:
              reference_image = cv2.imread('../../images/white_image.jpg')
              result = cv2.matchTemplate(reference_image, frame1, template_compare_method)
              mn, _, mnLoc, _ = cv2.minMaxLoc(result)
              MPx, MPy = mnLoc
              trows, tcols = reference_image.shape[:2]
              frame1 = cv2.rectangle(frame1, (MPx, MPy), (MPx+tcols, MPy+trows), (0, 0, 255), 2)
  
      cv2.imshow(&quot;image&quot;, frame1)
      key = cv2.waitKey(1)
      if key == ord('l'):
          line_show = True
      if key == ord('k'):
          cv2.imwrite(&quot;../../images/saved_image_&quot;+str(i)+&quot;.jpg&quot;, frame1)
          i = i + 1
      if key == ord('s'):
          cv2.imwrite(&quot;../../images/saved_image.jpg&quot;, frame)
          vid.release()
          print(&quot;Image saved&quot;)
          break
  
      elif key == ord('q'):
          vid.release()
          cv2.destroyAllWindows()
          break
  
  </code></pre>
  <p>Can I use any other algorithms, or am I approaching this problem in a wrong way by looking it as a object tracking task, where I save a small image and track it through template matching ?
  Can I use some other algorithms like Meanshift, Frame Difference etc. to achieve this ?</p>