【问题标题】:Probabilistic Hough lines returning only angled lines仅返回有角度线的概率霍夫线
【发布时间】:2021-03-08 22:25:05
【问题描述】:

我正在尝试使用 opencv-python 中的 HoughLinesP 解析平面图以将它们转换为线坐标数组,并且该函数仅返回有角度的线,并且与我的图像中的实际线无关。

这是我的代码:

import cv2

# Read image and convert to grayscale
img = cv2.imread('C:/Data/images/floorplan/extremely basic.png', -1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Get lines with probabilistic hough lines
found_lines = cv2.HoughLinesP(gray, 1, 3.14 / 160, 100,
                              minLineLength=1, maxLineGap=10)

# Loop through found lines and draw each line on original image
for line in found_lines:
    x1, y1, x2, y2 = line[0]
    cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 1)

# Show image, wait until keypress, close windows.
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

以下是分别返回阈值 150 和 100 的内容:

我尝试修改所有选项并尝试非概率霍夫线无济于事。

【问题讨论】:

  • 您的输入图像尺寸是多少?是不是很小?
  • 我尝试了多张具有多种尺寸的图像,但出现了同样的错误:随机倾斜的线条与原始图像无关,但对阈值有反应。
  • 我猜 maxLineGap=10 是问题所在。这些线之间的像素大于 10 尝试 40 或 50
  • 你能分享源图像,我们也可以试试吗?这种问题通常是因为您分配给该函数的参数
  • 我已将原始图像添加到 OP

标签: python opencv machine-learning computer-vision


【解决方案1】:

问题在于图像反转和参数。你必须做进一步的调整,因为这并没有给出所有的行。

代码是在 google colab 上测试的。删除 from google.colab.patches import cv2_imshow 并将 cv_imshow 替换为 cv2.imshow 以供本地使用。

部分图像输出

代码

import cv2
import numpy as np
from google.colab.patches import cv2_imshow

# Read image and convert to grayscale
img = cv2.imread('1.jpg', 0)
#gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)


s1, s2 = img.shape
new_img = np.zeros([s1,s2],dtype=np.uint8)
new_img.fill(255)


ret,thresh1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV)
cv2_imshow(thresh1)


kernel = np.ones((3,3),np.uint8)
erosion = cv2.erode(thresh1,kernel,iterations = 1)
cv2_imshow(erosion)


# Get lines with probabilistic hough lines
found_lines = cv2.HoughLinesP(erosion, np.pi/180, np.pi/180, 10, minLineLength=4, maxLineGap=4)

# Loop through found lines and draw each line on original image
for line in found_lines:
    x1, y1, x2, y2 = line[0]
    cv2.line(new_img, (x1, y1), (x2, y2), (0, 0, 255), 1)
    cv2_imshow(new_img)
    #cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 1)

# Show image, wait until keypress, close windows.
print("ORIGINAL IMAGE:")
cv2_imshow(img)
#cv2.imshow('image', img)
#cv2.waitKey(0)
#cv2.destroyAllWindows()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多