赞
踩
from skimage.measure import compare_ssim
#~ import skimage as ssim
import argparse
import imutils
import cv2
imageA = cv2.imread("D:/111test/111.png")
imageB = cv2.imread("D:/111test/444.png")
grayA = cv2.cvtColor(imageA,cv2.COLOR_BGR2GRAY)
grayB = cv2.cvtColor(imageB,cv2.COLOR_BGR2GRAY)
(score,diff) = compare_ssim(grayA,grayB,full = True)
diff = (diff *255).astype("uint8")
print("SSIM:{}".format(score))
thresh = cv2.threshold(diff,0,255,cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
cnts = cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
for c in cnts:
(x,y,w,h) = cv2.boundingRect(c)
cv2.rectangle(imageA,(x,y),(x+w,y+h),(0,0,255),2)
cv2.rectangle(imageB,(x,y),(x+w,y+h),(0,0,255),2)
cv2.imshow("Modified",imageB)
cv2.imwrite("haha2.png",imageB)
cv2.waitKey(0)
到这已经实现两张图片的对比并标识出不同。结果如下所示:(图1图2对比,图3为对标结果
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。