我在Python 3中使用OpenCV来检测白场上的白/黑球并给出其精确(x,y,半径)和颜色 .
我使用函数cv2.Canny()和cv2.findContours()来找到它,但问题是cv2.Canny()不要总是检测圆的完整形状(大部分时间只有圆的3/4) . 因此,当我使用cv2.findContours()时,它不会将其检测为轮廓 .
请参阅 :
和
这是最重要的代码:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(blurred, 10, 40) # 10 and 40 to be more perceptive
contours_canny= cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[-2]
之后我使用: approx = cv2.approxPolyDP(contour,0.01*cv2.arcLength(contour,True),True)
So if you can help me find a solution that would be great! Maybe is there a function that complete the shape of the circle so i can detect it?
2 回答
我建议你在使用canny检测之前应用一些中间色彩过滤 . 这将确保精确边缘检测发生在球图像和场之间的良好定义的边界上 .
这是一个使用跟踪栏的python代码,您可以自定义颜色阈值:
您可以使用cv.HoughCircles来估计最佳匹配的完整圆 . 球跟踪的好处在于,无论相机角度如何,球都将始终显示为图像中的圆圈 .