-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathold_main.py
270 lines (209 loc) · 6.55 KB
/
old_main.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import image, sensor, framebuf, time, random, math, gc
from ulab import numpy as np
sensor.shutdown(True)
gc.enable()
##### MATH #####
def addNum(img, num):
myImg = img.copy()
for x in range(myImg.width()):
for y in range(myImg.height()):
newVal = myImg.get_pixel(x,y)+num
if newVal<0: newVal=0
if newVal>255: newVal=255
myImg.set_pixel(x, y, newVal)
return myImg
##### UTIL #####
def resetImage(img_path):
myImage = image.Image(imgPath, copy_to_fb = True)
myImage.to_grayscale(copy_to_fb=True)
return myImage
def ndarrayToImage(arr, doesCopy):
shape = arr.shape()
if len(shape) == 1:
width = shape[0]
height = 1
else:
height, width = shape
newImg = image.Image(width, height, sensor.GRAYSCALE, copy_to_fb=doesCopy)
newImg.to_grayscale(copy_to_fb=doesCopy)
if height==1:
for i in range(width):
newImg[i] = int(math.ceil(arr[i]))
else:
for x in range(width):
for y in range(height):
newImg.set_pixel(x, y, int(math.ceil(arr[y][x])))
sensor.flush()
time.sleep_ms(1000)
return newImg
def imgToArray(img):
return np.array(img, dtype=np.uint8)
def printSig(img):
ar = imgToArray(img)
for i in range(len(ar[0])):
print(ar[0][i])
###### PREPROCESSING ######
def calculateK(img):
width = img.width()
height = img.height()
sqSize = 15
numIter = 300
lowBound = img.get_statistics().mean()
upBound = 0.9
tempK = 0
randX = random.randint(1, 500)
randY = random.randint(1, 500)
for _ in range(numIter):
minMax = upBound
for _ in range(12):
random.seed(randX * randY)
randX = random.randint(sqSize, width - sqSize - 1)
randY = random.randint(sqSize, height - sqSize - 1)
#print(randX, randY)
#print(img)
square = img.copy(roi=(randX, randY, sqSize, sqSize), copy=False)
maxVal = square.get_statistics().max()
if (maxVal < minMax) and (maxVal > lowBound) and (maxVal < upBound):
minMax = maxVal
del square
tempK = tempK + minMax
k = tempK / numIter
return k
def calculateContrast(img_path, img):
width = img.width()
height = img.height()
meanVal = img.get_statistics().mean()
for x in range(width):
for y in range(height):
img.set_pixel(x, y, img.get_pixel(x, y) - meanVal)
d2img = img.gamma_corr(0.25)
d2 = d2img.get_statistics().mean()
d = math.sqrt(d2)
m4img = d2img.gamma_corr(0.5)
del d2img
m4 = m4img.get_statistics().mean()
del m4img
alfa4 = d / math.pow(d2, 2)
C = d / pow(alfa4, (1.0/4.0))
C = 10000.0/C
return C
def contrastEnhance(img, C, k):
width = img.width()
height = img.height()
for x in range(width):
for y in range(height):
currVal = img.get_pixel(x,y)
if currVal >= k:
newVal = int(currVal * C)
if newVal > 255:
newVal = 255
img.set_pixel(x,y, newVal)
def preprocess(img_path, img):
k = calculateK(img)
C = calculateContrast(img_path, img)
del img
img = resetImage(img_path)
contrastEnhance(img, C, k)
img = img.median(3)
##### GRIDDING #####
def getProjections(mat):
Hlines = np.mean(mat, axis=1)
Vlines = np.mean(mat, axis=0)
return (Hlines, Vlines)
def calculateKernelSize(proj):
length = len(proj)
accum = 0
numNonZero = 0
wasZero = True
for i in range(length):
val = proj[i]
isNonZero = val > 0
if isNonZero: accum = accum+1
if not isNonZero and not wasZero: numNonZero = numNonZero+1
wasZero = not isNonZero
return math.ceil(accum/numNonZero)
def getReconstruction(marker, mask, kernelSize):
size = max(2, min(kernelSize, 21))
m1 = marker.copy()
for i in range(15):
m0 = m1.copy()
m1.dilate(size).min(mask)
m0.difference(m1)
empty = True
for x in range(m0.width()):
if not empty: break
for y in range(m0.height()):
if m0.get_pixel(x,y)!=0:
empty=False
break
if empty: break
return m1
def calculateSignals(projections, kernelSizes):
H, V = projections
kernelH, kernelV = kernelSizes
Hmean = H.get_statistics().mean()
Vmean = V.get_statistics().mean()
_H = addNum(H, -Hmean)
_V = addNum(V, -Vmean)
Hrec = getReconstruction(_H, H, kernelH)
Vrec = getReconstruction(_V, V, kernelV)
H_mark = H.copy().sub(Hrec)
V_mark = V.copy().sub(Vrec)
return (H_mark, V_mark)
def getBinarySignals(signals):
Hsig, Vsig = signals
Hthresh = Hsig.get_histogram().get_threshold().value()
Vthresh = Vsig.get_histogram().get_threshold().value()
Hbin = Hsig.copy().binary([(0,Hthresh)], invert=True)
Vbin = Vsig.copy().binary([(0,Vthresh)], invert=True)
return (Hbin, Vbin)
def getLines(binSig):
spot = []
lspot = []
flag = True
t = 0
for i in range(binSig.width()):
if flag:
if binSig.get_pixel(i, 0)>254:
spot.append(i)
flag = not flag
t = 0
else:
t = t+1
if t>4 and binSig.get_pixel(i,0)<1:
spot.append(i)
flag = not flag
lspot.append(spot[0])
for i in range(1,len(spot)-1, 2):
val = math.ceil((spot[i]+spot[i+1])/2)
lspot.append(val)
lspot.append(spot[len(spot)-1])
return lspot
def adjustToDevice(img, lines):
def gridding(img):
mat = imgToArray(img)
Hproj, Vproj = getProjections(mat)
Hkernel = calculateKernelSize(Hproj)
Vkernel = calculateKernelSize(Vproj)
HlinesImg = ndarrayToImage(np.array(Hproj, dtype=np.uint8), False)
VlinesImg = ndarrayToImage(np.array(Vproj, dtype=np.uint8), False)
Hsig, Vsig = calculateSignals((HlinesImg, VlinesImg),(Hkernel, Vkernel))
Hbin, Vbin = getBinarySignals((Hsig, Vsig))
Hlines = getLines(Hbin)
Vlines = getLines(Vbin)
print(Hlines, Vlines)
return (Hbin, Vbin)
###########################
##### MAIN #####
#ogPath = "/sample.bmp"
imgPath = "/mysample.bmp"
myImage = image.Image(imgPath, copy_to_fb = True)
myImage.to_grayscale(copy_to_fb=True)
preprocess(imgPath, myImage)
myImage.save("/contrasted.bmp")
gc.collect()
Htest, Vtest = gridding(myImage)
#print(imgToArray(Htest), imgToArray(Vtest))
Htest.save("/Htest.bmp")
Vtest.save("/Vtest.bmp")
time.sleep_ms(2000)