前提・実現したいこと
cv2.findContoursを利用して、target_fileに指定した画像とフォルダ内の全部の画像の形状マッチングを行いたい。
ここに質問の内容を詳しく書いてください。
target_fileに指定した画像とフォルダ内の全部の画像の形状マッチングを行いたいのですが、うまくいきません。
最初のファイルとのマッチングによる差の数値は、結果を返してくれるのですが、ほかの画像とのマッチング結果が0.0で返されてしまいます。
エラーが出るわけではないので、画像の読み込み事態はできていると思うのですが、何を直せばよいかわかりません。
何かお気づきがあればご指摘をいただきたいです。よろしくお願いします。
■■な機能を実装中に以下のエラーメッセージが発生しました。
返ってきた計算結果
TARGET_FILE: YRN0024.jpg YRN0364.jpg 1.7976931348623157e+308 YRN0104.jpg 0.0 YRN0139.jpg 0.0
Python
1import cv2 2import numpy as np 3import os 4 5TARGET_FILE = 'YRN0024.jpg' 6IMG_DIR = os.path.abspath(os.path.dirname(__file__)) + '/images/' 7 8target_img_path = IMG_DIR + TARGET_FILE 9target_img = cv2.imread(target_img_path,0) 10 11 12print('TARGET_FILE: %s' % (TARGET_FILE)) 13 14 15files = os.listdir(IMG_DIR) 16for file in files: 17 if file == '.DS_Store' or file == TARGET_FILE: 18 continue 19 20 comparing_img_path = IMG_DIR + file 21 try: 22 comparing_img = cv2.imread(comparing_img_path, cv2.IMREAD_GRAYSCALE) 23 ret, thresh = cv2.threshold(target_img, 127, 255,0) 24 ret, thresh2 = cv2.threshold(comparing_img, 127, 255,0) 25 image1,contours,hierarchy = cv2.findContours(thresh,2,1) 26 image2,contours2,hierarchy = cv2.findContours(thresh2,2,1) 27 cnt1 = contours[0] 28 cnt2 = contours2[0] 29 ret = cv2.matchShapes(cnt1,cnt2,1,0.0) 30 31 except cv2.error: 32 ret = 10000 33 except ZeroDivisionError: 34 print("ZeroDivisionError!!") 35 36 print (file, ret) 37
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/13 18:01