-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpreprocessor.py
More file actions
28 lines (20 loc) · 802 Bytes
/
preprocessor.py
File metadata and controls
28 lines (20 loc) · 802 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import cv2
import numpy as np
def prepare(input):
# preprocessing the image input
clean = cv2.fastNlMeansDenoising(input)
ret, tresh = cv2.threshold(clean, 127, 1, cv2.THRESH_BINARY_INV)
img = crop(tresh)
# 40x10 image as a flatten array
flatten_img = cv2.resize(img, (40, 10), interpolation=cv2.INTER_AREA).flatten()
# resize to 400x100
resized = cv2.resize(img, (400, 100), interpolation=cv2.INTER_AREA)
columns = np.sum(resized, axis=0) # sum of all columns
lines = np.sum(resized, axis=1) # sum of all lines
h, w = img.shape
aspect = w / h
return [*flatten_img, *columns, *lines, aspect]
def crop(img):
points = cv2.findNonZero(img)
x, y, w, h = cv2.boundingRect(points)
return img[y: y+h, x: x+w]