原文地址:-Image Difference with Opencv and Python-
本文是原作者之前提到的SSIM方法的一種延申,本文主要利用Opencv和Python依據(jù)SSIM來(lái)實(shí)現(xiàn)兩幅圖片不同之處的可視化。運(yùn)行環(huán)境python3/opencv3
利用原作者的圖片,通過(guò)調(diào)整閾值,本文可以得到非常好的結(jié)果颇蜡,但是使用網(wǎng)絡(luò)上的找茬圖片,存在很多的噪聲辆亏,效果不是很好风秤,原因多在于圖片經(jīng)過(guò)了裁剪、旋轉(zhuǎn)扮叨、移位缤弦、壓縮等操作,后續(xù)繼續(xù)研究另外一位作者cangyan的思路和方法彻磁。
原文效果圖
1.計(jì)算不同
我們?nèi)庋劭梢暂p松識(shí)別下面兩幅圖的不同之處:右圖右下角缺少一個(gè)logo碍沐。
我們可以立刻找到兩幅圖片的不同,也許要花一點(diǎn)點(diǎn)時(shí)間衷蜓,但是當(dāng)兩幅圖片的差別特別細(xì)微的時(shí)候累提,我們?nèi)庋蹘缀跏欠直娌怀龅摹?/p>
那么,識(shí)別圖片的不同為什么這么重要呢磁浇?
比較常見(jiàn)的一個(gè)問(wèn)題就是釣魚(yú)網(wǎng)站斋陪,攻擊者利用幾乎一模一樣的圖片制作一個(gè)高仿的銀行網(wǎng)站,迷惑那些毫無(wú)戒心的網(wǎng)友。
對(duì)比網(wǎng)站logo以及及時(shí)熟知網(wǎng)站的用戶界面在很大程度上可以防止釣魚(yú)攻擊无虚。相比于兩幅圖的比較鞍匾,釣魚(yú)網(wǎng)站檢測(cè)系統(tǒng)將更為復(fù)雜,但我們?nèi)匀豢梢允褂靡延械闹R(shí)來(lái)判別圖像是否做了手腳骑科。
讀入圖片
原文代碼:
# import the necessary packages
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)
查找輪廓函數(shù)cv2.findcontours
需要圖片是二值圖橡淑,所以先將圖片由BGR轉(zhuǎn)為gray;
接下來(lái)咆爽,計(jì)算兩幅圖之間的SSIM梁棠,由于
diff
的值范圍[0, 1],為了可以用Opencv進(jìn)一步操作斗埂,將其轉(zhuǎn)換為[0, 255]符糊;參考代碼:
# 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))
然后,調(diào)整閾值呛凶,獲得二值圖男娄,再利用Opencv找到兩幅圖內(nèi)容差異diff
的輪廓,并用矩形標(biāo)出來(lái)漾稀;
參考代碼:
# 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)
原文作者考慮的甚是周全模闲,考慮到用戶的Opencv版本可能不同,使用了imutils.grab_contours()
函數(shù)崭捍,具體可以參考imutils.grab_contours
源碼
# if the length the contours tuple returned by cv2.findContours
# is '2' then we are using either OpenCV v2.4, v4-beta, or
# v4-official
if len(cnts) == 2:
cnts = cnts[0]
# if the length of the contours tuple is '3' then we are using
# either OpenCV v3, v4-pre, or v4-alpha
elif len(cnts) == 3:
cnts = cnts[1]
# otherwise OpenCV has changed their cv2.findContours return
# signature yet again and I have no idea WTH is going on
else:
raise Exception(("Contours tuple must have length 2 or 3, "
"otherwise OpenCV changed their cv2.findContours return "
"signature yet again. Refer to OpenCV's documentation "
"in that case"))
# return the actual contours array
return 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)
2.圖片不同可視化
使用下面的命令实夹,可以很好的發(fā)現(xiàn)兩幅圖片的不同:
$ python image_diff.py --first images/original_02.png --second images/modified_02.png
3.個(gè)人測(cè)試
詳細(xì)代碼參考:myGithub
使用網(wǎng)絡(luò)圖片2,得到了400多個(gè)輪廓粒梦,畫出輪廓面積最大的10個(gè)結(jié)果亮航,可以看到得到了較好的判定結(jié)果,除了發(fā)現(xiàn)不同之外匀们,還多出了3處缴淋;
使用網(wǎng)絡(luò)圖片1,效果相比差點(diǎn)昼蛀,雖然也發(fā)現(xiàn)了稍有的幾處不同宴猾,但誤判的區(qū)域更占多數(shù)圆存,主要原因更多是圖片的質(zhì)量問(wèn)題叼旋,右邊稍有壓縮痕跡。
總結(jié)
利用Opencv/Python/Skimage計(jì)算的SSIM
沦辙,我們實(shí)現(xiàn)了兩幅圖之間的差異可視化效果夫植,要得到完全正確的結(jié)果,前提要保證變動(dòng)的部分是嚴(yán)格在原始圖上進(jìn)行的操作,而且內(nèi)容要重合好详民,這樣可以得到像原作者文中的效果延欠。