如何使用OpenCV在python中比较不同大小的相同图像的相似性?



我使用下面的代码用三个浏览器捕获了相同的图像(图像的高度根据添加和删除特定页面的内容动态更改)。

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

现在我想比较这三张相同的图片。为此,我使用了structural_similarity。但是由于大小不同,我得到了错误。

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。调整图片大小,如下图

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

我尝试使用imagehash也

import imagehash
hash0 = imagehash.average_hash(Image.open('planoverview_Chrome.jpg')) 
hash1 = imagehash.average_hash(Image.open('planoverview_Chromemodified.jpg')) 
cutoff = 1  # maximum bits that could be different between the hashes. 
print(hash0)
print(hash1)
if hash0 - hash1 < cutoff:
print('images are similar')
else:
print('images are not similar')

O/p:

0307070307ffffff
0307070307ffffff
images are similar

报告两个图像相同。

问题:

我已经使用了set (driver)。Set_window_size(1920, 6080))-之前保存屏幕短&比较了相似性,它在Egde &铬。但这里的问题是,当页面内容增长时,图像不能按预期捕获。

之后,我在转换为灰度之前调整大小,并比较了三张图像。但是SIMM没有检测到微小的变化,要么报告所有的都是相同的,要么图像完全不同。

是否有任何有效的我可以比较不同大小的图像在python中使用一些lib &告诉他们是相同的还是不同的?

我的方法是使用规范化的相互关系。如果不同图像的内容相同,则需要首先调整图像的大小,使其具有相同的尺寸。OpenCV提供

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

作为method,我建议使用cv.TM_CCORR_NORMED

最新更新