簡體   English   中英

如何使用 OpenCV 在 python 中比較不同大小的相同圖像的相似度?

[英]How to compare the similarity of same image with different size in python using OpenCV?

我使用以下代碼使用三個瀏覽器捕獲了相同的圖像(圖像的高度根據內容的添加和刪除動態變化)。

ele = driver.find_element_by_xpath('//div[@id="content" and @class="active bg-forms bg-forms-light   "]')
time.sleep(2)
total_height = ele.size["height"]+100
print(total_height)
driver.set_window_size(1920, total_height)
time.sleep(5)
print(driver.get_window_size())

現在我想比較這三個圖像是否相同。 為此,我使用了結構相似性。 但由於尺寸不同,我得到了錯誤。

from skimage.metrics import structural_similarity as compare_ssim
import argparse
import imutils
import cv2

# load the two input images
imageA = cv2.imread("Test_chrome.jpg")
imageB = cv2.imread("Test_edge.jpg")
imageC = cv2.imread("Test_fire.jpg")

print(imageA.shape)
print(imageB.shape)
print(imageC.shape)
print(imageA.size)
print(imageB.size)
print(imageC.size)
# convert the images to grayscale
grayA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)
grayB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY)
grayC = cv2.cvtColor(imageC, cv2.COLOR_BGR2GRAY)
print(grayA.size)
print(grayB.size)
print(grayC.size)
# compute the Structural Similarity Index (SSIM) between the two
# images, ensuring that the difference image is returned
(scoreaandb, diffaandb) = compare_ssim(grayA, grayB, full=True)
diffaandb = (diffaandb * 255).astype("uint8")
print("SSIMaandb: {}".format(scoreaandb))
(scorebandc, diffbandc) = compare_ssim(grayB, grayC, full=True)
diffbandc = (diffbandc * 255).astype("uint8")
print("SSIMbandc: {}".format(scorebandc))
(scoreaandc, diffaandc) = compare_ssim(grayA, grayC, full=True)
diffaandc = (diffaandc * 255).astype("uint8")
print("SSIMaandc: {}".format(scoreaandc))

輸出:

(5956, 1920, 3)
(7306, 1920, 3)
(5994, 1908, 3)
34306560
42082560
34309656
11435520
14027520
11436552

我使用了 CV2.Resized 圖片如下

from skimage.metrics import structural_similarity as compare_ssim
import argparse
import imutils
import cv2

# load the two input images
imageA = cv2.imread("Test_Chrome.jpg")
imageB = cv2.imread("Test_edge.jpg")
imageC = cv2.imread("Test_fire.jpg")

print(imageA.shape)
print(imageB.shape)
print(imageC.shape)
print(imageA.size)
print(imageB.size)
print(imageC.size)

Resized_ImageB = cv2.resize(imageB,(imageA.shape[1],imageA.shape[0]))
Resized_ImageC = cv2.resize(imageC,(imageA.shape[1],imageA.shape[0]))
# convert the images to grayscale
grayA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)
grayB = cv2.cvtColor(Resized_ImageB, cv2.COLOR_BGR2GRAY)
grayC = cv2.cvtColor(Resized_ImageC, cv2.COLOR_BGR2GRAY)
print(grayA.size)
print(grayB.size)
print(grayC.size)
# compute the Structural Similarity Index (SSIM) between the two
# images, ensuring that the difference image is returned
(scoreaandb, diffaandb) = compare_ssim(grayA, grayB, full=True)
diffaandb = (diffaandb * 255).astype("uint8")
print("SSIMaandb: {}".format(scoreaandb))
(scorebandc, diffbandc) = compare_ssim(grayB, grayC, full=True)
diffbandc = (diffbandc * 255).astype("uint8")
print("SSIMbandc: {}".format(scorebandc))
(scoreaandc, diffaandc) = compare_ssim(grayA, grayC, full=True)
diffaandc = (diffaandc * 255).astype("uint8")
print("SSIMaandc: {}".format(scoreaandc))

問題:

我已經使用 set (driver.set_window_size(1920, 6080))- 在保存屏幕簡短並比較相似性之前,它工作正常。 但是這里的問題是當頁面內容增長時,圖像沒有按預期捕獲。

當我調整大小並比較這三個圖像時,即使那里幾乎沒有細微的變化。 SIMM 分數顯示所有三個相同。

有什么有效的方法我可以使用一些lib比較python中不同大小的圖像嗎?

我的方法是使用歸一化互相關。 如果不同圖像的內容相同,則需要先調整圖像大小以具有相同的尺寸。 OpenCV 提供

void cv::matchTemplate (InputArray image, InputArray templ, OutputArray result, int method, InputArray mask=noArray())

作為我建議使用cv.TM_CCORR_NORMEDmethod

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM