比较裁剪后的图像 python opencv
Compare cropped images python opencv
我想比较两幅图像以检查它们是否相等,但为此我需要比较两幅图像的特定区域 (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!")
您可以使用 结构相似性指数 (SSIM) 将 2 张图像作为输入并返回 [-1, 1] 范围内的分数值。得分为 1 表示 2 个输入图像之间非常相似(如果两个图像相等)
from skimage.measure import compare_ssim
(score, diff) = compare_ssim(image1, image2, full=True)
顺便说一句,在比较之前将输入图像转换为灰度是首选。
如果您确切知道要比较的对象在哪里,使用 OpenCV 比较两个图像的简单快速方法是使用 calcHistogram()
为每个通道(RGB 或 HSV)提取直方图,然后比较它们使用 compareHist()
.
可以在此处找到更多信息和示例:Histogram comparsion.
另一种方法可以做到同样的事情:
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)
您需要安装枕头。
希望这会帮助你。
我想比较两幅图像以检查它们是否相等,但为此我需要比较两幅图像的特定区域 (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!")
您可以使用 结构相似性指数 (SSIM) 将 2 张图像作为输入并返回 [-1, 1] 范围内的分数值。得分为 1 表示 2 个输入图像之间非常相似(如果两个图像相等)
from skimage.measure import compare_ssim
(score, diff) = compare_ssim(image1, image2, full=True)
顺便说一句,在比较之前将输入图像转换为灰度是首选。
如果您确切知道要比较的对象在哪里,使用 OpenCV 比较两个图像的简单快速方法是使用 calcHistogram()
为每个通道(RGB 或 HSV)提取直方图,然后比较它们使用 compareHist()
.
可以在此处找到更多信息和示例:Histogram comparsion.
另一种方法可以做到同样的事情:
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)
您需要安装枕头。 希望这会帮助你。