title: Detecting an approaching object
body: <p>I read <a href="https://shaneormonde.wordpress.com/2014/01/25/webcam-laser-rangefinder/" rel="nofollow noreferrer">this</a> blog post where he uses a Laser and a Webcam to estimated the distance of the cardboard from the Webcam.</p>
  
  <p>I had another idea about that. I don't want to calculate the distance from the webcam.</p>
  
  <p>I want to check if an object is approaching the webcam. The algorithm, according to me, will be something like:</p>
  
  <ol>
  <li>Detect the object in the webcam feed.</li>
  <li>If the object is approaching the webcam it'll grow larger and larger in the video feed.</li>
  <li>Use this data for further calculations.</li>
  </ol>
  
  <p>Since I want to detect random objects, I am using the <code>findContours()</code> method to find the contours in the video feed. Using that, I will at least have the outlines of the objects in the video feed. The source code is:</p>
  
  <pre><code>import numpy as np
  import cv2
  
  vid=cv2.VideoCapture(0)
  ans, instant=vid.read()
  average=np.float32(instant)
  cv2.accumulateWeighted(instant, average, 0.01)
  background=cv2.convertScaleAbs(average)
  
  while(1):
      _,f=vid.read()
      imgray=cv2.cvtColor(f, cv2.COLOR_BGR2GRAY)
      ret, thresh=cv2.threshold(imgray,127,255,0)
      diff=cv2.absdiff(f, background)
      cv2.imshow("input", f)
      cv2.imshow("Difference", diff)
      if cv2.waitKey(5)==27:
          break
  cv2.destroyAllWindows()
  </code></pre>
  
  <p>The output is:</p>
  
  <p><img src="https://i.stack.imgur.com/mozX3.jpg" alt="enter image description here"></p>
  
  <p>I am stuck here. I have the contours stored in an array. What do I do with it when the size increases? How do I proceed?</p>