pythonopencvcomputer-visionimage-morphologydilation

Using dilation in only one direction?


This is my first post on Stack Overflow, so I am sorry if the problem isn't defined enough.

I am currently working on extracting table data from images and I need a way to dilate the text only in a vertical direction so that I can get clear column representation that will be used for further segmentation.

After removing horizontal and vertical lines and transforming the image bitwise, I am at this stage:

Current state after dilation and line extraction

The ideal goal for this problem would be:

The goal

Is there a method or an algorithm that would be helpful in my case?


Solution

  • You can just call cv2.dilate with the appropriate structuring element.

    import cv2
    
    pre_img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
    h, w = pre_img.shape
    
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, ksize=(1, 2 * h))
    
    dilated = cv2.dilate(pre_img, kernel)
    
    cv2.imshow('input', pre_img)
    cv2.imshow('output', dilated)
    cv2.waitKey(0)
    

    Input input image

    Output output image

    To visualize better what's happening:

    blended = (pre_img.astype(float) + dilated.astype(float)) / 2
    cv2.imshow('blended', blended.astype(np.uint8))
    cv2.waitKey(0)
    

    Blended image blend