import time import sys import cv2 import operator from itertools impor

  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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import time
import sys
import cv2
import operator
from itertools import imap
import numpy as np
from matplotlib import pyplot as plt
import boxcutter
def readImage(filename):
img = cv2.imread(filename, cv2.CV_LOAD_IMAGE_GRAYSCALE)[245:727 + 245, 90:1198 + 90]
img[img < 60] = 0
img[img > 0] = 255
contours, _ = cv2.findContours(img, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
rect = cv2.boundingRect(c)
region = np.matrix(img[rect[1]:rect[1] + rect[3], rect[0]:rect[0] + rect[2]])
box = {
'x': rect[0],
'y': rect[1],
'data': region
}
yield box
def splitByMinimalColumn(box, minValue = 1):
minCol = -1
minSum = 9999
for i in xrange(box['data'].shape[1]):
s = np.sum(box['data'][:,i])
if s < minSum:
minSum = s
minCol = i
if minSum <= minValue:
left = { 'x': box['x'],
'y': box['y'],
'data': box['data'][:,:minCol]
}
right = { 'x': box['x'] + minCol + 1,
'y': box['y'],
'data': box['data'][:,minCol+1:]
}
return [left, right]
return [box]
def splitByPixel(box):
for i in xrange(box['data'].shape[1] - 1):
for j in xrange(box['data'].shape[0]):
if np.sum(box['data'][:j,i]) + np.sum(box['data'][j:,i+1]) == 0:
#left = { 'x': box['x'],
#'y': box['y'],
#'data': box['data'][:,:i+1]
#}
#left['data'][:j,-1] = 0
#right = { 'x': box['x'] + i + 1,
#'y': box['y'],
#'data': box['data'][:,i:]
#}
#right['data'][j:,0] = 0
#return [left, right]
left = { 'x': box['x'],
'y': box['y']
#'data': box['data'][:,:i+1]
}
right = { 'x': box['x'] + i + 1,
'y': box['y']
#'data': box['data'][:,i:]
}
left['data'], right['data'] = np.split(box['data'], [i+1], axis=1)
left['data'][:j,-1] = 0
right['data'][j:,0] = 0
#print left, right
return [left, right]
if i > 0 and np.sum(box['data'][:j,i]) + np.sum(box['data'][j:,i-1]) == 0:
left = { 'x': box['x'],
'y': box['y']
#'data': box['data'][:,:i-1]
}
right = { 'x': box['x'] + i,
'y': box['y']
}
left['data'], right['data'] = np.split(box['data'], [i-1], axis=1)
left['data'][j:,-1] = 0
right['data'][:j,0] = 0
#print 'asdadsadas'
return [left, right]
return [box]
def cleanSymbol(box):
cols, rows = np.nonzero(box['data'])
cols = cols.tolist()[0]
rows = rows.tolist()[0]
if rows[-1] + 1 == box['data'].shape[0] and cols[-1] + 1 == box['data'].shape[1]:
return box
print cols
print
print rows
for i in range(cols[0]):
box['data'] = box['data'][:, 1:]
box['x'] += 1
for i in range(rows[0]):
box['data'] = box['data'][1:, :]
box['y'] += 1
for i in range(cols[0]+1, box['data'].shape[0]):
box['data'] = box['data'][:-1, :]
for i in range(rows[0]+1, box['data'].shape[1]):
box['data'] = box['data'][:, :-1]
#while np.sum(box['data'][:,0]) == 0:
#box['data'] = box['data'][:, 1:]
#box['x'] += 1
#while np.sum(box['data'][:,-1]) == 0:
#box['data'] = box['data'][:, :-1]
#while np.sum(box['data'][0,:]) == 0:
#box['data'] = box['data'][1:, :]
#box['y'] += 1
#while np.sum(box['data'][-1,:]) == 0:
#box['data'] = box['data'][:-1, :]
return box
def splitSymbols(box):
if box['data'].shape[1] <= 15:
return [box]
parts = splitByPixel(box)
if len(parts) == 1:
parts = splitByMinimalColumn(box)
if len(parts) == 1:
return [box]
c = imap(cleanSymbol, parts)
r = imap(splitSymbols, c)
t = reduce(list.__add__, r)
return t
def work():
res = []
reduce(list.__add__, imap(splitSymbols, readImage(sys.argv[1])))
import cProfile
cProfile.run('work()')