opencv检测圆形
时间: 2023-10-26 15:08:31 浏览: 102
opencv偵測圓形
3星 · 编辑精心推荐
要使用OpenCV来检测圆形,你可以使用Hough圆变换算法。下面是一个基本的代码示例:
```python
import cv2
import numpy as np
# 读取图像
image = cv2.imread('image.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 高斯模糊
blur = cv2.GaussianBlur(gray, (5, 5), 0)
# 检测圆形
circles = cv2.HoughCircles(blur, cv2.HOUGH_GRADIENT, dp=1, minDist=20,
param1=50, param2=30, minRadius=10, maxRadius=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), 4)
# 显示结果
cv2.imshow("Detected Circles", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
阅读全文