亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

利用OpenCV和Python實現(xiàn)查找圖片差異

 更新時間:2019年12月19日 10:17:58   作者:flyfish1986  
今天小編就為大家分享一篇利用OpenCV和Python實現(xiàn)查找圖片差異,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

使用OpenCV和Python查找圖片差異

flyfish

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

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

擬合 誤差平方和( 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

一種衡量兩幅圖像結(jié)構(gòu)相似度的新指標(biāo),其值越大越好,最大為1。

新建一個Python文件,命名為 image_diff.py

原文

Image Difference with OpenCV and Python

原理

根據(jù)參數(shù)讀取兩張圖片并轉(zhuǎn)換為灰度:

使用SSIM計算兩個圖像之間的差異,這種方法已經(jīng)在scikit-image 庫中實現(xiàn)

在兩個圖像之間的不同部分繪制矩形邊界框。

代碼如下 已編譯通過

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", required=True,
 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一種衡量兩幅圖像結(jié)構(gòu)相似度的新指標(biāo),其值越大越好,最大為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, 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]

# 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)

使用方法

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

如果不想使用參數(shù)將參數(shù)代碼部分直接變成

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

以上這篇利用OpenCV和Python實現(xiàn)查找圖片差異就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python實現(xiàn)圖片變亮或者變暗的方法

    python實現(xiàn)圖片變亮或者變暗的方法

    這篇文章主要介紹了python實現(xiàn)圖片變亮或者變暗的方法,涉及Python中Image模塊操作圖片的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • Python基于正則表達式實現(xiàn)檢查文件內(nèi)容的方法【文件檢索】

    Python基于正則表達式實現(xiàn)檢查文件內(nèi)容的方法【文件檢索】

    這篇文章主要介紹了Python基于正則表達式實現(xiàn)檢查文件內(nèi)容的方法,可實現(xiàn)針對文件中import強制依賴的文件關(guān)系檢索,涉及Python文件目錄的遍歷及正則匹配相關(guān)操作技巧,需要的朋友可以參考下
    2017-08-08
  • Python3基礎(chǔ)之條件與循環(huán)控制實例解析

    Python3基礎(chǔ)之條件與循環(huán)控制實例解析

    這篇文章主要介紹了Python3基礎(chǔ)的條件與循環(huán)控制,需要的朋友可以參考下
    2014-08-08
  • 超詳細(xì)注釋之OpenCV更改像素與修改圖像通道

    超詳細(xì)注釋之OpenCV更改像素與修改圖像通道

    這篇文章主要介紹了OpenCV更改像素與修改圖像通道,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • Python中操作MySQL入門實例

    Python中操作MySQL入門實例

    這篇文章主要介紹了Python中操作MySQL入門實例,本文講解了安裝、打開數(shù)據(jù)庫連接、插入數(shù)據(jù)、查詢數(shù)據(jù)、刪除數(shù)據(jù)等操作,需要的朋友可以參考下
    2015-02-02
  • Django自帶的用戶驗證系統(tǒng)實現(xiàn)

    Django自帶的用戶驗證系統(tǒng)實現(xiàn)

    這篇文章主要介紹了Django自帶的用戶驗證系統(tǒng)實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • python模塊之StringIO使用示例

    python模塊之StringIO使用示例

    這篇文章主要介紹了python模塊之StringIO使用示例,本文直接給出示例代碼,需要的朋友可以參考下
    2015-04-04
  • Django admin實現(xiàn)圖書管理系統(tǒng)菜鳥級教程完整實例

    Django admin實現(xiàn)圖書管理系統(tǒng)菜鳥級教程完整實例

    這篇文章主要介紹了Django admin實現(xiàn)圖書管理系統(tǒng)菜鳥級教程完整實例,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • 淺談scrapy 的基本命令介紹

    淺談scrapy 的基本命令介紹

    下面小編就為大家?guī)硪黄獪\談scrapy 的基本命令介紹。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • Python文件目錄操作常用模塊的使用詳解

    Python文件目錄操作常用模塊的使用詳解

    Python中用于文件目錄操作的常用模塊有os, shutil,pathlib等。os模塊提供的就是各種 Python 程序與操作系統(tǒng)進行交互的接口。shutil模塊是對os模塊的補充,主要針對文件的拷貝、刪除、移動、壓縮和解壓操作
    2022-07-07

最新評論