Search code examples
pythonopencvimage-processinglinenoise

How to delete (noisy and short) lines?


Using OpenCV with Python, I'm trying to find horizontal and vertical lines in my image.

This is my output :

vertical_lines

horizontal_lines

Well, I'm trying to detect only the long lines and delete the short (noisy) lines.

I hope that my output looks like this modified image:

Final_Horizontal_lines

I can provide you with my code if needed.


Solution

  • You can solve that problem by using contours.

    im2, contours, hierarchy = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    for contour in contours:
        (x, y, w, h) = cv2.boundingRect(contour)
        # if h<500: # check the length of verical lines 
        if w<500: # check the length of horizontal lines
            cv2.fillConvexPoly(gray,contour,0) #fill the contour with black color
    

    enter image description here