简体   繁体   中英

Cut bounding box of text and save it as sub images of main image

I need to extract the bounding box of text and save it as sub-images of main image.
I have this code for detecting text on an image and bounding it by a green rectangle (look image example below), I have done this step but in the second step I try to extract each bounding box into a separate image, could any help? my image is a scanned document that does not have a noisy background.

在此处输入图像描述

    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 images are numpy arrays which can be sliced easily:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM