使用OpenCV Hough变换在2D点云中进行线检测

Jak*_*kas 6 python opencv image-processing line computer-vision

我已尽力找出如何使用OpenCV进行线路检测。但是,我找不到所需的示例。我想用它在简单的二维点云中找到线。作为测试,我想使用以下几点:

在此处输入图片说明

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

a = np.random.randint(1,101,400)  # Random points.
b = np.random.randint(1,101,400)  # Random points.

for i in range(0, 90, 2):  # A line to detect
    a = np.append(a, [i+5])
    b = np.append(b, [0.5*i+30])

plt.plot(a, b, '.')
plt.show()
Run Code Online (Sandbox Code Playgroud)

我已经找到了有关霍夫变换如何工作的许多初始示例。但是,当涉及到代码示例时,我只能发现已使用图像。

有没有一种方法可以使用OpenCV Hough变换来检测一组点中的线,或者可以推荐其他方法或库吗?

----编辑----

阅读了一些很好的答案之后,我觉得我应该描述一下我打算使用它的目的。我有高分辨率的2D LiDAR,需要从数据中提取墙。打字扫描如下所示: 在此处输入图片说明

“正确的输出”如下所示: 在此处输入图片说明

在进行了更多研究之后,我怀疑霍夫变换在这种情况下使用效果不佳。关于我应该寻找的任何提示?

(如果有人感兴趣,可以使用LiDAR和墙面提取来生成地图并导航机器人。)

谢谢,雅各布

Nik*_*ble 2

一种方法是按照这些幻灯片自行实现霍夫变换,跳过边缘检测部分。

或者,您可以从点列表创建图像,例如

#create an image from list of points
x_shape = int(np.max(a) - np.min(a))
y_shape = int(np.max(b) - np.min(b))

im = np.zeros((x_shape+1, y_shape+1))

indices = np.stack([a-1,b-1], axis =1).astype(int)
im[indices[:,0], indices[:,1]] = 1

plt.imshow(im)

#feed to opencv as usual
Run Code Online (Sandbox Code Playgroud)

按照这个问题的答案

编辑:不要输入 OpenCV,而是使用 skimage,如文档中所述

import numpy as np

from skimage.transform import (hough_line, hough_line_peaks,
                               probabilistic_hough_line)
from skimage.feature import canny
from skimage import data

import matplotlib.pyplot as plt
from matplotlib import cm


# Constructing test image
#image = np.zeros((100, 100))
#idx = np.arange(25, 75)
#image[idx[::-1], idx] = 255
#image[idx, idx] = 255

image = im

# Classic straight-line Hough transform
h, theta, d = hough_line(image)

# Generating figure 1
fig, axes = plt.subplots(1, 3, figsize=(15, 6))
ax = axes.ravel()

ax[0].imshow(image, cmap=cm.gray)
ax[0].set_title('Input image')
ax[0].set_axis_off()

ax[1].imshow(np.log(1 + h),
             extent=[np.rad2deg(theta[-1]), np.rad2deg(theta[0]), d[-1], d[0]],
             cmap=cm.gray, aspect=1/1.5)
ax[1].set_title('Hough transform')
ax[1].set_xlabel('Angles (degrees)')
ax[1].set_ylabel('Distance (pixels)')
ax[1].axis('image')

ax[2].imshow(image, cmap=cm.gray)
for _, angle, dist in zip(*hough_line_peaks(h, theta, d)):
    y0 = (dist - 0 * np.cos(angle)) / np.sin(angle)
    y1 = (dist - image.shape[1] * np.cos(angle)) / np.sin(angle)
    ax[2].plot((0, image.shape[1]), (y0, y1), '-r')
ax[2].set_xlim((0, image.shape[1]))
ax[2].set_ylim((image.shape[0], 0))
ax[2].set_axis_off()
ax[2].set_title('Detected lines')

plt.tight_layout()
plt.show()

# Line finding using the Probabilistic Hough Transform
image = data.camera()
edges = canny(image, 2, 1, 25)
lines = probabilistic_hough_line(edges, threshold=10, line_length=5,
                                 line_gap=3)

# Generating figure 2
fig, axes = plt.subplots(1, 3, figsize=(15, 5), sharex=True, sharey=True)
ax = axes.ravel()

ax[0].imshow(image, cmap=cm.gray)
ax[0].set_title('Input image')

ax[1].imshow(edges, cmap=cm.gray)
ax[1].set_title('Canny edges')

ax[2].imshow(edges * 0)
for line in lines:
    p0, p1 = line
    ax[2].plot((p0[0], p1[0]), (p0[1], p1[1]))
ax[2].set_xlim((0, image.shape[1]))
ax[2].set_ylim((image.shape[0], 0))
ax[2].set_title('Probabilistic Hough')

for a in ax:
    a.set_axis_off()

plt.tight_layout()
plt.show()
Run Code Online (Sandbox Code Playgroud)

结果

  • 您能否将“#feed to opencv as通常”替换为POC代码,以显示您的方法在这种情况下确实有效? (2认同)