如何在OpenCV Python中比较两张图像?
为了比较两张图像,我们使用两张图像像素值的均方误差 (MSE)。相似的图像将具有较小的均方误差值。使用此方法,我们可以比较具有相同高度、宽度和通道数的两张图像。
步骤
您可以使用以下步骤来使用OpenCV比较两张图像:
导入所需的库。在以下所有Python示例中,所需的Python库是OpenCV。请确保您已安装它。
import cv2
使用cv2.imread()读取输入图像并将其转换为灰度图像。图像的高度、宽度和通道数必须相同。
img1 = cv2.imread('panda.png') img2 = cv2.imread('panda1.png') img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
定义一个函数来计算两张图像之间的均方误差。
def mse(img1, img2): h, w = img1.shape diff = cv2.subtract(img1, img2) err = np.sum(diff**2) mse = err/(float(h*w)) return mse
计算图像之间的均方误差(匹配误差)。
error = mse(img1, img2)
打印图像匹配误差 (mse) 并显示图像差异。
print("Image matching Error between the two images:", mse) cv2.imshow("Contour", img) cv2.waitKey(0) cv2.destroyAllWindows()
打印结果值,图像形状匹配指标。值越低,匹配度越高。
print("Matching Image 1 with Image 2:", ret12)
让我们来看一些示例以便更好地理解。
我们将在下面的示例中使用以下图像作为输入文件。
示例1
在这个例子中,我们创建了一个简单的具有四层且没有前向函数的人工神经网络。
# import required libraries import cv2 import numpy as np # load the input images img1 = cv2.imread('panda.jpg') img2 = cv2.imread('panda1.jpg') # convert the images to grayscale img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) # define the function to compute MSE between two images def mse(img1, img2): h, w = img1.shape diff = cv2.subtract(img1, img2) err = np.sum(diff**2) mse = err/(float(h*w)) return mse, diff error, diff = mse(img1, img2) print("Image matching Error between the two images:",error) cv2.imshow("difference", diff) cv2.waitKey(0) cv2.destroyAllWindows()
输出
执行后,它将在控制台上产生以下输出:
Image matching Error between the two images: 3.0696934396076028
我们将看到以下窗口显示两张图像之间的差异:
示例2
在这个Python程序中,我们比较三张图像。
import cv2 import numpy as np import matplotlib.pyplot as plt img1 = cv2.imread('panda.jpg') img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) h, w = img1.shape img2 = cv2.imread('panda1.jpg') img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) img3 = cv2.imread('bike.jpg') img3 = cv2.cvtColor(img3, cv2.COLOR_BGR2GRAY) def error(img1, img2): diff = cv2.subtract(img1, img2) err = np.sum(diff**2) mse = err/(float(h*w)) msre = np.sqrt(mse) return mse, diff match_error12, diff12 = error(img1, img2) match_error13, diff13 = error(img1, img3) match_error23, diff23 = error(img2, img3) print("Image matching Error between image 1 and image 2:",match_error12) print("Image matching Error between image 1 and image 3:",match_error13) print("Image matching Error between image 2 and image 3:",match_error23) plt.subplot(221), plt.imshow(diff12,'gray'),plt.title("image1 - Image2"),plt.axis('off') plt.subplot(222), plt.imshow(diff13,'gray'),plt.title("image1 - Image3"),plt.axis('off') plt.subplot(223), plt.imshow(diff23,'gray'),plt.title("image2 - Image3"),plt.axis('off') plt.show()
输出
执行上述Python程序后,将在控制台上产生以下输出:
Image matching Error between image 1 and image 2: 3.0696934396076028 Image matching Error between image 1 and image 3: 23.37356529736358 Image matching Error between image 2 and image 3: 24.15752299202943
我们将看到以下窗口,显示图像之间的差异:
请注意,图像1和图像2之间的匹配误差小于图像1和3之间的匹配误差以及图像2和3之间的匹配误差。因此,图像1和图像2更相似。
广告