第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用 for 循環(huán) python 循環(huán)遍歷兩個(gè)目錄

使用 for 循環(huán) python 循環(huán)遍歷兩個(gè)目錄

躍然一笑 2023-08-22 14:46:52
我正在嘗試比較兩個(gè)pdf。我的方法包括轉(zhuǎn)換兩個(gè) pdf 并比較相應(yīng)頁(yè)面的圖像。我最初編寫了一個(gè)簡(jiǎn)單的代碼,它比較與下面的第一個(gè)代碼相對(duì)應(yīng)的兩個(gè)圖像,該代碼可以正常工作。import cv2import numpy as nporiginal = cv2.imread("image_old/imageOld_1.jpg")image_to_compare = cv2.imread("image_new/imageNew_1.jpg")image1 = original.shapeimage2 = image_to_compare.shapeif 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 * 100print('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()接下來(lái),我添加了一個(gè) for 循環(huán),與上面的代碼執(zhí)行相同的操作,但針對(duì) 5 個(gè)圖像。這個(gè)想法是,它獲取 imageOld_1.jpg 并與 imageNew_1.jpg、imageOld_2.jpg 進(jìn)行比較,并與 imageNew_2.jpg 進(jìn)行比較...下面的代碼比較第一對(duì)圖像 imageOld_1.jpg 和 imageNew_1.jpg(我得到結(jié)果)但由于某種原因,它不會(huì)繼續(xù)循環(huán)遍歷其他圖像,即比較 imageOld_2.jpg 和 imageNew_2.jpg、比較 imageOld_3.jpg 和 imageNew_3.jpg...我收到以下錯(cuò)誤,我做錯(cuò)了什么?
查看完整描述

1 回答

?
明月笑刀無(wú)情

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超4個(gè)贊

您需要組合兩個(gè)列表來(lái)比較相同的圖像。


您可以使用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()

結(jié)果:


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.jpg以下是我用于和的示例imagesOld_1.jpg

https://img1.sycdn.imooc.com//64e45a0a0001109703560227.jpg

查看完整回答
反對(duì) 回復(fù) 2023-08-22
  • 1 回答
  • 0 關(guān)注
  • 3455 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)