【问题标题】:Crop bounding box from PyTesseract来自 PyTesseract 的裁剪边界框
【发布时间】:2021-01-22 08:59:17
【问题描述】:

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

    你可以使用pillow's crop函数:

    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]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-07
      • 2020-08-12
      • 2020-03-19
      • 2016-04-05
      • 2018-10-24
      • 1970-01-01
      相关资源
      最近更新 更多