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

标签 python numpy opencv

我正在尝试比较两个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 ...我在下面收到错误信息,我在做什么错了?
import cv2
import numpy as np

a=[1,2,3,4,5]
b=[1,2,3,4,5]

for i in a:
    original = cv2.imread("image_old/imageOld_"+str(i)+".jpg")
    for j in b:
        if a == b :
            image_to_compare = cv2.imread("image_new/imageNew_"+str(j)+".jpg")
            image1 = original.shape
            image2 = image_to_compare.shape

            if original.shape == image_to_compare.shape:
                print("The images "+str(i)+" have same size and channels")

                print("Diffing page "+str(i)+" and "+str(j)+" of both pdfs")
                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))
                
                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()
错误
 DeprecationWarning: elementwise comparison failed; this will raise an error in the future.
  if a == b :

最佳答案

您需要合并两个列表以比较相同的图像。
您可以使用zip:

import cv2

a = [1]
b = [1]

for i, j in zip(a, b):
    original = cv2.imread("image_old/imageOld_" + str(i) + ".jpg")
    image_to_compare = cv2.imread("image_new/imageNew_" + str(j) + ".jpg")
    image1 = original.shape
    image2 = image_to_compare.shape

    if original.shape == image_to_compare.shape:
        print("The images " + str(i) + " have same size and channels")
        print("Diffing page " + str(i) + " and " + str(j) + " of both pdfs")
        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))

        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()
结果:
The images 1 have same size and channels
Diffing page 1 and 1 of both pdfs
The images are completely Equal
Keypoints of 1st image: 8066
Keypoints of 2nd image: 8066
The images have 3117 good points matches
Similarity 39 %
以下是我同时用于imagesNew_1.jpgimagesOld_1.jpg的示例
enter image description here

关于python - 使用for循环python遍历两个目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63990776/

相关文章:

python - 如何加快 python 中的数组生成速度?

python - 在python中读取二进制大端文件

python - 翻转图像后,OpenCV putText不起作用

opencv - Jetson Nano 上带有 CUDA 的 ROS 和 OpenCV : Incompatible versions

python - 无法使用 Airflow 连接到 Postgres 数据库

python - 如何在 ctypes 中传回指针?

python - 使用 Python 按第一个单词对文件/行进行排序

python - 在删除 axis0 的 I 索引的子元素的同时创建一个平铺的多维数组?

python - cv2.groupRectangles 返回一个 len=1 的数组

opencv - Opencv videoCapture() 中的 Gstreamer 管道