本文主要是解决从两张图中寻找不同之处,类似于“找茬”小游戏。为了比较两张图片的结构化差异,寻找两张图中的不同,引入了Structural Similarity Index 指标。而这个指标直接scikit-image 库中集成,可直接调用。
环境依赖:OpenCV, scikit-image, imutils。
Figure 1: Manually inspecting the difference between two input images (source).可观察左右两张图的不同。
compare_ssim 函数用来计算两张图片之间的结构化相似度。返回两个参数:score和 diff:
The score represents the structural similarity index between the two input images. This value can fall into the range [-1, 1] with a value of one being a “perfect match”.
The diff image contains the actual image differences between the two input images that we wish to visualize. The difference image is currently represented as a floating point data type in the range [0, 1] so we first convert the array to 8-bit unsigned integers in the range [0, 255] before we can further process it using OpenCV.
代码相对简单,不作过多注解。需要指出的是,输入的两张图维度要相同。
源码:
'''
引入依赖包
from skimage.measure import compare_ssim
import argparse
import imutils
import cv2
construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-f", "--first", required=True,
help="first input image")
ap.add_argument("-s", "--second", required=True,
help="second")
args = vars(ap.parse_args())
load the two input images
imageA = cv2.imread(args["first"])
imageB = cv2.imread(args["second"])
convert the images to grayscale
grayA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)
grayB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY)
compute the Structural Similarity Index (SSIM) between the two images, ensuring that the difference image is returned
(score, diff) = compare_ssim(grayA, grayB, full=True)
diff = (diff * 255).astype("uint8")
print("SSIM: {}".format(score))
threshold the difference image, followed by finding contours to
obtain the regions of the two input images that differ
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 = imutils.grab_contours(cnts)
loop over the contours
for c in cnts:
# compute the bounding box of the contour and then draw the
# bounding box on both input images to represent where the two
# images differ
(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)
show the output images
cv2.imshow("Original", imageA)
cv2.imshow("Modified", imageB)
cv2.imshow("Diff", diff)
cv2.imshow("Thresh", thresh)
cv2.waitKey(0)
'''
本文主要来自:原文地址。by Adrian Rosebrock