From 1011c7b5e96b5acb0d360bf8f871a7c98afd4a1e Mon Sep 17 00:00:00 2001 From: Rusty Striker Date: Fri, 12 Jan 2024 14:10:37 +0200 Subject: [PATCH] first commit --- classify.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 classify.py diff --git a/classify.py b/classify.py new file mode 100644 index 0000000..7effe41 --- /dev/null +++ b/classify.py @@ -0,0 +1,18 @@ +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 +