-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathifm.py
133 lines (98 loc) · 3.84 KB
/
ifm.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import argparse
import numpy as np
import scipy.io
import scipy.sparse as sp
from scipy.sparse.linalg import cg
import skimage.io
import torch as th
def color_mixture_laplacian(N, inInd, neighInd, flows, weights):
""" """
row_idx = np.tile(inInd, (1, flows.shape[1]))
col_idx = neighInd
Wcm = sp.coo_matrix((np.ravel(flows), (np.ravel(row_idx), np.ravel(col_idx))), shape=(N, N))
Wcm = sp.spdiags(np.ravel(weights), 0, N, N).dot(Wcm)
Lcm = sp.spdiags(np.ravel(np.sum(Wcm, axis=1)), 0, N, N) - Wcm
Lcm = (Lcm.T).dot(Lcm)
return Lcm
def matting_laplacian(N, inInd, flowRows, flowCols, flows, weights):
""" """
weights = np.ravel(weights)[inInd]
nweights = weights.size
flow_sz = flows.shape[0]
flows *= np.tile(np.reshape(weights, (1, 1, nweights)), [flow_sz, flow_sz, 1])
Wmat = sp.coo_matrix((np.ravel(flows), (np.ravel(flowRows), np.ravel(flowCols))), shape=(N, N))
Wmat = (Wmat + Wmat.T)*0.5
Lmat = sp.spdiags(np.ravel(np.sum(Wmat, 1)), 0, N, N) - Wmat
return Lmat
def similarity_laplacian(N, inInd, neighInd, flows, weights):
""" """
weights = np.ravel(weights)[inInd]
nweights = weights.size
flow_sz = flows.shape[1]
flows *= np.tile(weights,[1, flow_sz])
inInd = np.tile(inInd, [1, neighInd.shape[1]])
Wcs = sp.coo_matrix((np.ravel(flows), (np.ravel(inInd), np.ravel(neighInd))), shape=(N, N))
Wcs = (Wcs + Wcs.T)*0.5
Lcs = sp.spdiags(np.ravel(np.sum(Wcs, 1)), 0, N, N) - Wcs
return Lcs
def convert_index(old, h, w):
idx = np.unravel_index(old, [w, h])
new = np.ravel_multi_index((idx[1], idx[0]), (h, w))
return new
def main(args):
data = scipy.io.loadmat(args.ifm_data)['IFMdata']
CM_inInd = data['CM_inInd'][0][0]
CM_neighInd = data['CM_neighInd'][0][0]
CM_flows = data['CM_flows'][0][0]
LOC_inInd = data['LOC_inInd'][0][0]
LOC_flowRows = data['LOC_flowRows'][0][0]
LOC_flowCols = data['LOC_flowCols'][0][0]
LOC_flows = data['LOC_flows'][0][0]
IU_inInd = data['IU_inInd'][0][0]
IU_neighInd = data['IU_neighInd'][0][0]
IU_flows = data['IU_flows'][0][0]
kToU = data['kToU'][0][0]
kToUconf = data['kToUconf'][0][0]
known = data['known'][0][0]
h, w = kToU.shape
N = h*w
# Convert indices from matlab to numpy format
CM_inInd = convert_index(CM_inInd, h, w)
CM_neighInd = convert_index(CM_neighInd, h, w)
LOC_inInd = convert_index(LOC_inInd, h, w)
LOC_flowRows = convert_index(LOC_flowRows, h, w)
LOC_flowCols = convert_index(LOC_flowCols, h, w)
IU_inInd = convert_index(IU_inInd, h, w)
IU_neighInd = convert_index(IU_neighInd, h, w)
CM_weights = np.ones((N,))
LOC_weights = np.ones((N,))
IU_weights = np.ones((N,))
KU_weights = np.ones((N,))
cm_mult = 1;
loc_mult = 1;
iu_mult = 0.01;
ku_mult = 0.05;
lmbda = 100;
print("Assembling linear system")
A = cm_mult*color_mixture_laplacian(N, CM_inInd, CM_neighInd, CM_flows, CM_weights) + \
loc_mult*matting_laplacian(N, LOC_inInd, LOC_flowRows, LOC_flowCols, LOC_flows, LOC_weights) + \
iu_mult*similarity_laplacian(N, IU_inInd, IU_neighInd, IU_flows, IU_weights) + \
ku_mult*sp.spdiags(np.ravel(KU_weights), 0, N, N).dot(sp.spdiags(np.ravel(kToUconf), 0, N, N)) + \
lmbda*sp.spdiags(np.ravel(known).astype(np.float64), 0, N, N)
b = (ku_mult*sp.spdiags(np.ravel(KU_weights), 0, N, N).dot(sp.spdiags(np.ravel(kToUconf), 0, N, N)) + \
lmbda*sp.spdiags(np.ravel(known).astype(np.float64), 0, N, N)).dot(np.ravel(kToU))
print("Solving")
alpha, info = cg(A, b, tol=1e-6, maxiter=2000)
print(np.amin(alpha), " ", np.amax(alpha))
alpha = np.clip(alpha, 0, 1)
alpha = np.reshape(alpha, (h, w))
print(info)
skimage.io.imsave("alpha.png", alpha)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("ifm_data")
args = parser.parse_args()
main(args)