28-Image Difference Demo

Description 

Image difference in computer vision refers to the process of finding the dissimilarity or changes between two images. This can be useful in various applications, such as motion detection, change detection, or identifying anomalies in images

Section 1-Image difference using Matlab

If you can add to images, you can subtract them as well. The difference between two images is simply one image minus the other. It might be hard to understand at first what’s going on. Greater values in the difference image signify greater difference between the two images. Brighter areas in this result indicate where the two images differ more.

Note that this is dolphin minus bicycle. Here the order mattered. Bicycle minus dolphin gives us a different result. This makes sense, as what the difference operation is doing is simply subtracting pixels in corresponding locations. If two such pixel values are a and b, then a minus b is different from b minus a. But when thinking about the difference between two images, we often don't care about which one is greater, and which one is less? Note that b minus a is simply a minus b negated. When thinking about the difference between two images, we often don't care about the sign of this difference, only the magnitude. That is, we're interested in the absolute difference between two images. For that you use the Octave ABS, or ABS function.

 Let's see how different the two results are. Wait a second. These two don't look different. In fact, they're exactly the same. What's going on? Let's take a closer look at our code. Especially this line. Let's say two values being subtracted are 20 from bicycle and 56 from dolphin. Theoretically the result should be minus 36. But remember uint8? These images can only represent numbers between zero and 255. So what happens here? It gets truncated to zero. Notice that even in the absolute difference case, the subtraction is performed first. This intermediate result is the same as the original difference. The numbers here are already between zero and 255. So the absolute value operator doesn't make any difference. So what can we do about this. 

Section 2- Image difference using Python 

Here's a simple example of how you can perform image difference using Python and the OpenCV library python

import cv2
import numpy as np
# Load two images
image1 = cv2.imread('image1.jpg')
image2 = cv2.imread('image2.jpg')
# Convert images to grayscale
gray1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
# Compute absolute difference between the two images
difference = cv2.absdiff(gray1, gray2)
# Apply thresholding to highlight the differences
_, thresholded = cv2.threshold(difference, 30, 255, cv2.THRESH_BINARY)
# Find contours of the differences
contours, _ = cv2.findContours(thresholded, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Draw rectangles around the differing regions
for contour in contours:
x, y, w, h = cv2.boundingRect(contour)
cv2.rectangle(image1, (x, y), (x + w, y + h), (0, 0, 255), 2)
# Display the result
cv2.imshow('Difference', image1)
cv2.waitKey(0)
cv2.destroyAllWindows()

References

Last modified: Saturday, 9 December 2023, 9:50 AM