とあるサイトを参考にさせていただきながら,PythonのOpenCVで画像認識をプログラムしております.
画像の中でパターンマッチを用いて,楕円の検出を行うプログラムです.
検出自体はされるのですが,検出箇所を四角で囲うとマッチが重複してしまいます.
この重複してしまっている検出を1つに絞りたいです.
つまり座標の近似値をみて,数値が近かった場合,1つの座標にまとめたいということです.
どうか皆様の知恵をお借りできれば幸いです.
ターミナルに座標を出力した結果
TERMINAL
1('y=', 30, 'x=', 217) 2('y=', 31, 'x=', 216) 3('y=', 31, 'x=', 217) 4('y=', 32, 'x=', 216) 5('y=', 32, 'x=', 217) 6('y=', 34, 'x=', 180) 7以下略
検出を行っているプログラム
Python
1# coding:utf-8 2import cv2 3import math 4import numpy 5import os 6 7#画像の読み込み 8image = cv2.imread('singlepage.jpg') 9gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 10 11retval, binarized = cv2.threshold(gray, 224, 255, cv2.THRESH_BINARY_INV) 12cv2.imshow('binarized', binarized) 13 14#パターンマッチ 15def pattern_match(image, original_image): 16 template_width = 15 17 template_height = 15 18 19 ellipse = numpy.zeros((template_height, template_width, 1), numpy.uint8) 20 #楕円の特徴 21 cv2.ellipse(ellipse, center=(8, 8), axes=(3, 5), angle=60, startAngle=0, 22 endAngle=360, color=255, thickness=-1, lineType=cv2.LINE_AA) 23 cv2.imshow('ellipse', ellipse) 24 25 matches = cv2.matchTemplate(image, ellipse, cv2.TM_CCOEFF_NORMED) 26 27 threshold = 0.6 28 for y in range(matches.shape[0]): 29 for x in range(matches.shape[1]): 30 #検出したところを四角で囲う 31 if matches[y][x] > threshold: 32 cv2.rectangle(original_image, (x, y), (x + template_width, y + template_height), (255, 0, 0), 1) 33 #座標の表示をやってみる 34 print("y=", y, "x=", x) 35#画像の表示 36pattern_match(binarized, image) 37cv2.imshow('score', image) 38cv2.imwrite('gazou1.png', image) 39cv2.waitKey(0) 40cv2.destroyAllWindows()

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/28 14:58