-
-
Save gonzalo123/df3e43477f8627ecd1494d138eae03ae to your computer and use it in GitHub Desktop.
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; | |
# NOTE: the two images must have the same dimension | |
err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2) | |
err /= float(imageA.shape[0] * imageA.shape[1]) | |
# return the MSE, the lower the error, the more "similar" | |
# the two images are | |
def diff_remove_bg(img0, img, img1): | |
d1 = diff(img0, img) | |
d2 = diff(img, img1) | |
return cv2.bitwise_and(d1, d2) | |
x1 = cv2.imread("images/xx1.jpg") | |
x2 = cv2.imread("images/xx12.jpg") | |
x1 = cv2.cvtColor(x1, cv2.COLOR_BGR2GRAY) | |
x2 = cv2.cvtColor(x2, cv2.COLOR_BGR2GRAY) | |
absdiff = cv2.absdiff(x1, x2) | |
cv2.imwrite("images/absdiff.png", absdiff) | |
diff = cv2.subtract(x1, x2) | |
result = not np.any(diff) | |
m = mse(x1, x2) | |
s = ssim(x1, x2) | |
print "mse: %s, ssim: %s" % (m, s) | |
if result: | |
print "The images are the same" | |
else: | |
cv2.imwrite("images/diff.png", diff) | |
print "The images are different" |
When I try to change imread files as .png .
I got error like this. can you please help me
absdiff = cv2.absdiff(x1, x2)
error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\core\src\arithm.cpp:669: error: (-209:Sizes of input arguments do not match) The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array' in function 'cv::arithm_op'
Could you pls. modify the above code for comparing images from two different folders. Code will compare first image from first folder and second image from second folder and then keep comparing all images one by one and share the result in a third folder. I am new to python and Open CV and tried but didn't work. Could you help here...
Hi Arvinder,
Please add your two input folders path here:
x1 = cv2.imread("images/xx1.jpg")
x2 = cv2.imread("images/xx12.jpg")
Also, add your output folder path here:
cv2.imwrite("images/absdiff.png", absdiff)
cv2.imwrite("images/diff.png", diff)
can I change the imread files as .png and print the output