【问题标题】:Hough lines missing some lines霍夫线缺少一些线
【发布时间】:2023-02-08 06:29:07
【问题描述】:

我正在尝试使用相对较低的 threshold 5 来检测不规则图像中的线条。我得到的结果如下:

其中红线是计算线。但是,我希望黄线也能满足参数。有谁知道为什么没有检测到黄线?这是我的代码:

# img
rho = 1  # distance resolution in pixels of the Hough grid
theta = np.pi / 180  # angular resolution in radians of the Hough grid
threshold = 5  # minimum number of votes (intersections in Hough grid cell)
min_line_length = 200  # minimum number of pixels making up a line
max_line_gap = 500  # maximum gap in pixels between connectable line segments

low_threshold = 50
high_threshold = 150

edge_image = img.copy()
edge_image = cv2.GaussianBlur(edge_image, (3, 3), 1)
edges = cv2.Canny(edge_image, low_threshold, high_threshold)

line_image = np.copy(edges)  # creating a blank to draw lines on
line_image = cv2.cvtColor(line_image, cv2.COLOR_GRAY2BGR)

lines = cv2.HoughLinesP(img, rho, theta, threshold, np.array([]),
                    min_line_length, max_line_gap)

for line in lines:
    for x1,y1,x2,y2 in line:
        cv2.line(line_image,(x1,y1),(x2,y2),(0,0,255),1)

【问题讨论】:

    标签: opencv


    【解决方案1】:

    发布的解决方案几乎没有问题:

    • HoughLinesP 应用于imglines = cv2.HoughLinesP(img...,本应应用于edges
    • 在具有此类“细线”的图像上使用GaussianBlurCanny 是不推荐的(结果是每条线被复制两次)。
      我建议应用二进制阈值和dilate(使用 2x2 内核进行扩展)。
    • min_line_length = 200 太长 - 黄色短线的长度约为 120 像素。
    • 我们可以更好地校准参数(max_line_gap = 500 例如没有意义)。

    代码示例:

    import cv2
    import numpy as np
    
    img = cv2.imread('input_image_with_lines.png')  # Read image as BGR
    
    # Apply threshold to each color channel for converting all the non-black pixels to white (needed to the usage of automatic threshold instead of manual threshold).
    b_thres = cv2.threshold(img[:, :, 0], 0, 255, cv2.THRESH_OTSU)[1]  # Apply automatic threshold to the blue channel
    g_thres = cv2.threshold(img[:, :, 1], 0, 255, cv2.THRESH_OTSU)[1]  # Apply automatic threshold to the green channel
    r_thres = cv2.threshold(img[:, :, 2], 0, 255, cv2.THRESH_OTSU)[1]  # Apply automatic threshold to the red channel
    thres_image = b_thres | g_thres | r_thres  # thres_image is combined threshold images
    
    #dilated_thres_image = cv2.dilate(thres_image, np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]], np.uint8))
    dilated_thres_image = cv2.dilate(thres_image, np.ones((2, 2), np.uint8))  # Dilate thres_image with very small kernel - makes the lines thinker. 
    
    # img
    rho = 0.5  #1 distance resolution in pixels of the Hough grid
    theta = np.pi / 180  # angular resolution in radians of the Hough grid
    threshold = 5  # minimum number of votes (intersections in Hough grid cell)
    min_line_length = 50 #200  # minimum number of pixels making up a line
    max_line_gap = 2 #500  # maximum gap in pixels between connectable line segments
    
    #low_threshold = 50
    #high_threshold = 150
    
    #edge_image = img.copy()
    #edge_image = cv2.GaussianBlur(edge_image, (3, 3), 1)
    #edges = cv2.Canny(edge_image, low_threshold, high_threshold)
    
    #lines = cv2.HoughLinesP(img, rho, theta, threshold, np.array([]),
    #                    min_line_length, max_line_gap)
    
    lines = cv2.HoughLinesP(dilated_thres_image, rho, theta, threshold, None, min_line_length, max_line_gap)
    
    line_image = cv2.cvtColor(dilated_thres_image, cv2.COLOR_GRAY2BGR) # creating a blank to draw lines on
    
    for line in lines:
        for x1,y1,x2,y2 in line:
            #cv2.line(line_image,(x1,y1),(x2,y2),(0,0,255),1)
            cv2.line(line_image, (x1,y1), (x2,y2), (0,255,0), 1)
    
    
    # Display images for testing
    cv2.imshow('img', img)
    cv2.imshow('thres_image', thres_image)
    cv2.imshow('dilated_thres_image', dilated_thres_image)
    cv2.imshow('line_image', line_image)
    cv2.waitKey()
    cv2.destroyAllWindows()
    

    输出:

    我意识到结果并不完美,但我们必须考虑 Hough-Lines 算法有其局限性......

    【讨论】:

      猜你喜欢
      • 2014-10-07
      • 2012-07-06
      • 1970-01-01
      • 1970-01-01
      • 2013-03-09
      • 2020-08-11
      • 2020-11-01
      • 2015-02-16
      • 2011-12-17
      相关资源
      最近更新 更多