title: Subtract And Merging Images for Making Video Frames
body: <p>When you want create a video file,the size of it has important.
  I want to save the difference frames from each other , not whole of them,with JPEG format for decrease size of my new video file.
  I used Aforge.Net Framework and it produces the result of difference well (with Difference class). But when I want merge the frame with previous frame for generate current frame the result isn't that I need,see the example below :</p>
  <h2>This is the Background Frame</h2>
  <p><img src="https://i.stack.imgur.com/4qMo0.png" alt="This is Background Frame" /></p>
  <h2>This is the Current Frame</h2>
  <p><img src="https://i.stack.imgur.com/cxSKM.png" alt="This is Current Frame" /></p>
  <h2>This is the Difference Frame</h2>
  <p><img src="https://i.stack.imgur.com/svtF1.jpg" alt="This is Difference Frame" /></p>
  <p>the code for getting difference :</p>
  <pre><code>AForge.Imaging.Filters.Difference sb = new AForge.Imaging.Filters.Difference(bmpDest);
              Bitmap result = sb.Apply(bmpSrc);
              result.Save(string.Format(address, 10002), ImageFormat.Jpeg);
  </code></pre>
  <p>As you know, when I merge the difference frame with the background frame,the current frame must be generated,but this is generated frame :</p>
  <p><img src="https://i.stack.imgur.com/tWAyQ.jpg" alt="enter image description here" /></p>
  <p>And the source code is:</p>
  <pre><code> AForge.Imaging.Filters.Merge mg = new AForge.Imaging.Filters.Merge(bmpSrc);
              Bitmap results = mg.Apply(new Bitmap(string.Format(address, 10002)));
              results.Save(string.Format(address, 1004), ImageFormat.Jpeg);
  </code></pre>
  <p>So,the problem is clear.
  These are screen captured but when I want use this operation to camera capture the result has many problem too.</p>
  <p>I used openCV for this operation too, but in the instruction mentioned for getting difference, first I should convert frames to grayscale images and i have problem with merging image too, like AForge.</p>
  <p>And at last i used my algorithm but the size of difference image be larger than original images!</p>
  <p>How should I do this operation?
  Remember the size of Images is very important and I want to save the JPEG files (for the compression).</p>