使用for Loop python遍历两个目录
我试图比较两个PDF。我的方法包括转换PDF和比较相应页面的图像。我最初编写了一个简单的代码,比较了与下面第一个代码相对应的两个图像,该代码工作时没有任何问题使用for Loop python遍历两个目录,python,numpy,opencv,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 =
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()
您需要合并这两个列表以比较相同的图像 您可以使用
zip
:
导入cv2
a=[1]
b=[1]
对于i,j(a,b):
原件=cv2.imread(“图像旧/图像旧”+str(i)+.jpg”)
image\u to\u compare=cv2.imread(“image\u new/imageNew”+str(j)+.jpg”)
image1=原始形状
image2=图像与形状的比较
如果original.shape==image\u to\u compare.shape:
打印(“图像”+str(i)+“具有相同的大小和通道”)
打印(“两个PDF的差异页”+str(i)+”和“+str(j)+”)
差值=cv2.相减(原始、图像比较)
r、 g,b=cv2.分割(差异)
cv2.imshow(“差异”,cv2.resize(差异,无,fx=0.3,fy=0.3))
如果cv2.countNonZero(b)==0且cv2.countNonZero(g)==0且cv2.countNonZero(r)==0:
打印(“图像完全相等”)
其他:
打印(“图像不相等”)
sift=cv2.xfeature2d.sift_create()
kp_1,desc_1=筛选、检测和计算(原始,无)
kp_2,desc_2=sift.detectAndCompute(图像比较,无)
打印(“第一张图像的关键点:+str(透镜(kp_1)))
打印(“第二幅图像的关键点:+str(len(kp_2)))
索引参数=dict(算法=0,树=5)
搜索参数=dict()
flann=cv2.FlannBasedMatcher(索引参数、搜索参数)
匹配=法兰。knnMatch(描述1,描述2,k=2)
好分数=[]
对于匹配中的m,n:
如果m.距离<0.6*n.距离:
好的分数。追加(m)
打印('图像有%d%s'(len(好点),“好点匹配”))
如果len(kp_1)
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 :