python - 从图像中查找车轮中的辐条总数 - OpenCV (Python)

标签 python numpy opencv image-processing machine-learning

我有几张带有马车车轮的图片。
它们是灰度级的——像素级别从最小值(黑色)到最大值(白色)不等。 轮子呈现为单个连接组件。 车轮部分为白色,背景为黑色。

示例图片 1:

Example image 1

示例图片 2:

Example image 2

这是轮子及其零件:

This is wheel with it's parts

我已经找到了轮轴的中心,但我还想找到图片中的所有辐条数断裂辐条数

以下是关于图片的一些事实:

1.每根辐条都与箍半径对齐
2. 每根辐条的最小角宽为2度
3. 任意两条辐条中心线之间的角度间隔至少10度
4.辐条不是等距放置
5. 如果辐条没有完全连接箍和轴,辐条中的间隙至少为10 像素
6. 轴 - 由最小半径为10 像素 的实心圆表示
7. 一个环——由两个与轮轴同心的圆圈表示。它的最小厚度为10 像素

所以,这是我检测轮轴的代码。我使用 HoughCircles 来检测最小的圆。

import cv2
import numpy as np
import matplotlib.pyplot as plt


img_bgr = cv2.imread('wheel.png')
img_gray = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY)

circles = cv2.HoughCircles(img_gray, cv2.HOUGH_GRADIENT, 1, 300, 
np.array([]), 10, 30, 10, 50)
center_x = 0
center_y = 0

if circles is not None:
    circles = np.uint16(np.around(circles))

    for i in circles[0, :]:
        cv2.circle(img_bgr, (i[0], i[1]), i[2], (0, 255, 0), 2)
        cv2.circle(img_bgr, (i[0], i[1]), 2, (0, 0, 255), 3)
        center_x = i[0]
        center_y = i[1]

print("%d %d" % (center_x, center_y))
plt.imshow(img_bgr, cmap='gray', interpolation='bicubic')
plt.show()

这就是结果。
Found center of the wheel

另外,我想找到最长弧的长度 不被任何辐条截断 φmax(以度为单位)。将车轮视为平坦的,即车轮的所有部分都位于一个平面内。

我试图用 CannyHoughLinesP 寻找边,但我卡住了,不知道下一步该怎么做。

threshhold, threshhold_img = cv2.threshold(img_gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
edges = cv2.Canny(threshhold_img, 150, 200, 3, 5)
lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 30, 20, 5)

for line in lines:
    for x1, y1, x2, y2 in line:
        print("(%d, %d) && (%d, %d)" % (x1, y1, x2, y2))
        cv2.line(img_bgr, (x1, y1), (x2, y2), (255, 0, 0), 3)

这是标记辐条的图像:
This is the image of marked spokes

我是 OpenCV 的新手,所以任何建议都会有所帮助。

最佳答案

要找到辐条,请使用靠近轴的小圆圈和靠近箍的大圆圈对图像进行采样。这些将拦截白色(角度)间隔。加入间隔的中间并检查辐条的连续性。当间隔缺失时,您可以外推到中心。

enter image description here

关于python - 从图像中查找车轮中的辐条总数 - OpenCV (Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49101544/

相关文章:

visual-c++ - HaarTraining 与 OpenCV 错误

java - 如何从 CPython 调用 java 对象和函数?

python - 如何让 iPython 在 Console2 上运行?

python - Numpy 3d数组映射操作

c++ - 在 OpenCV 和 C++ 中查找距轮廓中心最近的黑色像素

c++ - 如何计算二维对数色度?

python - Python 字典错误 : str object has no attribute append

Python TypeError : CommandEvent. GetString(): 参数太多

Python 2.7 和 PyDev - matplotlib 和 NumPy 不起作用

python - 取消新阵列与旧阵列的链接 : python