繁体   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