是否可以在python中使用opencv提取彩色背景区域内的文本?

2024-06-17 12:57:30 发布

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

我拥有原始图像的以下表格区域:

enter image description here

我试图从这个表中提取文本。但是当使用阈值时,整个灰色区域变暗。例如,如下所示

enter image description here

我使用的阈值类型

thresh_value = cv2.threshold(original_gray, 128, 255, cv2.THRESH_BINARY_INV +cv2.THRESH_OTSU)[1]

是否有可能提取并将灰色背景更改为白色,然后让文本像素保持原样(如果为黑色)


Tags: 图像文本区域类型thresholdvalue阈值cv2
1条回答
网友
1楼 · 发布于 2024-06-17 12:57:30

您应该在Python/OpenCV中使用自适应阈值

输入:

enter image description here

import cv2
import numpy as np

# read image
img = cv2.imread("text_table.jpg")

# convert img to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# do adaptive threshold on gray image
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 11)

# write results to disk
cv2.imwrite("text_table_thresh.jpg", thresh)

# display it
cv2.imshow("thresh", thresh)
cv2.waitKey(0)


结果

enter image description here

相关问题 更多 >