title: Detect and visualize differences between two images with OpenCV Python
body: <p>I have two images and would like to make it obvious where the differences are. I want to add color to the two images such that a user can clearly spot all the differences within a second or two.</p>
  <p>For example, here are two images with a few differences:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: center;"><em>leftImage.jpg:</em></th>
  <th style="text-align: center;"><em>rightImage.jpg:</em></th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: center;"><img src="https://i.stack.imgur.com/lWUlB.jpg" alt="image_1" /></td>
  <td style="text-align: center;"><img src="https://i.stack.imgur.com/gz9Kf.jpg" alt="image_2" /></td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>My current approach to make the differences obvious, is to create a mask (difference between the two images), color it red, and then add it to the images. The goal is to clearly mark all differences with a strong red color. Here is my current code:</p>
  <pre><code>import cv2
  
  # load images
  image1 = cv2.imread(&quot;leftImage.jpg&quot;)
  image2 = cv2.imread(&quot;rightImage.jpg&quot;)
  
  # compute difference
  difference = cv2.subtract(image1, image2)
  
  # color the mask red
  Conv_hsv_Gray = cv2.cvtColor(difference, cv2.COLOR_BGR2GRAY)
  ret, mask = cv2.threshold(Conv_hsv_Gray, 0, 255,cv2.THRESH_BINARY_INV |cv2.THRESH_OTSU)
  difference[mask != 255] = [0, 0, 255]
  
  # add the red mask to the images to make the differences obvious
  image1[mask != 255] = [0, 0, 255]
  image2[mask != 255] = [0, 0, 255]
  
  # store images
  cv2.imwrite('diffOverImage1.png', image1)
  cv2.imwrite('diffOverImage2.png', image1)
  cv2.imwrite('diff.png', difference)
  </code></pre>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: center;"><em>diff.png:</em></th>
  <th style="text-align: center;"><em>diffOverImage1.png</em></th>
  <th style="text-align: center;"><em>diffOverImage2.png</em></th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: center;"><img src="https://i.stack.imgur.com/fsQ77.png" alt="img_3" /></td>
  <td style="text-align: center;"><img src="https://i.stack.imgur.com/ZysEk.png" alt="img_4" /></td>
  <td style="text-align: center;"><img src="https://i.stack.imgur.com/Fwk7J.png" alt="img_5" /></td>
  </tr>
  </tbody>
  </table>
  </div>
  <p><strong>Problem with the current code:</strong>
  The computed mask shows some differences but not all of them (see for example the tiny piece in the upper right corner, or the rope thingy on the blue packet). These differences are shown only very lightly in the computed mask, but they should be clearly red like the other differences.</p>
  <p><strong>Input:</strong> 2 images with some differences.</p>
  <p><strong>Expected Output:</strong> 3 images: the two input images but with the differences highlighted (clearly highlighted in a configurable color), and a third image containing only the differences (the mask).</p>