主要实现的功能是能实时识别视频中的圆,并返回圆心位置
import cv2
import numpy as np
def decodeDisplay(video, flag):
gay_img = cv2.cvtColor(video, cv2.COLOR_BGRA2GRAY)
img = cv2.medianBlur(gay_img, 7) # 进行中值模糊,去噪点
cimg = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 50, param1=100, param2=30, minRadius=0, maxRadius=0)
if circles is not None:
circles = np.uint16(np.around(circles))
print(circles)
for i in circles[0, :]: # 遍历矩阵每一行的数据
cv2.circle(video, (i[0], i[1]), i[2], (0, 255, 0), 2)
cv2.circle(video, (i[0], i[1]), 2, (0, 0, 255), 3)
cv2.imshow("gay_img", video)
else:
# 如果识别不出,显示圆心不存在
print('x: None y: None')
cv2.imshow('frame', video)
def detect():
cap = cv2.VideoCapture(1)
while True:
# 逐帧捕获
ret, frame = cap.read()
cv2.imshow("img", frame)
flag = cv2.waitKey(1)
flag = decodeDisplay(frame, flag)
# 一切完成后,释放捕获
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
detect()
参考:https://blog.youkuaiyun.com/yukinoai/article/details/87447113