使用 for 循环 python 循环遍历两个目录

我正在尝试比较两个pdf。我的方法包括转换两个 pdf 并比较相应页面的图像。我最初编写了一个简单的代码,它比较与下面的第一个代码相对应的两个图像,该代码可以正常工作。



import cv2

import numpy as np


original = cv2.imread("image_old/imageOld_1.jpg")

image_to_compare = cv2.imread("image_new/imageNew_1.jpg")


image1 = original.shape

image2 = image_to_compare.shape


if original.shape == image_to_compare.shape:

    print("The images have same size and channels")

    difference = cv2.subtract(original, image_to_compare)

    r, g, b = cv2.split(difference)  

    cv2.imshow("difference", cv2.resize( difference, None, fx=0.3, fy=0.3))

    

    print(cv2.countNonZero(b))

    if cv2.countNonZero(b) == 0 and cv2.countNonZero(g) == 0 and cv2.countNonZero(r) == 0:

       print("The images are completely Equal")

    else:

        print("The images are not equal")


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 of 1st image: " + str(len(kp_1)))

print("Keypoints of 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 = []

for m, n in matches:

    if m.distance < 0.6*n.distance:

        good_points.append(m)

print('The images have %d %s' %(len(good_points),"good points matches"))


if len(kp_1) <= len(kp_2):

    number_keypoints = len(kp_1)

else:

    number_keypoints = len(kp_2)


percentage_similarity = len(good_points) / number_keypoints * 100

print('Similarity %d %s' %(round((percentage_similarity)),"%\n"))

result = cv2.drawMatches(original, kp_1, image_to_compare, kp_2, good_points, None)

cv2.waitKey(0)

cv2.destroyAllWindows()

接下来,我添加了一个 for 循环,与上面的代码执行相同的操作,但针对 5 个图像。这个想法是,它获取 imageOld_1.jpg 并与 imageNew_1.jpg、imageOld_2.jpg 进行比较,并与 imageNew_2.jpg 进行比较...下面的代码比较第一对图像 imageOld_1.jpg 和 imageNew_1.jpg(我得到结果)但由于某种原因,它不会继续循环遍历其他图像,即比较 imageOld_2.jpg 和 imageNew_2.jpg、比较 imageOld_3.jpg 和 imageNew_3.jpg...我收到以下错误,我做错了什么?


跃然一笑
浏览 3323回答 1
1回答

明月笑刀无情

您需要组合两个列表来比较相同的图像。您可以使用zip:import cv2a = [1]b = [1]for i, j in zip(a, b):&nbsp; &nbsp; original = cv2.imread("image_old/imageOld_" + str(i) + ".jpg")&nbsp; &nbsp; image_to_compare = cv2.imread("image_new/imageNew_" + str(j) + ".jpg")&nbsp; &nbsp; image1 = original.shape&nbsp; &nbsp; image2 = image_to_compare.shape&nbsp; &nbsp; if original.shape == image_to_compare.shape:&nbsp; &nbsp; &nbsp; &nbsp; print("The images " + str(i) + " have same size and channels")&nbsp; &nbsp; &nbsp; &nbsp; print("Diffing page " + str(i) + " and " + str(j) + " of both pdfs")&nbsp; &nbsp; &nbsp; &nbsp; difference = cv2.subtract(original, image_to_compare)&nbsp; &nbsp; &nbsp; &nbsp; r, g, b = cv2.split(difference)&nbsp; &nbsp; &nbsp; &nbsp; cv2.imshow("difference", cv2.resize(difference, None, fx=0.3, fy=0.3))&nbsp; &nbsp; &nbsp; &nbsp; if cv2.countNonZero(b) == 0 and cv2.countNonZero(g) == 0 and cv2.countNonZero(r) == 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("The images are completely Equal")&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("The images are not equal")&nbsp; &nbsp; sift = cv2.xfeatures2d.SIFT_create()&nbsp; &nbsp; kp_1, desc_1 = sift.detectAndCompute(original, None)&nbsp; &nbsp; kp_2, desc_2 = sift.detectAndCompute(image_to_compare, None)&nbsp; &nbsp; print("Keypoints of 1st image: " + str(len(kp_1)))&nbsp; &nbsp; print("Keypoints of 2nd image: " + str(len(kp_2)))&nbsp; &nbsp; index_params = dict(algorithm=0, trees=5)&nbsp; &nbsp; search_params = dict()&nbsp; &nbsp; flann = cv2.FlannBasedMatcher(index_params, search_params)&nbsp; &nbsp; matches = flann.knnMatch(desc_1, desc_2, k=2)&nbsp; &nbsp; good_points = []&nbsp; &nbsp; for m, n in matches:&nbsp; &nbsp; &nbsp; &nbsp; if m.distance < 0.6 * n.distance:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; good_points.append(m)&nbsp; &nbsp; print('The images have %d %s' % (len(good_points), "good points matches"))&nbsp; &nbsp; if len(kp_1) <= len(kp_2):&nbsp; &nbsp; &nbsp; &nbsp; number_keypoints = len(kp_1)&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; number_keypoints = len(kp_2)&nbsp; &nbsp; percentage_similarity = len(good_points) / number_keypoints * 100&nbsp; &nbsp; print('Similarity %d %s' % (round((percentage_similarity)), "%\n"))&nbsp; &nbsp; result = cv2.drawMatches(original, kp_1, image_to_compare, kp_2, good_points, None)cv2.waitKey(0)cv2.destroyAllWindows()结果:The images 1 have same size and channelsDiffing page 1 and 1 of both pdfsThe images are completely EqualKeypoints of 1st image: 8066Keypoints of 2nd image: 8066The images have 3117 good points matchesSimilarity 39 %imagesNew_1.jpg以下是我用于和的示例imagesOld_1.jpg
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python