現在、検出した複数の円の出現時間を個別に計測し、記録するシステムを作製しようと考えています。各円は座標により区別するものとしました。以下が、作成したプログラムになります。
python
1from datetime import datetime 2import math 3import numpy as np 4import cv2 5 6FIND_DIST = 30 7targets = [] 8 9id = 1 10cap = cv2.VideoCapture(1) 11while True: 12 pts = [] 13 now_time = datetime.now() 14 ret, frame = cap.read() 15 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 16 gray = cv2.GaussianBlur(gray, (33,33), 1) 17 circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 60, param1=10, param2=85, minRadius=10, maxRadius=80) 18 19 if circles is not None: 20 21 circles = np.uint16(np.around(circles)) 22 for i in circles[0,:]: 23 cv2.circle(frame,(i[0],i[1]),i[2],(255,255,0),2) 24 cv2.circle(frame,(i[0],i[1]),2,(0,0,255),3) 25 pt = (i[0],i[1]) 26 pts.append((float(pt[0]), float(pt[1]))) 27 28 print(pts) 29 for tgt in targets: 30 tgt['bIn'] = False 31 32 new_tgts = [] 33 34 for pt in pts: 35 bIn = False 36 for tgt in targets: 37 38 if math.sqrt( math.pow(pt[0] - tgt['pos'][0], 2) + math.pow(pt[1] - tgt['pos'][1], 2)) <= FIND_DIST: 39 tgt['bIn'] = True 40 tgt['pos'] = pt 41 bIn = True 42 43 if not bIn: 44 new_tgt = {'id':id,'bIn':True,'pos':pt,'in_time':now_time} 45 new_tgts.append(new_tgt) 46 id += 1 47 48 for tgt in targets: 49 if tgt['bIn']: 50 new_tgts.append(tgt) 51 else: 52 print('{}sec'.format((now_time - tgt['in_time']).total_seconds())) 53 54 targets = new_tgts 55 56 57 cv2.imshow('preview', frame) 58 key = cv2.waitKey(10) 59 if key == ord("q"): 60 break 61 62cv2.destroyAllWindows()
起動させた結果が以下の写真です。
複数円を表示させ続けていても、途中で計測をやめてしまいます。そのため、pts内の座標を表示させるようにし起動させたところ、座標がなくなったらその座標の時間計測をやめるという動きはできているようですが、しっかり座標を保管することが出来ていないということがわたりました。
1フレーム内の円の座標を逃さずlistに保管できるようにしたいのですが、ハフ変換では今回の結果が限界なのでしょうか。また、OpenCVでの円検出について調べてみたのですが、ハフ変換に関する内容のものしか見つけることが出来ていない状況です。ハフ変換より精度がよい円の検出方法について教えていただきたいです。読みずらい文章で申し訳ありません。よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー