1. 程式人生 > 程式設計 >利用OpenCV和Python實現查詢圖片差異

利用OpenCV和Python實現查詢圖片差異

使用OpenCV和Python查詢圖片差異

flyfish

方法1 均方誤差的演算法(Mean Squared Error,MSE)

下面的一些表達與《TensorFlow - 協方差矩陣》式子表示式一樣的

擬合 誤差平方和( sum of squared errors)

residual sum of squares (RSS),also known as the sum of squared residuals (SSR) or the sum of squared errors of prediction (SSE),
also known as 就我們所說的
RSS, SSR ,SSE表達的是一個意思

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
 return err

方法2 SSIM

​structural similarity index measurement (SSIM) system

一種衡量兩幅影象結構相似度的新指標,其值越大越好,最大為1。

新建一個Python檔案,命名為 image_diff.py

原文

Image Difference with OpenCV and Python

原理

根據引數讀取兩張圖片並轉換為灰度:

使用SSIM計算兩個影象之間的差異,這種方法已經在scikit-image 庫中實現

在兩個影象之間的不同部分繪製矩形邊界框。

程式碼如下 已編譯通過

from skimage.measure import compare_ssim
#~ import skimage as 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",help="second")
args = vars(ap.parse_args())
# load the two input images
imageA = cv2.imread(args["first"])
imageB = cv2.imread(args["second"])
'''
imageA = cv2.imread("E:\\1.png")
imageB = cv2.imread("E:\\2.png")
'''
# 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
#​structural similarity index measurement (SSIM) system一種衡量兩幅影象結構相似度的新指標,其值越大越好,最大為1。

(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,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]

# 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,255),2)
 cv2.rectangle(imageB,2)

# show the output images
cv2.imshow("Original",imageA)
cv2.imshow("Modified",imageB)
cv2.imshow("Diff",diff)
cv2.imshow("Thresh",thresh)
cv2.waitKey(0)

使用方法

python image_diff.py –first original.png –second images/modified.png 

如果不想使用引數將引數程式碼部分直接變成

imageA = cv2.imread(“E:\1.png”) 
imageB = cv2.imread(“E:\2.png”)

以上這篇利用OpenCV和Python實現查詢圖片差異就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。