-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimg_utils.py
43 lines (37 loc) · 1.28 KB
/
img_utils.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import os
import cv2
import numpy as np
# remove white borders at top and bottom of image
def remove_white_border(img):
y_top, y_bottom = 0, len(img) - 1
border_removed = False
for y in range(len(img)):
for x in range(len(img[0])):
if img[y,x] != 0:
y_top = y
border_removed = True
break
if border_removed:
break
border_removed = False
for y in reversed(range(len(img))):
for x in range(len(img[0])):
if img[y,x] != 0:
y_bottom = y
border_removed = True
break
if border_removed:
break
return img[y_top : y_bottom + 1, :]
# rescale image (distorts aspect ratio)
def rescale_image(img, width, height):
return cv2.resize(img, (width, height), interpolation=cv2.INTER_LINEAR)
# other processes
def enhance_image(img):
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # grayscaling
img = cv2.GaussianBlur(img, (3,3), 0) # gaussian bluring
_, img = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU) # otsu thresholding
img = cv2.bitwise_not(img) # inverse color for better feature extraction
img = np.array(img).astype('float32')
img /= 255.0 # pixel normalization
return img