是否可以在 python 中使用 opencv 提取彩色背景区域内的文本?
is it possible to extracting text inside colored background region using opencv in python?
我有以下来自原始图像的 table 区域:
我正在尝试从这个 table.But 中提取文本,当使用阈值时,整个灰色区域得到 darkening.For 示例,如下所示,
我用过的阈值类型,
thresh_value = cv2.threshold(original_gray, 128, 255, cv2.THRESH_BINARY_INV +cv2.THRESH_OTSU)[1]
是否可以提取灰色背景并将其更改为白色,然后让文本像素保持原样?
您应该在 Python/OpenCV 中使用自适应阈值。
输入:
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)
结果
我有以下来自原始图像的 table 区域:
我正在尝试从这个 table.But 中提取文本,当使用阈值时,整个灰色区域得到 darkening.For 示例,如下所示,
我用过的阈值类型,
thresh_value = cv2.threshold(original_gray, 128, 255, cv2.THRESH_BINARY_INV +cv2.THRESH_OTSU)[1]
是否可以提取灰色背景并将其更改为白色,然后让文本像素保持原样?
您应该在 Python/OpenCV 中使用自适应阈值。
输入:
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)
结果