###前提・実現したいこと
以前にした質問1
以前にした質問2
を使って円検出の精度を上げたいと思っています。
###発生している問題
知識不足で申し訳ないですがどう組み合わせればいいかわかりません。円を検出した後に色を確認したほうがいいのか、順番を変えたほうがいいのか具体的にどう条件分岐をさせればいいか教えていただきたいです。
#該当のソースコード
python
1import numpy as np 2import cv2 3 4cam = cv2.VideoCapture(0) 5 6 7while(cam.isOpened()): 8 9 ret, frame = cam.read() 10 cam_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) 11 cam_hsv = cv2.inRange(cam_hsv, (30,100,100), (60,160,160)) 12 13 cam_color = frame.copy() 14 cam_color[cam_hsv==0] = 0 15 16 17 element = np.ones((5,5)).astype(np.uint8) 18 cam_hsv = cv2.erode(cam_hsv, element) 19 cam_hsv = cv2.dilate(cam_hsv, element) 20 points = np.dstack(np.where(cam_hsv>0)).astype(np.float32) 21 center, radius = cv2.minEnclosingCircle(points) 22 cv2.circle(frame, (int(center[1]), int(center[0])), int(radius), (255,0,0), thickness=3) 23 24 25 cv2.imshow('preview', frame) 26 27 if cv2.waitKey(1) & 0xFF == ord('q'): 28 break
python
1import numpy as np 2import cv2 3 4cam = cv2.VideoCapture(0) 5 6while(cam.isOpened()): 7 8 ret, frame = cam.read() 9 10 size = (640, 480) 11 frame = cv2.resize(frame, size) 12 13 gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY) 14 gray = cv2.GaussianBlur(gray, (33,33), 1) 15 16 colimg = frame.copy() 17 18 circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 60, param1=40, param2=75, minRadius=10, maxRadius=100) 19 20 if circles is not None: 21 circles = np.uint16(np.around(circles)) 22 for i in circles[0,:]: 23 24 cv2.circle(colimg,(i[0],i[1]),i[2],(255,255,0),2) 25 26 cv2.imshow('preview', colimg) 27 if cv2.waitKey(1) & 0xFF == ord('q'): 28 break 29 30cam.release() 31cv2.destroyAllWindows()
回答1件
あなたの回答
tips
プレビュー