computer_vision_project/classify.py
2024-01-12 14:10:37 +02:00

19 lines
668 B
Python

import cv2 as cv
import numpy as np
import h5py as h5
# Extract letter from a bounding box
def extract_bb(img, bb):
# Get the bounding box
rect = cv.minAreaRect(bb.astype(np.float32).transpose())
# will be useful later, map center and size to ints
center, size = tuple(map(int, rect[0])), tuple(map(int, rect[1]))
# Calculate rotation matrix and rotate the image
rot_matrix = cv.getRotationMatrix2D(center, rect[2], 1)
rot_img = cv.warpAffine(img, rot_matrix, (img.shape[1], img.shape[0]))
# bounding box is now axis aligned, and we can crop it
print(size)
cropped = cv.getRectSubPix(rot_img, size, center)
return cropped