deeplearning-cv-notes
deeplearning-cv-notes copied to clipboard
判断两张图片是否相同
第一种写法:
一、
import numpy as np
import cv2
global n
zong=0
n = 0
img1 = cv2.imread('1.png', cv2.IMREAD_GRAYSCALE)
img2 = cv2.imread('22.png', cv2.IMREAD_GRAYSCALE)
height, width = img1.shape
for line in range(height):
for pixel in range(width):
if img1[line][pixel] != img2[line][pixel]:
n = n + 1
print (n)
第二种写法:
import cv2
import numpy as np
image1 = cv2.imread("1.png")
image2 = cv2.imread("22.png")
difference = cv2.subtract(image1, image2)
result = not np.any(difference) #if difference is all zeros it will return False
if result is True:
print("两张图片一样")
else:
cv2.imwrite("result.jpg", difference)
print ("两张图片不一样")