Search code examples
pythonopencvroi

Compare cropped images python opencv


I want to compare two images to check if they are equal or not, but for that i need to compare a specific region (ROI) of both images. I've cropped the areas i want to compare, but now i would like to know how can i do that process, because i can't directly compare the cropped images. How can i for example get the average pixels values of both cropped images and compare them?

Update: I've solved the situation. Current code:

import cv2
import numpy as np
from skimage.measure import compare_ssim as ssim


def mse(imageA, imageB):
    # the 'Mean Squared Error' between the two images is the sum of the squared difference between the two images;
    err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2) 
    err /= float(imageA.shape[0] * imageA.shape[1])


polarity_ok = cv2.resize(cv2.imread("polarity_OK_edited.jpg"),None,fx=0.2, fy=0.2) #resize the image to be smaller
polarity_nok = cv2.resize(cv2.imread("Polarity_NOK1.JPG"), None,fx=0.2, fy=0.2) #resize the image to be smaller

polarity_ok_cropped = polarity_ok[350:408, 97:111]
polarity_nok_cropped = polarity_nok[350:408, 97:111]

polarity_ok_cropped1 = polarity_ok[359:409, 232:240]
polarity_nok_cropped1 = polarity_nok[359:409, 232:240]

polarity_ok_cropped2 = polarity_ok[118:153, 44:69]
polarity_nok_cropped2 = polarity_nok[118:153, 44:69]

polarity_ok_cropped3 = polarity_ok[94:142, 192:197]
polarity_nok_cropped3 = polarity_nok[94:142, 192:197]

m = mse(polarity_ok_cropped, polarity_nok_cropped)
s = ssim(polarity_ok_cropped, polarity_nok_cropped, multichannel=True)

diff = cv2.subtract(polarity_ok_cropped, polarity_nok_cropped)
result = not np.any(diff)

m1 = mse(polarity_ok_cropped1, polarity_nok_cropped1)
s1 = ssim(polarity_ok_cropped1, polarity_nok_cropped1, multichannel=True)

diff1 = cv2.subtract(polarity_ok_cropped1, polarity_nok_cropped1)
result1 = not np.any(diff1)

m2 = mse(polarity_ok_cropped2, polarity_nok_cropped2)
s2 = ssim(polarity_ok_cropped2, polarity_nok_cropped2, multichannel=True)

diff2 = cv2.subtract(polarity_ok_cropped2, polarity_nok_cropped2)
result2 = not np.any(diff2)

m3 = mse(polarity_ok_cropped2, polarity_nok_cropped2)
s3 = ssim(polarity_ok_cropped2, polarity_nok_cropped2, multichannel=True)

diff3 = cv2.subtract(polarity_ok_cropped3, polarity_nok_cropped3)
result3 = not np.any(diff3)


if (result and result1 and result2 and result3):
    print ("The polarity is correct. Awesome :)")
else:
    print ("Nice try, but the polarity is incorrect. Take another chance!")

Solution

  • You can use the Structural Similarity Index (SSIM) as giving the 2 images as input and returning a score value in the range [-1, 1]. A score of 1 indicating a perfect similarity between 2 input images (In case of both images are equal)

    from skimage.measure import compare_ssim
    (score, diff) = compare_ssim(image1, image2, full=True)
    

    Btw Converting the input images before comparison into grayscale is prefered.