title: Motion Detector - Disregard Background Motion/Incorrect ID
body: <p>I have a script in python which acts as a motion detector. I read a video file using cv2, convert to grayscale, and do simple background subtraction from the current frame to detect motion, which I draw a rectangle over. The video is eventually saved as a new file, where I can finally view it.</p>
  <p>This works fine, except sometimes the starting frame (background frame) already has motion in it, or sometimes there are features in the background which move but I don't want to detect (eg if I was detecting people, I wouldn't be interested in a flag blowing in the breeze). So I want to somehow disregard 'stationary' movement (ie motion which does not move vertically/horizontally over the course of the video). However I'm having trouble with my approach. There doesn't seem to be any functions or scripts on the internet to solve this.</p>
  <p>One idea I had was to draw a larger rectangle over the original, and then if the original rectangle doesn't leave the outer rectangle (which stays put) over the video, then that motion can be cancelled altogether. I have no idea how to implement this. I have managed to draw a larger rectangle, but it follows the original and doesn't stay in place.</p>
  <p>Does anyone have any idea how I might be able to do this? Or any resources they could point me in? Thank you. Below is my code starting from when I draw the rectangles.</p>
  <pre><code>for c in cnts:
      # if the contour is too small, ignore it
      if cv2.contourArea(c) &lt; min_area:
          continue
      # compute the bounding box for the contour, draw it on the frame, and update the text
      (x, y, w, h) = cv2.boundingRect(c)
      cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
      text = &quot;Occupied&quot; # frame is occupied 
      
      half_w=int(w/2) # get 50% sizing width
      half_h=int(h/2) # get 50% sizing height
      x_surr = int (x - (half_w/2))
      y_surr = int(y - (half_h/2))
      w_surr = (w+half_w)
      h_surr = (h+half_h)
      cv2.rectangle(frame, (x_surr, y_surr), (x_surr+w_surr, y_surr + h_surr), (255, 255, 255), 2)
  </code></pre>