-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSCVUtils.py
196 lines (177 loc) · 6.48 KB
/
SCVUtils.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
"""
Utilities for implementing the Sum of Conditional Variances as
described by Richa et. al.
R. Richa, R. Sznitman, R. Taylor, and G. Hager, "Visual tracking
using the sum of conditional variance," Intelligent Robots and
Systems (IROS), 2011 IEEE/RSJ International Conference on, pp.
2953-2958, 2011.
Author: Travis Dick ([email protected])
"""
import numpy as np
from scipy import weave
from scipy.weave import converters
#from ImageUtils import *
def getSCVIntensityMap(src, dst):
conditional_probability = np.zeros((256,256))
intensity_map = np.arange(256, dtype=np.float64)
n = len(src)
for k in xrange(n):
conditional_probability[src[k], dst[k]]+=1
for i in xrange(256):
normalizer=0
weighted_sum=0
for j in xrange(256):
weighted_sum += j * conditional_probability[i,j]
normalizer += conditional_probability[i,j]
if normalizer>0:
intensity_map[i] = weighted_sum / normalizer
return intensity_map
def scv_intensity_map(src, dst):
#print 'starting scv_intensity_map'
#log_file=open("temp_data.txt","w")
#log_file.write("src:\n")
#log_file.write(src)
#log_file.write("\ndst:\n")
#log_file.write(dst)
#np.savetxt(log_file, src)
#log_file.close()
conditional_probability = np.zeros((256,256))
intensity_map = np.arange(256, dtype=np.float64)
n = len(src)
code = \
"""
for (int k = 0; k < n; k++) {
int i = int(src(k));
int j = int(dst(k));
conditional_probability(i,j) += 1;
}
for (int i = 0; i < 256; i++) {
double normalizer = 0;
double total = 0;
for (int j = 0; j < 256; j++) {
total += j * conditional_probability(i,j);
normalizer += conditional_probability(i,j);
}
if (normalizer > 0) {
intensity_map(i) = total / normalizer;
}
}
"""
#print "executing weave"
weave.inline(code, ['conditional_probability', 'intensity_map', 'n', 'src', 'dst'],
type_converters=converters.blitz,
compiler='gcc')
#print "Done executing weave"
#print 'done scv_intensity_map'
return intensity_map
def scv_intensity_map_vec(src, dst):
# print 'src.shape: ', src.shape
# print 'dst.shape: ', dst.shape
if len(src.shape)!=2 or len(dst.shape)!=2:
print 'src.shape: ', src.shape
print 'dst.shape: ', dst.shape
raise SystemExit('Error in scv_intensity_map_vec:\nSource and/or destination images are not multichannel')
#print 'starting scv_intensity_map'
#log_file=open("temp_data.txt","w")
#log_file.write("src:\n")
#log_file.write(src)
#log_file.write("\ndst:\n")
#log_file.write(dst)
#np.savetxt(log_file, src)
#log_file.close()
nchannels=src.shape[0]
# print 'nchannels=', nchannels
conditional_probability = np.zeros((256,256))
intensity_map=np.empty((nchannels, 256))
for i in xrange(nchannels):
intensity_map[i, :] = np.arange(256, dtype=np.float64)
np.savetxt('intensity_map_scv.txt', intensity_map, fmt='%10.5f')
n = src.shape[1]
code = \
"""
for (int ch = 0; ch < nchannels; ch++) {
for (int k = 0; k < n; k++) {
int i = int(src(ch, k));
int j = int(dst(ch, k));
conditional_probability(i,j) += 1;
}
for (int i = 0; i < 256; i++) {
double normalizer = 0;
double total = 0;
for (int j = 0; j < 256; j++) {
total += j * conditional_probability(i,j);
normalizer += conditional_probability(i,j);
conditional_probability(i,j)=0
}
if (normalizer > 0) {
intensity_map(ch, i) = total / normalizer;
}
}
}
"""
# print "executing weave in scv_intensity_map_vec"
weave.inline(code, ['conditional_probability', 'intensity_map', 'n', 'src', 'dst', 'nchannels'],
type_converters=converters.blitz,
compiler='gcc')
# print "Done executing weave"
#print 'done scv_intensity_map'
return intensity_map
def scv_intensity_map_vec2(src_vec, dst_vec):
#print 'src_vec.shape: ', src_vec.shape
#print 'dst_vec.shape: ', dst_vec.shape
if len(src_vec.shape)!=2 or len(dst_vec.shape)!=2:
print 'src.shape: ', src_vec.shape
print 'dst.shape: ', dst_vec.shape
raise SystemExit('Error in scv_intensity_map_vec:\nSource and/or destination images are not multichannel')
#print 'nchannels=', nchannels
nchannels=src_vec.shape[0]
intensity_map_vec=np.empty((nchannels, 256))
conditional_probability = np.zeros((256,256))
n = src_vec.shape[1]
#print 'n=', n
code = \
"""
for (int k = 0; k < n; k++) {
int i = int(src(k));
int j = int(dst(k));
conditional_probability(i,j) += 1;
}
for (int i = 0; i < 256; i++) {
double normalizer = 0;
double total = 0;
for (int j = 0; j < 256; j++) {
total += j * conditional_probability(i,j);
normalizer += conditional_probability(i,j);
}
if (normalizer > 0) {
intensity_map(i) = total / normalizer;
}
}
"""
# print 'src_vec.shape:', src_vec.shape
# print 'dst_vec.shape:', dst_vec.shape
# print 'executing weave in scv_intensity_map_vec2'
for i in xrange(nchannels):
src=src_vec[i,:]
dst=dst_vec[i,:]
# print 'src.shape:', src.shape
# print 'dst.shape:', dst.shape
intensity_map = np.arange(256, dtype=np.float64)
weave.inline(code, ['conditional_probability', 'intensity_map', 'n', 'src', 'dst'],
type_converters=converters.blitz,
compiler='gcc')
intensity_map_vec[i,:]=intensity_map
# print 'Done'
#np.savetxt('intensity_map_scv.txt', intensity_map_vec, fmt='%10.5f')
#print "Done executing weave"
#print 'done scv_intensity_map'
return intensity_map_vec
def scv_expectation(original, intensity_map):
return intensity_map[np.floor(original).astype(np.int)]
def scv_expectation_vec(original, intensity_map):
#print 'original.shape:', original.shape
expectation=np.empty(original.shape)
for i in xrange(original.shape[0]):
ch_map=intensity_map[i,:]
expectation[i, :]=ch_map[np.floor(original[i, :]).astype(np.int)]
return expectation