【问题标题】:Multiple line detection in HoughLinesP openCV functionHoughLinesP openCV 函数中的多线检测
【发布时间】:2018-04-24 04:45:44
【问题描述】:

我是 Python 和 OpenCV 的新手。我正在尝试使用来自互联网的代码使用 HoughLinesP 函数检测单行,检测到 3-4 行。我尝试使用 maxLineGap 变量但没有帮助。

输入图像: 输出图像:

import sys
import math
import cv2 as cv
import numpy as np

def main(argv):

    default_file =  "line.png"
    filename = argv[0] if len(argv) > 0 else default_file
    # Loads an image
    src = cv.imread(filename, cv.IMREAD_GRAYSCALE)
    # Check if image is loaded fine
    if src is None:
        print ('Error opening image!')
        print ('Usage: hough_lines.py [image_name -- default ' + default_file + '] \n')
        return -1


    dst = cv.Canny(src, 50, 200, None, 3)

    # Copy edges to the images that will display the results in BGR
    cdst = cv.cvtColor(dst, cv.COLOR_GRAY2BGR)
    cdstP = np.copy(cdst)

    lines = cv.HoughLines(dst, 1, np.pi / 180, 150, None, 0, 0)

    if lines is not None:
        for i in range(0, len(lines)):
            rho = lines[i][0][0]
            theta = lines[i][0][1]
            a = math.cos(theta)
            b = math.sin(theta)
            x0 = a * rho
            y0 = b * rho
            pt1 = (int(x0 + 1000*(-b)), int(y0 + 1000*(a)))
            pt2 = (int(x0 - 1000*(-b)), int(y0 - 1000*(a)))
            cv.line(cdst, pt1, pt2, (0,0,255), 3, cv.LINE_AA)


    linesP = cv.HoughLinesP(dst, 1, np.pi / 180, 50, None, 50, 150)

    no_of_Lines = 0
    if linesP is not None:
        for i in range(0, len(linesP)):
            l = linesP[i][0]
            no_of_Lines = no_of_Lines + 1
            cv.line(cdstP, (l[0], l[1]), (l[2], l[3]), (0,0,255), 3, cv.LINE_AA)



    print('Number of lines:' + str(no_of_Lines))

    cv.imshow("Source", src)
    cv.imshow("Detected Lines (in red) - Standard Hough Line Transform", cdst)

    cv.imshow("Detected Lines (in red) - Probabilistic Line Transform", cdstP)

    cv.waitKey()
    return 0

if __name__ == "__main__":
    main(sys.argv[1:])

【问题讨论】:

  • 我在你的输出图像中只看到一行
  • 要检查我使用了计数器的行数,它显示为 4。
  • 如果您没有注意到,在应用 Canny 后,您的图像中有几行。为什么 HoughLinesP 应该返回一行?

标签: opencv image-processing hough-transform


【解决方案1】:

Canny 边缘检测器的输出不止一行。结果,函数cv.HoughLines() 返回不止一行。您需要对图像进行骨架化,以便将所有线条合并为一条。

这就是我所做的

由于这是一个简单的图像,我在 Canny 边缘输出上执行了几个形态学操作。膨胀后腐蚀。如果你注意到下面的代码,我使用了更大的内核大小来执行腐蚀,从而得到一条细线。

补充代码:

kernel = cv.getStructuringElement(cv.MORPH_ELLIPSE, (3, 3))
dilation = cv.dilate(dst, kernel, iterations = 1)
kernel1 = cv.getStructuringElement(cv.MORPH_ELLIPSE, (5, 5))
erosion = cv.erode(dilation, kernel1, iterations = 1)

输出:

这是我在 python 控制台上得到的:

Number of lines:1

侵蚀后的输出:

霍夫线变换的输出:

概率霍夫线变换的输出:

注意:

在尝试识别它们之前,请始终确保图像中有细线。

【讨论】:

  • 这是按预期工作的,但是当我尝试提供三角形或矩形时,没有检测到 lin 或更少的线。有什么想法吗?
  • @Neha 你能发布你尝试过的结果吗?
【解决方案2】:

将 Canny 应用于粗线的结果是粗线的轮廓。这给了你多行。因此,您不能期望 Hough 变换会产生一条线。

您有两个选择:合并输出行或预处理您的输入,使其仅包含一行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-17
    • 1970-01-01
    • 2021-08-04
    • 2016-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多