我正在使用 pytesseract 和 opencv 来擦除图像的边界。使用 tesseract 从图像中提取文本
这是我根据这篇文章写的源代码。 以编程方式删除图像中的所有线条和边框(保留文本)的方法是什么?
import cv2
import os
try:
from PIL import Image
except ImportError:
import Image
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract'
image = cv2.imread("D://ocrtestimg//id.jpg")
result = image.copy()
gray = cv2.cvtColor(result, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (30,1))
remove_horizontal = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
cnts = cv2.findContours(remove_horizontal, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(result, [c], -1, (255,255,255), 5) # contours 그리기
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,30))
remove_vertical = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
cnts = cv2.findContours(remove_vertical, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(result, [c], -1, (255,255,255), 5)
cv2.imshow('thresh', thresh)
cv2.imshow('result', result)
cv2.imwrite('result.png', result)
cv2.waitKey()
image = cv2.imread('result.png')
result = image.copy()
gray = cv2.cvtColor(result, cv2.COLOR_BGR2GRAY)
filename = "{}.png".format(os.getpid())
print(filename)
cv2.imwrite(filename, gray)
#Simple image to string- OCR
text = pytesseract.image_to_string(Image.open(filename), lang='eng')
os.remove(filename)
print(text)
cv2.imshow("Image", image)
cv2.waitKey(0)
图像质量很差,所以表格中的垂直线被破坏了。我需要做哪些额外的工作才能擦除这条断线?即使您通过导入图像来增加 dpi 或增加照片的大小,垂直虚线也不会被删除。