title: Noise removal to create mask in OpenCV
body: <p>I need to create a mask to retrieve an object (foreground object) based on two related images.</p>
  
  <p>Image 1:</p>
  
  <p>[![enter image description here]</p>
  
  <p>Image 2:</p>
  
  <p>[![enter image description here]</p>
  
  <p>The images contain a foreground object and a background with texture.
  The two images are mostly the same except that in image2, the foreground object may have changed a little bit (it could have been rotated, translated or/and scaled).</p>
  
  <p>Using OpenCV, I did the followings: </p>
  
  <ul>
  <li>perform image alignment (using <code>findTransformECC</code> with param <code>cv::MOTION_AFFINE</code>) to get transformation of foreground;</li>
  <li>do transformation to image1 (using <code>cv::warpAffine</code> with param <code>cv::INTER_LINEAR</code> + <code>cv::WARP_INVERSE_MAP</code>) based on the transform matrix above;</li>
  <li>do absolute diff (<code>cv::absdiff</code> &amp; <code>cv::threshold</code> with param <code>cv::THRESH_BINARY_INV</code>) between image2 and already transformed image1. </li>
  </ul>
  
  <p>I think I am close to my goal but I still can not get clean mask of foreground object due to remaining noises on the background area.</p>
  
  <p><a href="https://i.stack.imgur.com/bkwoQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bkwoQ.png" alt="enter image description here"></a></p>
  
  <p>What is the solution to remove all noise on the image_absdiff_invert.png (above) in order to create a clean mask of the foreground object ?</p>