【发布时间】:2021-04-15 11:37:04
【问题描述】:
我正在使用 OpenCV 来比较 2 张图像。
几天后,我能够对其进行修改以将图像与图像列表进行比较。
如何将图像列表与另一个列表进行比较?
例如:我们有 2 个文件夹 Images1 和 Images2。图片1 = te1.jpg,te2.jpg,te3.jpg;图片2 = te1.jpg、te2.jpg、te3.jpg。
我想将 Images1 中的 te1.jpg 与 Images2 中的 te1.jpg 进行比较,将 Images1 中的 te2.jpg 与 Images2 中的 te2.jpg 以及 Images1 中的 te3.jpg 与 Images2 中的 te3.jpg 进行比较。
我可以添加这两个文件夹并使其循环遍历它们,以便为 Images1 中的每个图像获取 Images2 中的对应图像吗?
He is my code until now:
import cv2
import numpy as np
import glob
original = cv2.imread("te.jpg")
#Load all the images
all_images_to_compare = []
titles = []
for f in glob.iglob("images2/*"):
image = cv2.imread(f)
titles.append(f)
all_images_to_compare.append(image)
for image_to_compare, title in zip(all_images_to_compare, titles):
# 1) Check if 2 images are equals
if original.shape == image_to_compare.shape:
print("The images have the same size and channels")
difference = cv2.subtract(original, image_to_compare)
b, g, r = cv2.split(difference)
#image1 = original.shape
#image2 = duplicate.shape
cv2.imshow("difference", difference)
#cv2.imshow("b", b)
#cv2.imshow("g", g)
#cv2.imshow("r", r)
#print(image1)
#print(image2)
print(cv2.countNonZero(b))
if cv2.countNonZero(b) == 0 and cv2.countNonZero(g) == 0 and cv2.countNonZero(r) ==0:
print("Similarity: 100% (equal size and channels)")
# 2) Check for similarities between the 2 images
sift = cv2.xfeatures2d.SIFT_create()
kp_1, desc_1 = sift.detectAndCompute(original, None)
kp_2, desc_2 = sift.detectAndCompute(image_to_compare, None)
#print("Keypoints 1ST Image: " + str(len(kp_1)))
#print("Keypoints 2ND Image: " + str(len(kp_2)))
index_params = dict(algorithm=0, trees=5)
search_params = dict()
flann = cv2.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(desc_1, desc_2, k=2)
good_points = []
ratio = 0.9 # mai putin de 1
for m, n in matches:
if m.distance < ratio*n.distance:
good_points.append(m)
# Define how similar they are
number_keypoints = 0
if len(kp_1) <= len(kp_2):
number_keypoints = len(kp_1)
else:
number_keypoints = len(kp_2)
print("Keypoints 1ST Image: " + str(len(kp_1)))
print("Keypoints 2ND Image: " + str(len(kp_2)))
print("Title:" +title)
percentage_similarity = len(good_points) / number_keypoints * 100
print("Similarity: " + str(int(percentage_similarity)) + "%\n")
【问题讨论】:
标签: python-3.x opencv image-compression