比较裁剪的图像python opencv

时间:2018-12-21 12:30:26

标签: python opencv roi

我想比较两个图像以检查它们是否相等,但是为此,我需要比较两个图像的特定区域(ROI)。 我已经裁剪了要比较的区域,但是现在我想知道如何执行该过程,因为我无法直接比较裁剪后的图像。 例如,如何获取两个裁剪图像的平均像素值并进行比较?

更新:我已经解决了这种情况。 当前代码:

import cv2
import numpy as np
from skimage.measure import compare_ssim as ssim


def mse(imageA, imageB):
    # the 'Mean Squared Error' between the two images is the sum of the squared difference between the two images;
    err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2) 
    err /= float(imageA.shape[0] * imageA.shape[1])


polarity_ok = cv2.resize(cv2.imread("polarity_OK_edited.jpg"),None,fx=0.2, fy=0.2) #resize the image to be smaller
polarity_nok = cv2.resize(cv2.imread("Polarity_NOK1.JPG"), None,fx=0.2, fy=0.2) #resize the image to be smaller

polarity_ok_cropped = polarity_ok[350:408, 97:111]
polarity_nok_cropped = polarity_nok[350:408, 97:111]

polarity_ok_cropped1 = polarity_ok[359:409, 232:240]
polarity_nok_cropped1 = polarity_nok[359:409, 232:240]

polarity_ok_cropped2 = polarity_ok[118:153, 44:69]
polarity_nok_cropped2 = polarity_nok[118:153, 44:69]

polarity_ok_cropped3 = polarity_ok[94:142, 192:197]
polarity_nok_cropped3 = polarity_nok[94:142, 192:197]

m = mse(polarity_ok_cropped, polarity_nok_cropped)
s = ssim(polarity_ok_cropped, polarity_nok_cropped, multichannel=True)

diff = cv2.subtract(polarity_ok_cropped, polarity_nok_cropped)
result = not np.any(diff)

m1 = mse(polarity_ok_cropped1, polarity_nok_cropped1)
s1 = ssim(polarity_ok_cropped1, polarity_nok_cropped1, multichannel=True)

diff1 = cv2.subtract(polarity_ok_cropped1, polarity_nok_cropped1)
result1 = not np.any(diff1)

m2 = mse(polarity_ok_cropped2, polarity_nok_cropped2)
s2 = ssim(polarity_ok_cropped2, polarity_nok_cropped2, multichannel=True)

diff2 = cv2.subtract(polarity_ok_cropped2, polarity_nok_cropped2)
result2 = not np.any(diff2)

m3 = mse(polarity_ok_cropped2, polarity_nok_cropped2)
s3 = ssim(polarity_ok_cropped2, polarity_nok_cropped2, multichannel=True)

diff3 = cv2.subtract(polarity_ok_cropped3, polarity_nok_cropped3)
result3 = not np.any(diff3)


if (result and result1 and result2 and result3):
    print ("The polarity is correct. Awesome :)")
else:
    print ("Nice try, but the polarity is incorrect. Take another chance!")

3 个答案:

答案 0 :(得分:1)

如果您确切知道要比较的对象在哪里,使用OpenCV比较两个图像的简单快捷方法是使用calcHistogram()提取每个通道(RGB或HSV)的直方图,然后使用{ {1}}。

更多信息和示例可以在这里找到:Histogram comparsion.

答案 1 :(得分:0)

您可以使用结构相似性索引(SSIM)作为输入2张图像并返回[-1,1]范围内的得分值。得分为1表示2个输入图像之间完全相似(如果两个图像相等)

from skimage.measure import compare_ssim
(score, diff) = compare_ssim(image1, image2, full=True)

最好在比较之前将输入图像转换为灰度。

答案 2 :(得分:0)

另一种相同的方法:

from PIL import Image
import math, operator

i1 = Image.open('./image1.png')
i2 = Image.open('./image2.png')

#this will resize any format of image file
assert i1.mode == i2.mode, "Different kinds of images."
assert i1.size == i2.size, "Different sizes."
 
pairs = zip(i1.getdata(), i2.getdata())
if len(i1.getbands()) == 1:
    # for gray-scale jpegs
    dif = sum(abs(p1-p2) for p1,p2 in pairs)
else:
    dif = sum(abs(c1-c2) for p1,p2 in pairs for c1,c2 in zip(p1,p2))
 
ncomponents = i1.size[0] * i1.size[1] * 3
print ("Difference (percentage):", (dif / 255.0 * 100) / ncomponents)

您需要安装枕头。 希望这会帮助你。