【问题标题】:Cut bounding box of text and save it as sub images of main image剪切文本的边界框并将其保存为主图像的子图像
【发布时间】:2021-01-09 12:13:29
【问题描述】:

我需要提取文本的边界框并将其保存为主图像的子图像。
我有这段代码用于检测图像上的文本并用绿色矩形对其进行边界(看下面的图像示例),我已经完成了这一步,但在第二步中我尝试将每个边界框提取到单独的图像中,有什么帮助吗? 我的图片是没有嘈杂背景的扫描文档。

    IMAGE_PATH = 'test1.png'
    reader = easyocr.Reader(['en'])
    result = reader.readtext(IMAGE_PATH)
    result

img = cv2.imread(IMAGE_PATH)
spacer = 100
for detection in result: 
    top_left = tuple([int(val) for val in detection[0][0]])
    bottom_right = tuple([int(val) for val in detection[0][2]])
    text = detection[1]
    font= cv2.FONT_HERSHEY_SIMPLEX
    img = cv2.rectangle(img, top_left, bottom_right, (0,255,0), 2)
    #img = cv2.putText(img,text,(20,spacer), font, 1,(0,255,0),2,cv2.LINE_AA)
    spacer+=3
plt.figure(figsize=(30,30))    
plt.imshow(img)
plt.show

【问题讨论】:

  • 您是在问如何在 OpenCV 中裁剪? opencv 中的图像是 numpy 数组(形状为 [高度、宽度、通道])。给定左上角 (tl) 和右下角 (br),您可以使用 img[tl[1]:br[1], tl[0]:br[0], :] 裁剪框

标签: python image-processing computer-vision ocr


【解决方案1】:

OpenCV 图像是可以轻松切片的 numpy 数组:

cropped_img = img[top_left[1]:bottom_right[1], top_left[0]:bottom_right[0], :] 
plt.imsave('cropped.jpg', cropped_img )

【讨论】:

    猜你喜欢
    • 2012-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-03
    相关资源
    最近更新 更多