树莓派opencv检验圆形
时间: 2023-09-05 11:13:52 浏览: 159
详解利用python+opencv识别图片中的圆形(霍夫变换)
5星 · 资源好评率100%
使用OpenCV在树莓派上检测圆形的步骤如下:
1. 导入必要的库,包括cv2和numpy
```python
import cv2
import numpy as np
```
2. 加载图像并将其转换为灰度图像
```python
img = cv2.imread('image.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
```
3. 对图像进行模糊处理以减少噪声
```python
blurred = cv2.GaussianBlur(gray, (11,11), 0)
```
4. 使用Hough变换检测圆形
```python
circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT, 1.2, 100)
```
5. 绘制检测到的圆形
```python
if circles is not None:
circles = np.round(circles[0, :]).astype("int")
for (x, y, r) in circles:
cv2.circle(img, (x, y), r, (0, 255, 0), 4)
```
完整的代码示例:
```python
import cv2
import numpy as np
img = cv2.imread('image.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (11,11), 0)
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(img, (x, y), r, (0, 255, 0), 4)
cv2.imshow("output", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
注意:在树莓派上运行OpenCV可能会比在其他计算机上慢,因此可能需要调整一些参数以获得最佳性能。
阅读全文