Comparing images or movies

This revision is from 2024/02/09 16:29. You can Restore it.

With Python, comparing image using Structural Similarity Index (SSIM) or using a perceptual hashing algorithm like pHash.

{code}

pip install scikit-image opencv-python

pip install python-phash

{/code}

Comparing Images using SSIM (Structural Similarity Index):

{code}

import cv2

import numpy as np

from skimage import measure

  1. Load images

img1 = cv2.imread('image1.jpg', 0) # grayscale

img2 = cv2.imread('image2.jpg', 0) # grayscale

  1. Ensure both images have the same dimensions

assert img1.shape == img2.shape, "Images must have the same dimensions"

  1. Compute SSIM

ssim = measure.compare_ssim(img1, img2)

print("SSIM:", ssim)

{/code}

Comparing Images using pHash (Perceptual Hashing):

{code}

import phash

  1. Load images

img1 = cv2.imread('image1.jpg')

img2 = cv2.imread('image2.jpg')

  1. Compute pHash

hash1 = phash.dct_image_hash(img1)

hash2 = phash.dct_image_hash(img2)

  1. Compare hashes

distance = hamming_distance(hash1, hash2)

print("Hamming Distance:", distance)

{/code}

  

📝 📜 ⏱️ ⬆️