【发布时间】:2018-06-14 09:04:45
【问题描述】:
我正在开发一个应用程序来使用 OpenCV 和 Tesseract 从图像中提取文本。我被困在一个地方当图像包含白色文本时以及图像混合了白色和其他颜色的情况。
如果文本不是白色,我可以轻松提取文本,但在文本为白色的情况下它不起作用。我正在使用 Tesseract 从文本中提取数据。 以下是我在传递给 tesseract 之前的图像处理代码:
image = cv2.imread(imgPath)
filename = getFileName()
img2gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#cv2.imwrite('python_scripts/temp/img2gray_' + filename, img2gray)
""" below removing shadow from image """
rgb_planes = cv2.split(img2gray)
result_planes = []
result_norm_planes = []
for plane in rgb_planes:
dilated_img = cv2.dilate(plane, np.ones((1, 1), np.uint8))
bg_img = cv2.medianBlur(dilated_img, 21)
diff_img = 255 - cv2.absdiff(plane, bg_img)
norm_img = diff_img
norm_img = cv2.normalize(diff_img, norm_img, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1)
result_planes.append(diff_img)
result_norm_planes.append(norm_img)
result = cv2.merge(result_planes)
result_norm = cv2.merge(result_norm_planes)
img2gray = result
origImage = image.copy()
_, new_img = cv2.threshold(img2gray, 80, 255, cv2.THRESH_BINARY) # for black text , cv.THRESH_BINARY_INV
# to manipulate the orientation of dilution , large x means horizonatally dilating more, large y means vertically dilating more
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (13, 13))
dilated = cv2.dilate(new_img, kernel, iterations=9) # dilate , more the iteration more the dilation
cv2.imwrite('python_scripts/temp/dilated_' + filename, dilated)
_, contours, _ = cv2.findContours(dilated, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE) # get contours
(imgH, imgW) = img2gray.shape[:2]
#logger.info("imgH: " + str(imgH) + ", imgW: " + str(imgW))
ctr = 0
contoursSort = {}
for contour in contours:
# to sort contours from top to bottom
[x, y, w, h] = cv2.boundingRect(contour)
contoursSort[y] = contour
orderedContours = collections.OrderedDict(sorted(contoursSort.items()))
for key, contour in orderedContours.items():
# get rectangle bounding contour
(x, y, w, h) = cv2.boundingRect(contour)
# Don't plot small false positives that aren't text
if w < 35 and h < 35:
continue
length = x + w
breadth = y + h
area = length * breadth
cv2.rectangle(origImage, (x, y), (x + w, y + h), (255, 255, 0), 2)
# don't consider contour which is touching the border
if x != 0 and y != 0 and x != imgW and y != imgH:
#logger.info("x: " + str(x) + ", y: " + str(y) + ", length: " + str(length)
# + ", breadth: " + str(breadth) + ", area: " + str(area))
croppedImg = origImage[y:(y + h), x:(x + w)]
croppedGray = cv2.cvtColor(croppedImg, cv2.COLOR_BGR2GRAY)
ctr = ctr + 1
th3 = cv2.adaptiveThreshold(croppedGray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,75,10)
#th3_height, th3_width = th3.shape[:2]
#th3 = cv2.resize(th3,(2*th3_width, 2*th3_height), interpolation = cv2.INTER_CUBIC)
th3 = cv2.GaussianBlur(th3,(5,5),0)
tessImgPath = 'python_scripts/temp/th3_' + str(ctr) + "_" + filename
cv2.imwrite(tessImgPath, th3)
tessText = runTesseract(tessImgPath)
os.remove(tessImgPath)
logger.info("tess text: " + str(tessText))
请指导我如何通过 OpenCV 处理图像,以便两者 可以单次提取白色和其他颜色的文本。
我现在尝试的一种方法是获取图像中的白色部分,如果它小于其他颜色,则执行图像的bitwise_xor,然后将其传递给 Tesseract。
提前致谢
【问题讨论】:
-
我也面临这个问题。它根本无法识别灰色或浅灰色文本。
-
@YogeshSanchihar 你可以通过
adaptiveThreshold或threshold获得灰色和浅灰色文本。如果无法这样做,请在任何免费保管箱中分享任何示例图像。谢谢
标签: python-3.x tesseract opencv3.0 python-tesseract