This repository has been archived by the owner on Jan 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.py
176 lines (145 loc) · 4.83 KB
/
tools.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
import matplotlib
import matplotlib.pyplot as plt
from skimage.color import rgb2gray
import math as m
import numpy as np
from copy import copy
from skimage import data, io, filters, util
import IPython
UMPX=87.3/1216
#useful functions
def derivation(V,i):
#naivelly simple numerical derivation
return V[i+1]-V[i]
def array_der(V):
#derivation of an array
dV=[]
for i in range(len(V)-1):
dV.append(derivation(V,i))
dV.append(dV[-1])
return dV
def split(m):
#splits the list into two lists, one for indices, one for values, filters zero values
x=[]
y=[]
for i in range(len(m)):
if m[i]!=0:
x.append(i)
y.append(m[i])
return [x, y]
def widths(sw):
#returns widths and distances in pixels from the list in the form of the splitted list (function 'split')
w=[]
d=[]
start=sw[0]
for i in range(len(sw)-1):
if sw[i]!=sw[i+1]-1:
w.append(sw[i]-start)
d.append(sw[i+1]-sw[i])
start=sw[i+1]
w.append(sw[-1]-start)
return w, d
def wires_analysis(area_color, display=True):
#profile (average of values in x direction)
area=rgb2gray(area_color)
y=[]
wires=[]
gaps=[]
for r in area:
y.append(sum(r))
#derivation
dy=array_der(y)
#identification of wires and gaps:
for i in range(len(y)):
if m.fabs(dy[i])<max(dy)*0.65:
if y[i]< np.average(y):
wires.append(y[i])
gaps.append(0)
else:
gaps.append(y[i])
wires.append(0)
else:
wires.append(0)
gaps.append(0)
#gaining the right form of the data using some functions
s_wires=split(wires)[0]
w_wires=widths(s_wires)[0]
d_wires=widths(s_wires)[1]
if display:
print('widths of wires [px]: {}'.format(w_wires))
print('widths of gaps [px]: {}'.format(d_wires))
print('average width: {:.3f} um'.format(np.average(w_wires)*UMPX))
print('average distance: {:.3f} um'.format(np.average(d_wires)*UMPX))
print('std width: {:.3f} um'.format(np.std(w_wires)*UMPX))
print('std distance: {:.3f} um'.format(np.std(d_wires)*UMPX))
print('together: {:.3f} um'.format(np.average(w_wires)*UMPX+np.average(d_wires)*UMPX))
#shows the recognized wires in red
area_wires=copy(area_color)
for i in s_wires:
area_wires[i,:]=(180,0,0)
plt.imshow(area_wires)
plt.savefig('tmp/wires_red.png', dpi=200, bbox_inches='tight')
#graph
fig = plt.figure()
ax = fig.add_axes([0.2, 0.2, 0.7, 0.7])
ax.grid(linestyle='--')
ax.plot(np.arange(len(y)), y, linewidth=2)
ax.set_title('Intensity profile in the image')
ax.set_xlabel('x direction [px]')
ax.set_ylabel('intensity a.u.')
# ax.plot(np.arange(len(dy)), dy, linewidth=2)
# ax.plot(np.arange(len(wires)), wires, linewidth=2)
# ax.scatter(split(gaps)[0], split(gaps)[1], linewidth=2)
return w_wires, d_wires
'''
def wires_analysis(area_color):
#profile (average of values in x direction)
area=rgb2gray(area_color)
y=[]
wires=[]
gaps=[]
for r in area:
y.append(sum(r))
#derivation
dy=f.array_der(y)
#identification of wires and gaps:
for i in range(len(y)):
if m.fabs(dy[i])<max(dy)*0.65:
if y[i]< np.average(y):
wires.append(y[i])
gaps.append(0)
else:
gaps.append(y[i])
wires.append(0)
else:
wires.append(0)
gaps.append(0)
#gaining the right form of the data using some functions
s_wires=f.split(wires)[0]
w_wires=f.widths(s_wires)[0]
d_wires=f.widths(s_wires)[1]
print(w_wires)
print(d_wires)
print('average width: {:.3f} um'.format(np.average(w_wires)/14))
print('average distance: {:.3f} um'.format(np.average(d_wires)/14))
print('std width: {:.3f} um'.format(np.std(w_wires)/14))
print('std distance: {:.3f} um'.format(np.std(d_wires)/14))
print('together: {:.3f} um'.format(np.average(w_wires)/14+np.average(d_wires)/14))
#shows the recognized wires in red
area_wires=copy(area_color)
for i in s_wires:
area_wires[i,:]=(255,0,0)
plt.imshow(area_wires)
plt.savefig('wires_red.png', dpi=200, bbox_inches='tight')
#graph
fig = plt.figure()
ax = fig.add_axes([0.2, 0.2, 0.7, 0.7])
ax.grid(linestyle='--')
ax.plot(np.arange(len(y)), y, linewidth=2)
ax.set_title('Intensity profile in the image')
ax.set_xlabel('x direction [px]')
ax.set_ylabel('intensity a.u.')
# ax.plot(np.arange(len(dy)), dy, linewidth=2)
# ax.plot(np.arange(len(wires)), wires, linewidth=2)
# ax.scatter(split(gaps)[0], split(gaps)[1], linewidth=2)
'''