Extract bounding box and save it as an image

The following will give you a single letter

letter = im[y:y+h,x:x+w]

Here's an approach:

  • Convert image to grayscale
  • Otsu's threshold to obtain a binary image
  • Find contours
  • Iterate through contours and extract ROI using Numpy slicing

After finding contours, we use cv2.boundingRect() to obtain the bounding rectangle coordinates for each letter.

x,y,w,h = cv2.boundingRect(c)

To extract the ROI, we use Numpy slicing

ROI = image[y:y+h, x:x+w]

Since we have the bounding rectangle coordinates, we can draw the green bounding boxes

cv2.rectangle(copy,(x,y),(x+w,y+h),(36,255,12),2)

Here's the detected letters

enter image description here

Here's each saved letter ROI

enter image description here

import cv2

image = cv2.imread('1.png')
copy = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray,0,255,cv2.THRESH_OTSU + cv2.THRESH_BINARY)[1]

cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

ROI_number = 0
for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    ROI = image[y:y+h, x:x+w]
    cv2.imwrite('ROI_{}.png'.format(ROI_number), ROI)
    cv2.rectangle(copy,(x,y),(x+w,y+h),(36,255,12),2)
    ROI_number += 1

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