-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.py
118 lines (96 loc) · 3.44 KB
/
index.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import numpy as np
import cv2
import sys, getopt
import math
def load_files(inputfile):
print('Loading file...')
return cv2.imread(inputfile)
def parse_args():
inputfile = ''
outputfile = ''
scale = '1.2'
debug = False
text = ''
try:
opts, args = getopt.getopt(sys.argv[1:],'dhi:o:s:t:',['ifile=','ofile=', 'scale=', 'debug', 'text='])
except getopt.GetoptError:
print( 'test.py -i <inputfile> -o <outputfile> -s <scale ammount>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print( 'test.py -i <inputfile> -o <outputfile>')
sys.exit()
elif opt in ('-i', '--ifile'):
inputfile = arg
elif opt in ('-o', '--ofile'):
outputfile = arg
elif opt in ('-s', '--scale'):
scale = arg
elif opt in ('-d', '--debug'):
debug = True
elif opt in ('-t', '--text'):
text = arg
return {'inputfile': inputfile, 'outputfile' : outputfile, 'scale': scale, 'debug': debug, 'text': text}
def find_and_order_contours(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(1,1),5000)
ret,thresh = cv2.threshold(blur,127,255,cv2.THRESH_BINARY)
edged = cv2.Canny(thresh, 0, 50, apertureSize=5)
# cv2.namedWindow("output", cv2.WINDOW_NORMAL) # Create window with freedom of dimensions
# imS = cv2.resize(edged, (1000, 1000)) # Resize image
# cv2.imshow("output", edged) # Show image
# cv2.waitKey(0)
contours, hierarchy = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
return sorted(contours, key=lambda x: cv2.contourArea(x), reverse=True)
def bounding_debug(image, border):
cv2.drawContours(image,[border] , -1, (255, 255, 0), 5)
M = cv2.moments(border)
cX = int(M['m10'] / M['m00'])
cY = int(M['m01'] / M['m00'])
cv2.drawContours(image, [border], -1, (0, 255, 0), 2)
cv2.circle(image, (cX, cY), 7, (255, 255, 255), -1)
cv2.putText(image, 'center', (cX - 20, cY - 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
# ---- MAIN PROGRAM ----
config = parse_args()
original_image = load_files(config['inputfile'])
border = find_and_order_contours(original_image)[0]
if config['debug']:
bounding_debug(original_image,border)
rect = cv2.minAreaRect(border)
box = cv2.boxPoints(rect)
box = np.int0(box)
scale = float(config['scale'])
W = rect[1][0]
H = rect[1][1]
Xs = [i[0] for i in box]
Ys = [i[1] for i in box]
x1 = min(Xs)
x2 = max(Xs)
y1 = min(Ys)
y2 = max(Ys)
angle = rect[2]
rotated = False
if angle < -45:
angle += 90
rotated = True
center = (int((x1+x2)/2), int((y1+y2)/2))
size = (int(scale*(x2-x1)), int(scale*(y2-y1)))
M = cv2.getRotationMatrix2D((size[0]/2, size[1]/2), angle, 1.0)
cropped = cv2.getRectSubPix(original_image, size, center)
cropped = cv2.warpAffine(cropped, M, size)
croppedW = W if not rotated else H
croppedH = H if not rotated else W
original_image = cv2.getRectSubPix(
cropped, (int(croppedW*scale), int(croppedH*scale)), (size[0]/2, size[1]/2))
rows,cols = original_image.shape[:2]
if config['text']:
cv2.putText(original_image, config['text'], (10, int(croppedH*scale) - 10),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2)
print('saving...')
if config['outputfile']:
cv2.imwrite(config['outputfile'], original_image)
else:
op = config['inputfile'].rsplit( ".", 1 )
op = op[0] + '-op.' + op[1]
cv2.imwrite(op, original_image)