title: CV - Extract differences between two images
body: <p>I am currently working on an intrusion system based on video surveillance. In order to complete this task, I take a snapshot of the background of my scene (assume it's totally clean, no people or moving objects). Then, I compare the frame I get from the (static) video camera and look for the differences. I have to be able to check <strong>any</strong> differences, not only human shape or whatever, so I cannot specific feature extraction.</p>
  
  <p>Typically, I have:</p>
  
  <p><a href="https://i.imgur.com/vvRgjyn.jpg" rel="noreferrer"><img src="https://i.imgur.com/vvRgjyn.jpg" alt="http://postimg.org/image/dxtcp4u8h/"></a></p>
  
  <p>I am using OpenCV, so to compare I basically do:</p>
  
  <pre><code>cv::Mat bg_frame;
  cv::Mat cam_frame;
  cv::Mat motion;
  
  cv::absdiff(bg_frame, cam_frame, motion);
  cv::threshold(motion, motion, 80, 255, cv::THRESH_BINARY);
  cv::erode(motion, motion, cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3,3)));
  </code></pre>
  
  <p>Here is the result:</p>
  
  <p><a href="https://i.imgur.com/Bwm1tjE.png" rel="noreferrer"><img src="https://i.imgur.com/Bwm1tjE.png" alt="http://postimg.org/image/3kz0o62id/"></a></p>
  
  <p>As you can see, the arm is stripped (due to color differential conflict I guess) and this is sadly not what I want.</p>
  
  <p>I thought about add the use of <code>cv::Canny()</code> in order to detect the edges and fill the missing part of the arm, but sadly (once again), it only solves the problem in few situation not most of them.</p>
  
  <p>Is there any algorithm or technique I could use to obtain an <em>accurate</em> difference report?</p>
  
  <p>PS: Sorry for the images. Due to my newly subscription, I do not have enough reputation.</p>
  
  <p><strong>EDIT</strong>
  I use grayscale image in here, but I am open to any solution.</p>