-
Notifications
You must be signed in to change notification settings - Fork 0
/
newfcns.py
270 lines (223 loc) · 7.13 KB
/
newfcns.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 cv2
import numpy as np
import glob as gb
import matplotlib.pyplot as plt
scale = 5
# following smoothing windows are scaled from here by /scale
# values below reflect a nominal image width of 1670
deriv_win_size = 12 # 20 = 1.2% of image width
smooth_size= -30 # <0: do not smooth
blur_rad = 25
if blur_rad%2 == 0:
blur_rad += 1
#img_width = int(3000/scale)
#img_height = int(4000/scale)
#img_width = int(1671)
#img_height = int(1206)
#
# new functions by BH
#
##
#
# Apply criteria to gaps
#
def Gap_filter(gaps,img, tmin=20, blacklabel=6):
img_height=img.shape[0]
img_width=img.shape[1]
# tmin: min width pixels
# blacklabel: int label of background black
halfway = int(img_height/2)
candidates = []
for g in gaps:
#
# exclude
#
# 1) narrow gaps
width = abs(g[0]-g[1])
if width < tmin:
print('found a very narrow gap')
continue
# 2) gaps that match background
values = []
for c1 in range(width):
col = g[0]+c1-1
for r in range(halfway):
row = halfway + r -1
if col < img_width:
values.append(img[row,col])
(val,cnts) = np.unique(values, return_counts=True)
if val[np.argmax(cnts)] == blacklabel: # background
print('found a black gap')
continue
else: # we didn't exclude this gap
candidates.append(g)
return candidates
#
#
#
def Gen_gaplist(cross):
cm1=0
gaps = []
for c in cross:
if c < 0:
c = c*-1
gaps.append([cm1,c])
cm1 = c
return gaps
#
# neg and pos zero crossings
#
def Find_crossings(yvals):
ym1 = yvals[0]
c = []
for i,y in enumerate(yvals):
if y<0 and ym1>=0:
c.append(-i) # - == neg crossing
if y>0 and ym1 <= 0:
c.append(i) # positive crossing
ym1 = y
return c
def Est_derivative(yvals, w):
if w < 0:
return np.gradient(line,2)
if w > len(yvals)/2:
print(' derivative window {} is too big for {} values.'.format(w,len(yvals)))
quit()
else:
ym1 = yvals[0]
dydn = []
dn = 1 # for now
for y in yvals:
dy = y-ym1
dydn.append(dy/dn)
ym1 = y
if w>1:
dydn = smooth(dydn, window_len=w, window='hanning')
return dydn
def Find_edges_line(line):
#grad = np.gradient(line,2)
grad = Est_derivative(line, 3)
thresh = 0.1
edges = []
for i,gv in enumerate(grad):
if abs(gv) > thresh:
edges.append(i)
# get "edge of edges"
e2 = []
ep = edges[0]
for e in edges:
if e-ep > 1: # leading edge of each gradient peak
e2.append(e)
ep = e
edges = e2
return edges
#
# select a horizontal strip
# return a series of labels
#
def Trancept_labeled(lab_img, yval):
img_height=img.shape[0]
img_width=img.shape[1]
r = int(yval)
result = []
for c in range(img_width):
lab_pix = lab_img[r,c]
result.append(lab_pix)
return result
#
# Same as Trancept but return most common
# label in a vertical bar of width bw
#
# cluster mean = avg value of pixel labels
def Trancept_bar_labeled(lab_img, yval,bw):
img_height=lab_img.shape[0]
img_width=lab_img.shape[1]
y_val = int(yval) # y=row, x=col
result = []
offset = int(bw/2)
vv_array = []
for x in range(img_width):
vertvals = []
for i in range(bw):
y = y_val-offset + i
if y < img_height:
vertvals.append(lab_img[y,x])
if len(vertvals) > 1:
(val,cnts) = np.unique(vertvals,return_counts=True)
result.append(val[np.argmax(cnts)]) # return most common label in the vert bar
else:
result.append(-2)
vv_array.append(vertvals)
return result, vv_array
#
# Cluster colors by K-means
#
def KM(img,N):
img_height=img.shape[0]
img_width=img.shape[1]
pixels = np.float32(img.reshape(-1, 3))
n_colors = N
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 200, .05)
flags = cv2.KMEANS_RANDOM_CENTERS
_, labels, (centers) = cv2.kmeans(pixels, n_colors, None, criteria, 10, flags)
_, counts = np.unique(labels, return_counts=True)
#And finally the dominant colour is the palette colour which occurs most frequently on the quantized image:
dominant = centers[np.argmax(counts)]
labeled_image = labels.reshape(img.shape[:-1])
#print('i label (n pix) Pallette')
#for i in range(len(labels)):
#print('{} {} ({}) '.format(i,labels[i],counts[i]), palette[i])
# from float back to 8bit
centers = np.uint8(centers)
labels = labels.flatten()
newimg = centers[labels.flatten()]
#reshape
newimg = newimg.reshape(img.shape)
return [newimg, labeled_image]
def smooth(x,window_len=11,window='hanning'):
"""smooth the data using a window with requested size.
This method is based on the convolution of a scaled window with the signal.
The signal is prepared by introducing reflected copies of the signal
(with the window size) in both ends so that transient parts are minimized
in the begining and end part of the output signal.
input:
x: the input signal
window_len: the dimension of the smoothing window; should be an odd integer
window: the type of window from 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'
flat window will produce a moving average smoothing.
output:
the smoothed signal
example:
t=linspace(-2,2,0.1)
x=sin(t)+randn(len(t))*0.1
y=smooth(x)
see also:
np.hanning, np.hamming, np.bartlett, np.blackman, np.convolve
scipy.signal.lfilter
TODO: the window parameter could be the window itself if an array instead of a string
NOTE: length(output) != length(input), to correct this: return y[(window_len/2-1):-(window_len/2)] instead of just y.
"""
#assert x.ndim != 1, "smooth only accepts 1 dimension arrays."
#print ('input data:', len(x))
#print (x)
assert (len(x) > window_len),"Input vector needs to be bigger than window size."
if window_len<3:
return x
if window_len%2 == 0:
print(' smoothing window length must be ODD')
quit()
assert (window in ['flat', 'hanning', 'hamming', 'bartlett', 'blackman']), "Window must be: 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'"
s=np.r_[x[window_len-1:0:-1],x,x[-2:-window_len-1:-1]]
#print(len(s))
if window == 'flat': #moving average
w=np.ones(window_len,'d')
else:
w=eval('np.'+window+'(window_len)')
y=np.convolve(w/w.sum(),s,mode='valid')
#return y to original length
extra = len(y)-len(x)
endchop = int(extra/2)
print('extra: {}, endchop: {}'.format(extra, endchop))
z = y[endchop:-endchop]
print('Orig len: {} New len: {}'.format(len(x), len(z)))
return z