python opencv 霍夫曼圆过滤重复
要在Python OpenCV中使用霍夫曼圆检测并过滤掉重复的圆,可以按照以下步骤进行操作:
1. 导入必要的库:
```python
import cv2
import numpy as np
```
2. 读取图像并转换为灰度图像:
```python
image = cv2.imread('input_image.jpg')
美妆网页设计模板大全gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
```
3. 对灰度图像进行边缘检测:
```python
edges = cv2.Canny(gray, 50, 150)
```
4. 使用霍夫曼圆检测来检测圆:
```python
circles = cv2.HoughCircles(edges, cv2.HOUGH_GRADIENT, dp=1, minDist=20, param1=50, param2=30, minRadius=0, maxRadius=0)
```
这里的参数可以根据实际情况进行调整。`dp`参数是霍夫曼变换的累加器分辨率与图像分辨率的倒数之比。`minDist`参数是圆心之间的最小距离。`param1`参数是边缘检测阈值,`param2`参数是累加器阈值。`minRadius`和`maxRadius`参数是待检测的圆半径的最小值和最大值。
5. 对检测到的圆进行去重:
```python
if circles is not None:
circles = np.round(circles[0, :]).astype("int")
unique_circles = []python基础代码大全下载
excel下拉序号windows server 2008添加硬盘 for (x, y, r) in circles:
is_duplicate = False
process on网页版 for (ux, uy, ur) in unique_circles:
if abs(x - ux) < 10 and abs(y - uy) < 10:
is_duplicate = True
break
if not is_duplicate:
unique_circles.append((x, y, r))
# 绘制去重后的圆
for (x, y, r) in unique_circles:
powerpoint没保存怎么恢复 cv2.circle(image, (x, y), r, (0, 255, 0), 4)
cv2.imshow("Output", image)
cv2.waitKey(0)
```
上述代码中,使用一个新的列表`unique_circles`来保存去重后的圆。对于每个检测到的圆,判断是否与已保存的圆过于接近,如果不接近则将其保存到`unique_circles`中。
最后,使用`cv2.circle`函数绘制去重后的圆,并展示结果图像。
以上就是在Python OpenCV中使用霍夫曼圆检测并过滤重复圆的方法。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论