[Python] How to Detect Thick Lines as Single Lines Using Hough Transform in OpenCV

Discussão em 'Python' iniciado por Stack, Março 15, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using OpenCV's HoughLinesP function to detect straight lines in an image. When the image contains thin lines, the detection works perfectly. However, when the image contains thick lines, the algorithm detects them as two parallel lines instead of a single line.

    Here's my current code:

    import cv2
    import numpy as np

    image_path = "thickLines.png"
    image = cv2.imread(image_path)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # Thresholding to create a binary image
    _, binary = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY_INV)

    # Edge Detection
    edges = cv2.Canny(binary, 50, 150, apertureSize=3)

    # Hough Line Transform to Detect Walls
    lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 100, minLineLength=50, maxLineGap=5)

    # Draw Detected Walls
    if lines is not None:
    for line in lines:
    x1, y1, x2, y2 = line[0]
    cv2.line(image, (x1, y1), (x2, y2), (0, 255, 0), 2) # Draw thick lines in green

    # Show Final Processed Image
    cv2.imshow("Detected Image", image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


    I have tried adjusting the Canny edge thresholds and modifying minLineLength and maxLineGap, but the issue persists.

    My goal is to detect thick lines as a single line instead of two parallel lines.

    Questions:

    1. How can I modify my approach to merge or simplify detected thick lines into a single line?
    2. Are there any parameters in HoughLinesP that can be tuned to achieve this?

    Screenshots
    Here are screenshots of the issue:
    The green lines represents the detected lines from the image [​IMG] [​IMG]

    Here are sample images of thick & thin lines [​IMG] [​IMG]

    Continue reading...

Compartilhe esta Página