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

标签 python image opencv image-processing image-recognition

我有一个包含多个红色矩形图像提取和输出的图像。

我正在使用 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

最佳答案

在使用Pytesseract 提取文本时,对图像进行预处理是极其重要的。通常,我们希望对文本进行预处理,使要提取的所需文本为黑色,背景为白色。为此,我们可以使用 Otsu 的阈值来获得二值图像,然后执行形态学操作来过滤和去除噪声。这是一个管道:

  • 将图像转换为灰度并调整大小
  • Otsu 的二值图像阈值
  • 反转图像并执行形态学操作
  • 寻找轮廓
  • 使用轮廓近似、纵横比和轮廓面积进行过滤
  • 去除不需要的噪音
  • 执行文本识别

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

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

现在我们找到轮廓并使用轮廓近似、纵横比和轮廓区域的组合进行过滤,以隔离不需要的部分。移除的噪声以绿色突出显示

现在噪声已被移除,我们再次反转图像以使所需的文本变为黑色,然后执行文本提取。我还注意到添加轻微的模糊可以增强识别度。这是我们对其执行文本提取的清理图像

我们为 Pytesseract 提供 --psm 6 配置,因为我们希望将图像视为统一的文本 block 。这是 Pytesseract 的结果

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

import cv2
import pytesseract
import imutils

pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"

# Resize, grayscale, Otsu's threshold
image = cv2.imread('1.png')
image = imutils.resize(image, width=500)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Invert image and perform morphological operations
inverted = 255 - thresh
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (15,3))
close = cv2.morphologyEx(inverted, cv2.MORPH_CLOSE, kernel, iterations=1)

# Find contours and filter using aspect ratio and area
cnts = cv2.findContours(close, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    area = cv2.contourArea(c)
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.01 * peri, True)
    x,y,w,h = cv2.boundingRect(approx)
    aspect_ratio = w / float(h)
    if (aspect_ratio >= 2.5 or area < 75):
        cv2.drawContours(thresh, [c], -1, (255,255,255), -1)

# Blur and perform text extraction
thresh = cv2.GaussianBlur(thresh, (3,3), 0)
data = pytesseract.image_to_string(thresh, lang='eng',config='--psm 6')
print(data)

cv2.imshow('close', close)
cv2.imshow('thresh', thresh)
cv2.waitKey()

关于python - 从图像中提取矩形内的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58546623/

相关文章:

python - 通过网络抓取使用帖子从网站获取结果

c# - 在 C# 中有效地调整图像的对比度

python - 读取视频文件时 OpenCV 内存不足

android - Android 中的 FFMPEG 视频录制覆盖绿色锁存器

python - 每个测试用例都有自己的日志文件/获取下一个测试用例的名称

python - 如何将 for 循环输出写入 csv 文件?

vb.net - 如何防止Image.FromFile()方法锁定文件

ios - 将图像数组下载为字节数组并转换为图像

opencv - 给定 OpenCV 中的立体视差图,我如何找到左右图像之间的真实像素偏移(每个像素)?

python - setup.py install 和 setup.py develop 的区别