-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctionSet.py
More file actions
165 lines (138 loc) · 4.16 KB
/
Copy pathfunctionSet.py
File metadata and controls
165 lines (138 loc) · 4.16 KB
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
import numpy
from PIL import Image
from pylab import *
import time
from scipy import ndimage
import skimage
#
def protectedDiv1d(left, right):
if right==0:
return 0
else: return left / right
def protectedDiv(left, right):
with numpy.errstate(divide='ignore',invalid='ignore'):
x = numpy.divide(left, right)
if isinstance(x, numpy.ndarray):
x[numpy.isinf(x)] = 1
x[numpy.isnan(x)] = 1
elif numpy.isinf(x) or numpy.isnan(x):
x = 1
return x
def sqrt(left):
with numpy.errstate(divide='ignore',invalid='ignore'):
x = numpy.sqrt(left,)
if isinstance(x, numpy.ndarray):
x[numpy.isinf(x)] = 1
x[numpy.isnan(x)] = 1
elif numpy.isinf(x) or numpy.isnan(x):
x = 1
return x
def relu(left):
return (abs(left)+left)/2
def addZerosPad(final,current):
M,N=final.shape
m1,n1=current.shape
pUpperSize=int((M-m1)/2)
pDownSize=int(M-pUpperSize-m1)
pLeftSize=int((N-n1)/2)
pRightSize=int(N-pLeftSize-n1)
PUpper=numpy.zeros((pUpperSize,n1))
PDown=numpy.zeros((pDownSize,n1))
current=numpy.concatenate((PUpper,current,PDown),axis=0)
m2,n2=current.shape
PLeft=numpy.zeros((m2,pLeftSize))
PRight=numpy.zeros((m2,pRightSize))
current=numpy.concatenate((PLeft,current,PRight),axis=1)
return current
def conta(*args):
return numpy.asarray(args)
def conta_boolean(*args):
tf_vector = numpy.zeros((len(args),))
for i in range(0,len(args)):
if args[i] < 0:
tf_vector[i] = 0
else: tf_vector[i] = 1
## print(tf_vector)
return tf_vector
def mis_match(img1,img2):
w1,h1=img1.shape
w2,h2=img2.shape
w=min(w1,w2)
h=min(h1,h2)
return img1[0:w,0:h],img2[0:w,0:h]
def mixconadd(img1, w1,img2, w2):
img11,img22=mis_match(img1,img2)
## print(w1, w2)
return numpy.add(img11*w1,img22*w2)
def mixconsub(img1, w1,img2, w2):
img11,img22=mis_match(img1,img2)
return numpy.subtract(img11*w1,img22*w2)
def conVector(img):
#### print(img.shape)
try:
img_vector=numpy.concatenate((img))
except ValueError:
img_vector=img
## print(img_vector)
return img_vector
def maxP(left, kel1, kel2):
#print(left.shape)
try:
current = skimage.measure.block_reduce(left,(kel1,kel2),numpy.max)
except ValueError:
current=left
#print(current.shape)
#left=addZerosPad(left,current)
return current
def ZeromaxP(left, kel1, kel2):
try:
current = skimage.measure.block_reduce(left,(kel1,kel2),numpy.max)
except ValueError:
current=left
#print(current.shape)
zero_p=addZerosPad(left,current)
return zero_p
def random_filters(filter_size):
filters = []
for i in range(filter_size*filter_size):
filters.append(numpy.random.randint(-5, 5))
#print(filters)
return filters
def root_conVector2(img1, img2):
image1=conVector(img1)
image2=conVector(img2)
feature_vector=numpy.concatenate((image1, image2),axis=0)
## print(feature_vector.shape)
return feature_vector
def root_conVector3(img1, img2, img3):
image1=conVector(img1)
image2=conVector(img2)
image3=conVector(img3)
feature_vector=numpy.concatenate((image1, image2, image3),axis=0)
## print(feature_vector.shape)
return feature_vector
def root_conVector4(img1, img2, img3, img4):
image1=conVector(img1)
image2=conVector(img2)
image3=conVector(img3)
image4=conVector(img4)
feature_vector=numpy.concatenate((image1, image2, image3, image4),axis=0)
## print(feature_vector.shape)
return feature_vector
def resize(img, w1):
## print(img.shape, w1)
img = Image.fromarray(img, 'L')
img=img.resize((w1,w1),Image.LANCZOS)
img=numpy.asarray(img)
## print(img.shape, w1)
return img
def conv_filters(image, filters):
length = len(filters)
size = int(sqrt(length))
filters_resize = numpy.asarray(filters).reshape(size, size)
img = ndimage.convolve(image, filters_resize)
return img
def root_conVector1(img1):
image1=conVector(img1)
feature_vector=numpy.concatenate((image1),axis=0)
return feature_vector