从包含选项卡的图像中提取文本

2024-03-28 21:54:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个图像,其中包含一个表。我需要从中提取文本。我试着先删除水平线和垂直线,但似乎不起作用。下面是我使用的代码。你知道吗

import cv2

import numpy as np

img = cv2.imread(r'A13205.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 175, 255, cv2.THRESH_BINARY)[1]

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (4, 4))
morph_img = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)

#inverse the image, so that lines are black for masking
morph_img_inv = cv2.bitwise_not(morph_img)

#perform bitwise_and to mask the lines with provided mask
masked_img = cv2.bitwise_xor(thresh, thresh, mask = morph_img)

Input from which text must be extracted


Tags: the图像文本importimgmaskcv2kernel