【问题标题】:Enhancement of Image for OCROCR 图像增强
【发布时间】:2018-03-16 08:34:14
【问题描述】:

[这是示例图片]

我想为 OCR 裁剪其他几个类似彩色图像的标题文本。预处理图像以更好地识别标题文本的最有效步骤是什么。

【问题讨论】:

  • 什么是标题文本?只是前 4 行文本还是最后 4 行?如果是前4个,是哪一个?只有 2-3-4 ?
  • 自上而下 1-2-3-4

标签: python opencv ocr


【解决方案1】:

注意

致所有想要复制代码并希望在其他项目中使用它的人:您必须调整和调整它(尤其是阈值/内核/迭代值)。 此版本在用户提供的图像上效果最佳。

import cv2

image = cv2.imread("image.jpg")
image_c = image.copy()

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)  # grayscale
cv2.imshow('gray', gray)
cv2.waitKey(0)

_, thresh = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)  # threshold
cv2.imshow('thresh', thresh)
cv2.waitKey(0)

kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))

dilated = cv2.dilate(thresh, kernel, iterations=13)  # dilate
cv2.imshow('dilated', dilated)
cv2.waitKey(0)

image, contours, hierarchy = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)  # get contours

# for each contour found, draw a rectangle around it on original image
for i, contour in enumerate(contours):
    # get rectangle bounding contour
    x, y, w, h = cv2.boundingRect(contour)

    roi = image_c[y:y + h, x:x + w]

    if 50 < h < 100 or 200 < w < 420:  # these values are specific for this example

        # draw rectangle around contour on original image
        rect = cv2.rectangle(image_c, (x, y), (x + w, y + h), (255, 255, 255), 1)
        cv2.imshow('rectangles', rect)
        cv2.waitKey(0)

        cv2.imwrite('extracted{}.png'.format(i), roi)


# write original image with added contours to disk - change values above to (255,0,255) to see clearly the contours
cv2.imwrite("contoured.jpg", image_c)

【讨论】:

  • 我不明白为什么这个答案还没有被接受!
【解决方案2】:

也许您可以先尝试检测文本,然后从检测到的区域获取最大行索引并将其剪切。有多种方法可以使用 opencv 检测文本。你可以试试this question here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-17
    • 1970-01-01
    • 2011-06-03
    • 2013-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多