用hough变换或其他图像处理算法检测非直线

2024-09-30 08:28:09 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有一种方法可以检测不完全笔直的线条?在

我有代表矩形的对象,但由于使用广角相机失真和质量不好的预处理,所以有点不均匀。另外,我必须事先做一个透视转换,这是另一个因素,较差的线质量。在

在用canny过滤器检测到边缘后,我得到了以下图像: Object with not straight edgeshough transform

我试着用hough线算法找出边缘线。但由于形状质量不好,有很多凹凸不平,所以无法找到倾斜的边缘。在

我尝试了正常的hough线变换(红线)和概率hough线变换(绿线),但结果相当糟糕。在

有没有其他方法可以检测到这种情况?或者有没有办法改善我的形象,让我得到直线?线条的变形是可变的,所以很难修复。在

这里还有一个例子:

example 2 - edges example 2 - hough results

我使用的是python3.4和opencv3.2,numpy1.12。 这是一个新的解决方法。在


Tags: 对象方法图像算法过滤器质量代表边缘
1条回答
网友
1楼 · 发布于 2024-09-30 08:28:09

你的边缘非常清晰,对概率Hough线变换来说绝对足够好。我想你只需要多玩一些自由参数。在

import numpy as np
import matplotlib.pyplot as plt
from skimage.transform import probabilistic_hough_line
from skimage import draw

def restore_lines(distorted):
    lines = probabilistic_hough_line(distorted,
                                     threshold=2,
                                     line_length=20,
                                     line_gap=15)

    restored = np.zeros_like(distorted, dtype=np.uint8)
    for line in lines:
        p0, p1 = line
        rr, cc = draw.line(p0[1], p0[0], p1[1], p1[0])
        restored[rr, cc] += 1
    return restored


# distorted = plt.imread('backslash.png')
distorted = plt.imread('tick.png')

# imread returns non-grayscale image in this case
distorted = distorted[:,:,0]

# restore
restored = restore_lines(distorted)

fig, axes = plt.subplots(1,2)
axes[0].imshow(distorted, cmap='gray', interpolation='none')
axes[1].imshow(restored, cmap='gray', interpolation='none')
axes[0].set_title('Original')
axes[1].set_title('Restored')
for ax in axes:
    ax.set_xticks([])
    ax.set_yticks([])

enter image description here

enter image description here

相关问题 更多 >

    热门问题