从 PyTesseract 裁剪边界框

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

我正在使用 pytesseract 来确定图像上的单个字符。我正在使用

image_to_boxes
函数,它为我提供了字符和坐标。但是,我需要将各个字符裁剪成单个图像文件。目前,下面的代码将创建边界框,但我需要它来裁剪字符并将其保存到每个字符的单独图像文件中。

filename = 'image.png'

# read the image and get the dimensions
img = cv2.imread(filename)
h, w, _ = img.shape # assumes color image

# run tesseract, returning the bounding boxes
boxes = pytesseract.image_to_boxes(img) 

# draw the bounding boxes on the image
for b in boxes.splitlines():
    b = b.split(' ')
    img = cv2.rectangle(img, (int(b[1]), h - int(b[2])), (int(b[3]), h - int(b[4])), (0, 255, 0), 2)
python image-processing ocr python-tesseract
1个回答
1
投票

您可以使用

pillow's crop
功能:

import matplotlib.pyplot as plt
from PIL import Image
img = Image.open("samp.png")

text = pytesseract.image_to_boxes(img).split("\n")

for i in text:
    if i:
        (left, upper, right, lower) = list(map(int, i.split(" ")[1:-1]))
        im_crop = img.crop((left, upper, right, lower))
        plt.imshow(im_crop)
        plt.show()

roi = img[lower:upper, left:right]
© www.soinside.com 2019 - 2024. All rights reserved.