How to compare two images in OpenCV Python?

To compare two images, we use the Mean Square Error (MSE) of the pixel values of the two images. Similar images will have less mean square error value. Using this method, we can compare two images having the same height, width and number of channels.

What is Mean Square Error?

Mean Square Error (MSE) measures the average squared differences between corresponding pixels of two images. Lower MSE values indicate greater similarity between images.

Steps to Compare Images

You can use the following steps to compare two images using OpenCV ?

Step 1: Import the required libraries. In all the following Python examples, the required libraries are OpenCV and NumPy.

import cv2
import numpy as np

Step 2: Read the input images using cv2.imread() and convert to grayscale. The height, width and number of channels must be the same.

# Create sample images for demonstration
img1 = np.random.randint(0, 255, (100, 100), dtype=np.uint8)
img2 = img1.copy()
img2[10:20, 10:20] = 255  # Add some difference

print(f"Image 1 shape: {img1.shape}")
print(f"Image 2 shape: {img2.shape}")
Image 1 shape: (100, 100)
Image 2 shape: (100, 100)

Step 3: Define a function to compute the Mean Squared Error between two images.

def mse(img1, img2):
    h, w = img1.shape
    diff = cv2.subtract(img1, img2)
    err = np.sum(diff**2)
    mse_value = err / (float(h * w))
    return mse_value, diff

# Calculate MSE
error, difference = mse(img1, img2)
print(f"Mean Square Error: {error:.2f}")
Mean Square Error: 255.00

Example 1: Basic Image Comparison

In this example, we compare two similar images and calculate their MSE ?

import cv2
import numpy as np

# Create two sample images
img1 = np.ones((50, 50), dtype=np.uint8) * 100
img2 = np.ones((50, 50), dtype=np.uint8) * 105

def mse(img1, img2):
    h, w = img1.shape
    diff = cv2.subtract(img1, img2)
    err = np.sum(diff**2)
    mse_value = err / (float(h * w))
    return mse_value, diff

error, diff = mse(img1, img2)
print(f"Image matching Error between the two images: {error:.2f}")
print(f"Maximum difference value: {np.max(diff)}")
print(f"Minimum difference value: {np.min(diff)}")
Image matching Error between the two images: 25.00
Maximum difference value: 5
Minimum difference value: 5

Example 2: Comparing Multiple Images

In this example, we compare three different images to see which ones are most similar ?

import cv2
import numpy as np

# Create three sample images
img1 = np.ones((50, 50), dtype=np.uint8) * 100
img2 = np.ones((50, 50), dtype=np.uint8) * 102  # Similar to img1
img3 = np.ones((50, 50), dtype=np.uint8) * 150  # Different from img1 and img2

def calculate_mse(img1, img2):
    h, w = img1.shape
    diff = cv2.subtract(img1, img2)
    err = np.sum(diff**2)
    mse_value = err / (float(h * w))
    return mse_value, diff

# Compare all pairs
mse_12, diff_12 = calculate_mse(img1, img2)
mse_13, diff_13 = calculate_mse(img1, img3)
mse_23, diff_23 = calculate_mse(img2, img3)

print(f"MSE between image 1 and image 2: {mse_12:.2f}")
print(f"MSE between image 1 and image 3: {mse_13:.2f}")
print(f"MSE between image 2 and image 3: {mse_23:.2f}")

# Determine most similar pair
min_mse = min(mse_12, mse_13, mse_23)
if min_mse == mse_12:
    print("Images 1 and 2 are most similar")
elif min_mse == mse_13:
    print("Images 1 and 3 are most similar")
else:
    print("Images 2 and 3 are most similar")
MSE between image 1 and image 2: 4.00
MSE between image 1 and image 3: 2500.00
MSE between image 2 and image 3: 2304.00
Images 1 and 2 are most similar

Key Points

  • Lower MSE values indicate more similar images
  • Images must have the same dimensions for comparison
  • Converting to grayscale simplifies comparison and improves performance
  • MSE is sensitive to pixel intensity differences

Comparison Methods

Method Best For Range
MSE Overall similarity 0 to ? (0 = identical)
SSIM Structural similarity -1 to 1 (1 = identical)
Histogram Comparison Color distribution 0 to 1 (depends on method)

Conclusion

Mean Square Error (MSE) provides a simple and effective way to compare image similarity in OpenCV. Lower MSE values indicate greater similarity, making it useful for image matching and quality assessment tasks.

---
Updated on: 2026-03-26T22:03:02+05:30

77K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements