Ask Your Question

# Revision history [back]

### Visualize differences between two images

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.

For example, here are two images with a few differences:

leftImage.jpg:

rightImage.jpg:

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:

import cv2

# load images
image1 = cv2.imread("leftImage.jpg")
image2 = cv2.imread("rightImage.jpg")

# compute difference
difference = cv2.subtract(image1, image2)

# try to 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]

# try to add the red mask to the images to make the differences obvious
diffOverImage1 = image1 + difference
diffOverImage2 = image2 + difference

# store images
cv2.imwrite('diffOverImage1.png', diffOverImage1)
cv2.imwrite('diffOverImage2.png', diffOverImage2)
cv2.imwrite('diff.png', difference)


Problem with the current code: The computed mask shows some differences but also contains irrelevant noise. When I add the mask to the image, the differences are not shown in red. How do I remove the irrelevant noise and clearly highlight all the differences in a strong red color?

diff.png:

diffOverImage1.png

diffOverImage2.png

### Visualize differences between two images

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.

For example, here are two images with a few differences:

leftImage.jpg:

rightImage.jpg:

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:

import cv2

# load images
image1 = cv2.imread("leftImage.jpg")
image2 = cv2.imread("rightImage.jpg")

# compute difference
difference = cv2.subtract(image1, image2)

# try to 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]

# try to add the red mask to the images to make the differences obvious
diffOverImage1 = image1 + difference
diffOverImage2 = image2 + difference

# store images
cv2.imwrite('diffOverImage1.png', diffOverImage1)
cv2.imwrite('diffOverImage2.png', diffOverImage2)
cv2.imwrite('diff.png', difference)


Problem with the current code: The computed mask shows some differences but also contains irrelevant noise. When I add the mask to the image, the differences are not shown in red. How do I remove the irrelevant noise and clearly highlight all the differences in a strong red color?

Input: 2 images with some differences.

Output: 3 images: the two input images but with the differences highlighted (clearly highlighted in a configurable color, no noise), and a third image containing only the differences (the mask).

diff.png:

diffOverImage1.png

diffOverImage2.png

### Visualize differences between two images

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.

For example, here are two images with a few differences:

leftImage.jpg:

rightImage.jpg:

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:

import cv2

# load images
image1 = cv2.imread("leftImage.jpg")
image2 = cv2.imread("rightImage.jpg")

# compute difference
difference = cv2.subtract(image1, image2)

# try to 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]

# try to add the red mask to the images to make the differences obvious
diffOverImage1 = image1 + difference
diffOverImage2 = image2 + difference
image1[mask != 255] = [0, 0, 255]
image2[mask != 255] = [0, 0, 255]

# store images
cv2.imwrite('diffOverImage1.png', diffOverImage1)
image1)
cv2.imwrite('diffOverImage2.png', diffOverImage2)
image1)
cv2.imwrite('diff.png', difference)


diff.png:

diffOverImage1.png

diffOverImage2.png

Problem with the current code: The computed mask shows some differences but also contains irrelevant noise. When I add the mask to the image, the 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 not shown only very lightly in red. How do I remove the irrelevant noise and the computed mask, but they should be clearly highlight all the differences in a strong red color?like the other differences.

Input: 2 images with some differences.

Output: 3 images: the two input images but with the differences highlighted (clearly highlighted in a configurable color, no noise), color), and a third image containing only the differences (the mask).

diff.png:

diffOverImage1.png

diffOverImage2.png