用python-opencv写一个调取摄像头并识别框取圆形图形的代码
时间: 2023-05-12 13:03:01 浏览: 229
可以使用以下代码实现:
```python
import cv2
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5, 5), 0)
circles = cv2.HoughCircles(blur, cv2.HOUGH_GRADIENT, 1, 20, param1=50, param2=30, minRadius=0, maxRadius=0)
if circles is not None:
circles = circles[0]
for circle in circles:
x, y, r = circle
cv2.circle(frame, (x, y), r, (0, 255, 0), 2)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
```
这段代码使用 OpenCV 库调用摄像头,并使用 Hough 变换检测圆形图形。如果检测到圆形,就在图像上画出圆形的边界。
阅读全文