前提・実現したいこと
pythonとOpenCVを使って画像の類似度を特徴点の距離を使って求めたいのですが、AttributeErrorが出てしまいます。まだ初心者なので初歩的なミスをしているかもしれませんが教えていただけると幸いです。
プログラムは全く関係ないところは省いています。
参考にしたサイト
https://qiita.com/best_not_best/items/c9497ffb5240622ede01
追記
最後の4行の類似度の算出のみ参考にさせていただきました。
発生している問題・エラーメッセージ
dist = [m.distance for m in matches] AttributeError: 'list' object has no attribute 'distance'
該当のソースコード
python
1import cv2 2import os 3 4files='...path' 5 6detector = cv2.AKAZE_create() 7 8for fname in os.listdir(files): 9 img1 = cv2.imread(files+'/'+fname) 10 faces1 = face_cascade.detectMultiScale(img1) 11 12 for x, y, w, h in faces1: 13 cv2.rectangle(img1, (x, y), (x + w, y + h), (255, 0, 0), 2) 14 face1 = img1[y: y + h, x: x + w] 15 img_re1 = cv2.resize(face1,(300, 300)) 16 17 kp1, des1 = detector.detectAndCompute(img_re1, None) 18 kp2, des2 = detector.detectAndCompute(img_re2, None) 19 20 bf = cv2.BFMatcher() 21 matches = bf.knnMatch(des1,des2,k=2) 22 23 ratio = 0.8 24 good = [] 25 for m, n in matches: 26 if m.distance < ratio * n.distance: 27 good.append([m]) 28 29 dist = [m.distance for m in matches] 30 ret = sum(dist) / len(dist) 31 32 print(fname,ret)
回答1件
あなたの回答
tips
プレビュー