使用 Python-OpenCV 测量图像之间的相似度
先决条件: Python OpenCV
假设我们有两个数据图像和一个测试图像。让我们在Python中使用Python和 OpenCV 库找出哪个数据图像与测试图像更相似。
让我们首先加载图像并找出图像的直方图。
导入库
import cv2
导入图像数据
image = cv2.imread('test.jpg')
转换为灰度图像
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
寻找直方图
histogram = cv2.calcHist([gray_image], [0],
None, [256], [0, 256])
例子:
使用的图像:
数据1.jpg
数据2.jpg
测试.jpg
Python3
import cv2
# test image
image = cv2.imread('cat.jpg')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
histogram = cv2.calcHist([gray_image], [0],
None, [256], [0, 256])
# data1 image
image = cv2.imread('cat.jpeg')
gray_image1 = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
histogram1 = cv2.calcHist([gray_image1], [0],
None, [256], [0, 256])
# data2 image
image = cv2.imread('food.jpeg')
gray_image2 = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
histogram2 = cv2.calcHist([gray_image2], [0],
None, [256], [0, 256])
c1, c2 = 0, 0
# Euclidean Distance between data1 and test
i = 0
while i
输出 :
data1.jpg is more similar to test.jpg as compare to data2.jpg