-
Notifications
You must be signed in to change notification settings - Fork 186
/
closed_form_matting.py
executable file
·58 lines (50 loc) · 2.37 KB
/
closed_form_matting.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
from __future__ import division
import argparse
import os
import scipy.misc as spm
import scipy.ndimage as spi
import scipy.sparse as sps
import numpy as np
import tensorflow as tf
def getlaplacian1(i_arr, consts, epsilon=1e-5, win_rad=1):
neb_size = (win_rad * 2 + 1) ** 2
h, w, c = i_arr.shape
img_size = w * h
consts = spi.morphology.grey_erosion(consts, footprint=np.ones(shape=(win_rad * 2 + 1, win_rad * 2 + 1)))
indsM = np.reshape(np.array(range(img_size)), newshape=(h, w), order='F')
tlen = int((-consts[win_rad:-win_rad, win_rad:-win_rad] + 1).sum() * (neb_size ** 2))
row_inds = np.zeros(tlen)
col_inds = np.zeros(tlen)
vals = np.zeros(tlen)
l = 0
for j in range(win_rad, w - win_rad):
for i in range(win_rad, h - win_rad):
if consts[i, j]:
continue
win_inds = indsM[i - win_rad:i + win_rad + 1, j - win_rad: j + win_rad + 1]
win_inds = win_inds.ravel(order='F')
win_i = i_arr[i - win_rad:i + win_rad + 1, j - win_rad: j + win_rad + 1, :]
win_i = win_i.reshape((neb_size, c), order='F')
win_mu = np.mean(win_i, axis=0).reshape(c, 1)
win_var = np.linalg.inv(
np.matmul(win_i.T, win_i) / neb_size - np.matmul(win_mu, win_mu.T) + epsilon / neb_size * np.identity(
c))
win_i2 = win_i - np.repeat(win_mu.transpose(), neb_size, 0)
tvals = (1 + np.matmul(np.matmul(win_i2, win_var), win_i2.T)) / neb_size
ind_mat = np.broadcast_to(win_inds, (neb_size, neb_size))
row_inds[l: (neb_size ** 2 + l)] = ind_mat.ravel(order='C')
col_inds[l: neb_size ** 2 + l] = ind_mat.ravel(order='F')
vals[l: neb_size ** 2 + l] = tvals.ravel(order='F')
l += neb_size ** 2
vals = vals.ravel(order='F')[0: l]
row_inds = row_inds.ravel(order='F')[0: l]
col_inds = col_inds.ravel(order='F')[0: l]
a_sparse = sps.csr_matrix((vals, (row_inds, col_inds)), shape=(img_size, img_size))
sum_a = a_sparse.sum(axis=1).T.tolist()[0]
a_sparse = sps.diags([sum_a], [0], shape=(img_size, img_size)) - a_sparse
return a_sparse
def getLaplacian(img):
h, w, _ = img.shape
coo = getlaplacian1(img, np.zeros(shape=(h, w)), 1e-5, 1).tocoo()
indices = np.mat([coo.row, coo.col]).transpose()
return tf.SparseTensor(indices, coo.data, coo.shape)