从图像中提取矩形内的文本

问题描述 投票:1回答:1

我有一个带有多个红色矩形图像的图像,并且输出效果很好。

我正在使用https://github.com/autonise/CRAFT-Remade进行文本识别

原始:

Original

我的图片:

IMAGE

我尝试使用pytesserac仅在所有矩形中提取文本,但没有成功。输出结果:

r

2

aseeaaei

ae

如何准确地从此图像中正确提取文本?

部分代码:

def saveResult(img_file, img, boxes, dirname='./result/', verticals=None, texts=None):
        """ save text detection result one by one
        Args:
            img_file (str): image file name
            img (array): raw image context
            boxes (array): array of result file
                Shape: [num_detections, 4] for BB output / [num_detections, 4] for QUAD output
        Return:
            None
        """
        img = np.array(img)

        # make result file list
        filename, file_ext = os.path.splitext(os.path.basename(img_file))

        # result directory
        res_file = dirname + "res_" + filename + '.txt'
        res_img_file = dirname + "res_" + filename + '.jpg'

        if not os.path.isdir(dirname):
            os.mkdir(dirname)

        with open(res_file, 'w') as f:
            for i, box in enumerate(boxes):
                poly = np.array(box).astype(np.int32).reshape((-1))
                strResult = ','.join([str(p) for p in poly]) + '\r\n'
                f.write(strResult)

                poly = poly.reshape(-1, 2)
                cv2.polylines(img, [poly.reshape((-1, 1, 2))], True, color=(0, 0, 255), thickness=2) # HERE
                ptColor = (0, 255, 255)
                if verticals is not None:
                    if verticals[i]:
                        ptColor = (255, 0, 0)

                if texts is not None:
                    font = cv2.FONT_HERSHEY_SIMPLEX
                    font_scale = 0.5
                    cv2.putText(img, "{}".format(texts[i]), (poly[0][0]+1, poly[0][1]+1), font, font_scale, (0, 0, 0), thickness=1)
                    cv2.putText(img, "{}".format(texts[i]), tuple(poly[0]), font, font_scale, (0, 255, 255), thickness=1)

        # Save result image
        cv2.imwrite(res_img_file, img)

发表评论后,结果如下:

Modified

和tesseract结果适合首次测试,但不准确:

400
300
200

“2615

1950



24
16
python opencv image-recognition
1个回答
0
投票

[使用Pytesseract提取文本时,对图像进行预处理非常重要。通常,我们要对文本进行预处理,以使要提取的文本为黑色,背景为白色。为此,我们可以使用Otsu的阈值来获取二进制图像,然后执行形态学运算以过滤和去除噪声。这是管道:

  • 将图像转换为灰度并调整大小
  • Otsu的二进制图像阈值
  • 反转图像并执行形态学操作
  • 查找轮廓
  • 使用轮廓近似,长宽比和轮廓区域进行过滤
  • 消除不必要的噪音
  • 执行文本识别

[转换为灰度后,我们使用imutils.resize()调整大小图像,然后使用Otsu的二值图像阈值。现在图像只有黑色或白色,但是仍然有不想要的噪点

从这里开始,我们将图像反转并使用水平核执行形态学操作。此步骤将文本合并到一个轮廓中,我们可以在其中过滤并删除不需要的线条和小斑点

现在,我们使用轮廓逼近,宽高比和轮廓区域的组合来找到轮廓和过滤器,以隔离不需要的部分。去除的噪声以绿色突出显示

现在消除了噪点,我们再次将图像反转为所需的黑色文本,然后执行文本提取。我还注意到,略微模糊可以增强识别度。这是我们在[]上执行文本提取的清洁图像

我们为Pytesseract提供imutils.resize()配置,因为我们希望将图像视为统一的文本块。这是来自Pytesseract的结果

--psm 6

输出不是完美的,但接近。您可以尝试其他配置设置6745 63 6 10.50 2245 21 18 17 525 4 22 0.18 400 4 a 0.50 300 3 4 0.75 200 2 3 0.22 2575 24 3 0.77 1950 ii 12 133

here
© www.soinside.com 2019 - 2024. All rights reserved.