Sitemap

Image compare using OpenCV Python

1 min readAug 11, 2019

--

How we can able to use OpenCV to compare 2 images?

some measure of how similar or dissimilar of two images are relative to each other.

  1. Subtraction

import numpy as np
import cv2

img= cv2.imread(‘sample1.jpg’)
img1= cv2.imread(‘sample2.jpg’)

#compare image
diff_img=cv2.subtract(img,img1)

Get Jinesh John’s stories in your inbox

Join Medium for free to get updates from this writer.

print(np.sum(diff_img))

if this print 0 which means its same or it is different

w,h,c=diff_img.shape

Find max different from that image

total_pixel_value_count=w*h*c*255

Find the silimarity between this images %
percentage_match=(total_pixcel_value_count -np.sum(res))/total_pixcel_value_count*100

print(percentage_match)

--

--