Filip Jurković
Filip Jurković

Reputation: 65

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?

Upvotes: 6

Views: 6988

Answers (2)

Cris Luengo
Cris Luengo

Reputation: 60504

It looks like you don’t want a dilation, but a maximum projection. For each column, check to see if any pixel is set. Use numpy.any for that:

result = np.any(image, axis=0)

Upvotes: 4

ndrplz
ndrplz

Reputation: 1654

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

Upvotes: 6

Related Questions