-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathgetExactColor_backup.py
125 lines (90 loc) · 4.04 KB
/
getExactColor_backup.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
def getColorExact( colorIm, YUV):
# YUV as ntscIm
n = YUV.shape[0]
m = YUV.shape[1]
image_size = n*m
# colorized as nI
colorized = np.zeros(YUV.shape)
colorized[:,:,0] = YUV[:,:,0]
# this Matlab code: z = reshape(x,3,4); should become z = x.reshape(3,4,order='F').copy() in Numpy.
# indices_matrix as indsM
indices_matrix = np.arange(image_size).reshape(n,m,order='F').copy()
# We have to reshape and make a copy of the view of an array
# for the nonzero() work like in MATLAB
color_copy_for_nonzero = colorIm.reshape(image_size).copy()
# label_inds as lblInds
label_inds = np.count_nonzero(color_copy_for_nonzero) # it's cool that nonzero likes boolean values, too
wd = 1
# length as len (for obv reasons)
length = 0
col_inds = np.zeros((image_size*( 2 * wd + 1 )**2,1))
row_inds = np.zeros((image_size*( 2 * wd + 1 )**2,1))
vals = np.zeros((image_size*( 2 * wd + 1 )**2,1))
gvals = np.zeros((2 * wd + 1 )**2)
# PREPS made, lets ITERATE!
count = 0 # for testing
consts_len = 0
for j in range(m):
for i in range(n):
consts_len += 1
if (not colorIm[i,j]):
tlen = 0
#print max( 0, i - wd ), min( i + wd+1, n ),max( 0, j - wd ), min( j + wd, m )+1
for ii in range(max( 0, i-wd ), min( i+wd+1, n )):
for jj in range( max( 0, j - wd ), min( j + wd, m )+1):
count += 1 # for testing
if ( ii != i or jj != j ):
row_inds[length,0] = consts_len
col_inds[length,0] = indices_matrix[ii,jj]
gvals[tlen] = YUV[ii,jj,0]
length += 1
tlen += 1
t_val = YUV[i,j,0].copy()
gvals[tlen] = t_val
c_var = np.mean((gvals[0:tlen+1] - np.mean(gvals[0:tlen+1]))**2)
csig = c_var * 0.6
mgv = min(( gvals[0:tlen+1] - t_val )**2)
if (csig < ( -mgv / np.log(0.01 ))):
csig = -mgv / np.log(0.01)
if (csig <0.000002):
csig = 0.000002
gvals[0:tlen] = np.exp( -(gvals[0:tlen] - t_val)**2 / csig )
gvals[0:tlen] = gvals[0:tlen] / np.sum(gvals[0:tlen])
vals[length-tlen:length,0] = -gvals[0:tlen]
# END IF
length += 1
row_inds[length-1,0] = consts_len
col_inds[length-1,0] = indices_matrix[i,j]
vals[length-1,0] = 1
# END OF FOR i
# END OF FOR j
# A LITTLE BIT MORE AND THEN CAN RETURN ALREADY SOMETHING!
vals = vals[0:length,0]
col_inds = col_inds[0:length,0]
row_inds = row_inds[0:length,0]
# A=sparse(row_inds,col_inds,vals,consts_len,imgSize);
'''THOUGHT FOOD
S = sparse(i,j,s,m,n,nzmax) uses vectors i, j, and s to generate an
m-by-n sparse matrix such that S(i(k),j(k)) = s(k), with space
allocated for nzmax nonzeros. Vectors i, j, and s are all the same
length. Any elements of s that are zero are ignored, along with the
corresponding values of i and j. Any elements of s that have duplicate
values of i and j are added together. The argument s and one of the
arguments i or j may be scalars, in which case the scalars are expanded
so that the first three arguments all have the same length.
>> a = diag(1:4)
a =
1 0 0 0
0 2 0 0
0 0 3 0
0 0 0 4
>> s = sparse(a)
s =
(1,1) 1
(2,2) 2
(3,3) 3
(4,4) 4
'''
#print something
sys.exit('Sparse needs to be implemented!')
return YUV # should be colorized, but mock until we make it