I'm trying to extract the names from a table in an image as part of an OCR project.
For each image, I am trying to get bounding boxes around the name fields. I've managed to get bounding boxes on the names column but for some reason it's also detecting contours around the characters of the names inside each cell as well.
I have two questions:
Why are contours being detected around the characters, and how can I get them to appear around words instead?
Right now, I have to specify the location of the bounding boxes. How can I do this automatically?
Here's my code:
import cv2
from matplotlib import pyplot as plt
import numpy as np
file = r'Corrected_images\table_deskew3.png'
table_image_contour = cv2.imread(file, 0)
table_image = cv2.imread(file)
ret, thresh_value = cv2.threshold(table_image_contour, 180, 255, cv2.THRESH_BINARY_INV)
kernel = np.ones((5, 5), np.uint8)
dilated_value = cv2.dilate(thresh_value, kernel, iterations=1)
contours, hierarchy = cv2.findContours(dilated_value, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
x, y, w, h = cv2.boundingRect(cnt)
# bounding the
if 842 < x < 2215 and 1288 < y:
table_image = cv2.rectangle(table_image, (x, y), (x + w, y + h), (0, 0, 255), 3)
roi = table_image[y: y + h, x: x + w]
#plt.imshow(roi)
#plt.show()
plt.imshow(table_image)
plt.show()
cv2.imwrite('target.png', table_image)
cv2.namedWindow('detectable', cv2.WINDOW_NORMAL)
Have you considered making the image grayscale then passing a glow filter through it? That way the word merges into a solid entity.