前提・実現したいこと
画像の類似度を調べるために、入力画像用のフォルダーと比較用の画像のフォルダーでマッチングを行っています。しかし、何故か上手くいくフォルダーと上手くいかないフォルダーがあります。なぜこのようになるのか教えてください。
下記のソースコードの8行目のパスがANSとSUSの時のみこのエラーが出てしまいます。
発生している問題・エラーメッセージ
File "C:/kenkyu/.spyder-py3/folder.py", line 41, in <module> matches = bf.knnMatch(des1,des2,k=2) error: C:\ci\opencv_1512688052760\work\modules\core\src\stat.cpp:4017: error: (-215) type == src2.type() && src1.cols == src2.cols && (type == 5 || type == 0) in function cv::batchDistance
該当のソースコード
python
1# -*- coding: utf-8 -*- 2import cv2 3import os 4import csv 5 6face_cascade_file = ("C:/anaconda/haarcascades/haarcascade_frontalface_default.xml") 7files='C:/face picture/filter face' 8test='C:/face picture/test face/ANS' 9 10detector = cv2.AKAZE_create() 11#detector = cv2.ORB_create() 12#detector = cv2.xfeatures2d.SURF_create() 13#detector = cv2.xfeatures2d.SIFT_create() 14 15face_cascade = cv2.CascadeClassifier(face_cascade_file) 16 17for img in os.listdir(test): 18 img2 = cv2.imread(test+'/'+img) 19 img2_gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) 20 21 faces2 = face_cascade.detectMultiScale(img2_gray) 22 23 for x, y, w, h in faces2: 24 cv2.rectangle(img2, (x, y), (x + w, y + h), (255, 0, 0), 2) 25 face2 = img2[y: y + h, x: x + w] 26 img_re2 = cv2.resize(face2,(300, 300)) 27 28 for fname in os.listdir(files): 29 img1 = cv2.imread(files+'/'+fname) 30 faces1 = face_cascade.detectMultiScale(img1) 31 32 for x, y, w, h in faces1: 33 cv2.rectangle(img1, (x, y), (x + w, y + h), (255, 0, 0), 2) 34 face1 = img1[y: y + h, x: x + w] 35 img_re1 = cv2.resize(face1,(300, 300)) 36 37 kp1, des1 = detector.detectAndCompute(img_re1, None) 38 kp2, des2 = detector.detectAndCompute(img_re2, None) 39 40 bf = cv2.BFMatcher() 41 matches = bf.knnMatch(des1,des2,k=2) 42 43 ratio = 0.9 44 good = [] 45 for m, n in matches: 46 if m.distance < ratio * n.distance: 47 good.append([m]) 48 49 dist = [m.distance for x in matches] 50 ret = sum(dist) / len(dist) 51 52 with open('result.csv','a',newline='') as csvFile: 53 writer = csv.writer(csvFile) 54 writer.writerow([fname,float(ret)]) 55 56csvFile.close() 57 58cv2.waitKey() 59cv2.destroyAllWindows
回答1件
あなたの回答
tips
プレビュー