Search code examples
pythonimage-processinghough-transformopencv

Hough Lines detecting too many lines


I am trying to extract the vertical lines from the fabric image using hough lines in opencv. I applied contrast enhancement to enhance the lines and bilateral filtering to try and remove the other fabric textures. However, on applying the houghlines, the code detects lines all over the image. I tried playing around with the parameters for hough but the results were the same.

Input image after applying histogram equalization and bilateral filter:

enter image description here

Here is the image after applying the hough line, red representing the detected lines. Output showing hough detections:

enter image description here

What is another approach I can try so that the hough does not start detecting the minute fabric patterns as lines as well?

Here is the code I have:

`

    img1= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    img2 = cv2.equalizeHist(img1)
    img3 = cv2.equalizeHist(img2)
    img4 = cv2.equalizeHist(img3)
    img5 = cv2.bilateralFilter(img4, 9, 75,75)
    cv2.imshow("threshold",img5)
    edges = cv2.Canny(img4,50,127,apertureSize = 3)
    lines= cv2.HoughLines(edges, 1, math.pi/180.0, 200, np.array([]), 0, 0)
    a,b,c = lines.shape
    for i in range(a):
        rho = lines[i][0][0]
        theta = lines[i][0][1]
        a = math.cos(theta)
        b = math.sin(theta)
        x0, y0 = a*rho, b*rho
        pt1 = ( int(x0+1000*(-b)), int(y0+1000*(a)) )
        pt2 = ( int(x0-1000*(-b)), int(y0-1000*(a)) )
        cv2.line(img, pt1, pt2, (0, 0, 255), 2, cv2.LINE_AA)

cv2.imshow('image1',img)
cv2.waitKey(0)
cv2.destroyAllWindows()`

Solution

  • You need to threshold your equalized image, apply morphology to clean it up before doing the canny edge detection and hough line extraction. Using Python/OpenCV to do the following processing.

    Input:

    enter image description here

    import cv2
    import numpy as np
    import math
    
    # read image
    img = cv2.imread('fabric_equalized.png')
    
    # convert to grayscale
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    
    # threshold
    thresh = cv2.threshold(gray,165,255,cv2.THRESH_BINARY)[1]
    
    # apply close to connect the white areas
    kernel = np.ones((15,1), np.uint8)
    morph = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
    kernel = np.ones((17,3), np.uint8)
    morph = cv2.morphologyEx(morph, cv2.MORPH_CLOSE, kernel)
    
    # apply canny edge detection
    edges = cv2.Canny(img, 175, 200)
    
    # get hough lines
    result = img.copy()
    lines= cv2.HoughLines(edges, 1, math.pi/180.0, 165, np.array([]), 0, 0)
    a,b,c = lines.shape
    for i in range(a):
        rho = lines[i][0][0]
        theta = lines[i][0][1]
        a = math.cos(theta)
        b = math.sin(theta)
        x0, y0 = a*rho, b*rho
        pt1 = ( int(x0+1000*(-b)), int(y0+1000*(a)) )
        pt2 = ( int(x0-1000*(-b)), int(y0-1000*(a)) )
        cv2.line(result, pt1, pt2, (0, 0, 255), 2, cv2.LINE_AA)
    
    
    # save resulting images
    cv2.imwrite('fabric_equalized_thresh.jpg',thresh)
    cv2.imwrite('fabric_equalized_morph.jpg',morph)
    cv2.imwrite('fabric_equalized_edges.jpg',edges)
    cv2.imwrite('fabric_equalized_lines.jpg',result)
    
    # show thresh and result    
    cv2.imshow("thresh", thresh)
    cv2.imshow("morph", morph)
    cv2.imshow("edges", edges)
    cv2.imshow("result", result)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    


    Thresholded image:

    enter image description here

    Morphology cleaned image:

    enter image description here

    Edge image:

    enter image description here

    Resulting Hough Lines:

    enter image description here