-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathpreprocess.py
More file actions
53 lines (46 loc) · 1.48 KB
/
Copy pathpreprocess.py
File metadata and controls
53 lines (46 loc) · 1.48 KB
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
44
45
46
47
48
49
50
51
52
53
import numpy
import cv2
def contour_coordinates(contour):
'''
Returns coordinates of a contour
'''
if cv2.contourArea(contour) > 10:
M = cv2.moments(contour)
return (int(M['m10']/M['m00']))
def drawSquare(image):
'''
Draws a square around the found digits
'''
b = [0,0,0]
height, width = image.shape[0], image.shape[1]
if(height == width): ## if square
square = image
return square
else:
d_size = cv2.resize(image, (2*width, 2*height), interpolation=cv2.INTER_CUBIC)
height, width = height * 2, width * 2
if (height > width):
padding = (height - width)/2
d_size_square = cv2.copyMakeBorder(d_size, 0, padding, padding, cv2.BORDER_CONSTANT, value=b)
else:
padding = (width - height)/2
d_size_square = cv2.copyMakeBorder(d_size, padding, padding, 0, 0, cv2.BORDER_CONSTANT, value=b)
return d_size_square
def resize(image, dim):
'''
Returns orignal image resized to shape 'dim'
'''
b = [0,0,0]
dim = dim - 4
squared = image
r = (float(dim) / squared.shape[1])
d = (dim, int(squared.shape[0] * r))
resized = cv2.resize(image, d, interpolation = cv2.INTER_AREA)
height, width = resized.shape[0], resized[1]
if (height > width):
resized = cv2.copyMakeBorder(resized, 0,0,0,1, cv2.BORDER_CONSTANT, value=b)
if (height < width):
resized = cv2.copyMakeBorder(resized, 1,0,0,0, cv2.BORDER_CONSTANT, value=b)
resized = cv2.copyMakeBorder(resized, 2,2,2,2,cv2.BORDER_CONSTANT, value=b)
height, width = resized.shape[0], resized.shape[1]
return resized