opencv识别圆形
时间: 2023-09-05 07:10:02 浏览: 59
你可以使用OpenCV库进行圆形识别。下面是一个简单的示例代码:
```python
import cv2
import numpy as np
# 读取图像
image = cv2.imread('image.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 高斯模糊
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
# 使用Hough变换检测圆形
circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT, 1.2, 100)
# 确保至少检测到一个圆
if circles is not None:
circles = np.round(circles[0, :]).astype('int')
# 遍历检测到的圆并绘制
for (x, y, r) in circles:
cv2.circle(image, (x, y), r, (0, 255, 0), 3)
cv2.rectangle(image, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)
# 显示结果图像
cv2.imshow("Detected Circles", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
请注意,此代码假定输入图像中只有一个圆。如果你的图像中有多个圆,你可以根据需要进行修改。