title: Detect bad frames in OpenCV 2.4.9
body: <p>I know the title is a bit vague but I'm not sure how else to describe it. </p>
  
  <p>CentOS with ffmpeg + OpenCV 2.4.9. I'm working on a simple motion detection system which uses a stream from an IP camera (h264). </p>
  
  <p>Once in a while the stream hiccups and throws in a "bad frame" (see pic-bad.png link below). The problem is, these frames vary largely from the previous frames and causes a "motion" event to get triggered even though no actual motion occured.</p>
  
  <p>The pictures below will explain the problem.</p>
  
  <p>Good frame (motion captured):</p>
  
  <p><img src="https://i.stack.imgur.com/SQpEC.png" alt="Good Frame"></p>
  
  <p>Bad frame (no motion, just a broken frame):</p>
  
  <p><img src="https://i.stack.imgur.com/gsBO1.png" alt="Bad Frame"></p>
  
  <p>The bad frame gets caught randomly. I guess I can make a bad frame detector by analyzing (looping) through the pixels going down from a certain position to see if they are all the same, but I'm wondering if there is any other, more efficient, "by the book" approach to detecting these types of bad frames and just skipping over them.</p>
  
  <p>Thank You!</p>
  
  <p>EDIT UPDATE:</p>
  
  <p>The frame is grabbed using a C++ motion detection program via <code>cvQueryFrame(camera);</code> so I do not directly interface with ffmpeg, OpenCV does it on the backend. I'm using the latest version of ffmpeg compiled from git source. All of the libraries are also up to date (h264, etc, all downloaded and compiled yesterday). The data is coming from an RTSP stream (ffserver). I've tested over multiple cameras (dahua 1 - 3 MP models) and the frame glitch is pretty persistent across all of them, although it doesn't happen continuously, just once on a while (ex: once every 10 minutes). </p>