Neha
Neha

Reputation: 21

Multiple line detection in HoughLinesP openCV function

I am new to Python and OpenCV. I am trying to detect single line with HoughLinesP function with code from the internet, 3-4 lines are detected. I tried with maxLineGap variable but not helpful.

Input image: enter image description here Output Image: enter image description here

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:])

Upvotes: 0

Views: 2846

Answers (2)

Jeru Luke
Jeru Luke

Reputation: 21243

The output of your Canny edge detector has more than one line. As a result the function cv.HoughLines() returns more than one line. You need to skeletonize your image so that all lines are merged to one.

Here is what I did

Since this is a simple image I performed a couple of morphological operations on the Canny edge output. Dilation followed by erosion. If you notice the code below, I used a bigger kernel size to perform erosion so as to get a thin line.

Supplement Code :

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)

Output :

This is what I get on my python console:

Number of lines:1

Output after erosion:

enter image description here

Output of Hough line transform:

enter image description here

Output of Probabilistic Hough line transform:

enter image description here

Note:

Always ensure fine lines in the image before trying to identify them.

Upvotes: 2

Piglet
Piglet

Reputation: 28994

The result of applying Canny to a thick line is the outline of that thick line. That gives you multiple lines. Hence you cannot expect Hough transform to yield a single line.

You have two options: Merge the output lines or preprocess your input so it will onyl contain a single line.

Upvotes: 1

Related Questions