gokaygokay commited on
Commit
17dd38b
1 Parent(s): 21085cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -19
app.py CHANGED
@@ -38,28 +38,29 @@ def poisson_blend(img_s, mask, img_t):
38
  ys, xs = np.where(mask == 1)
39
 
40
  # Precompute neighbor indices
41
- y_n = np.clip(np.stack([ys-1, ys+1, ys, ys]), 0, img_s_h-1)
42
- x_n = np.clip(np.stack([xs, xs, xs-1, xs+1]), 0, img_s_w-1)
 
 
 
 
43
 
44
  # Compute differences
45
- d = img_s[ys, xs][:, np.newaxis] - img_s[y_n.T, x_n.T].T
46
 
47
  # Construct sparse matrix A and vector b
48
- rows = np.arange(4*nnz)
49
- cols = np.repeat(im2var[ys, xs], 4)
50
- data = np.ones(4*nnz)
51
-
52
- A = sp.sparse.csr_matrix((data, (rows, cols)), shape=(4*nnz, nnz))
53
-
54
- mask_n = (im2var[y_n, x_n] != -1)
55
- cols_n = im2var[y_n, x_n][mask_n]
56
- rows_n = np.arange(4*nnz)[mask_n.ravel()]
57
- data_n = -np.ones(cols_n.size)
58
 
59
- A += sp.sparse.csr_matrix((data_n, (rows_n, cols_n)), shape=(4*nnz, nnz))
 
 
 
60
 
 
61
  b = d.ravel()
62
- b[~mask_n.ravel()] += img_t[y_n, x_n][~mask_n]
63
 
64
  # Solve the system
65
  v = sp.sparse.linalg.lsqr(A, b)[0]
@@ -79,8 +80,12 @@ def mixed_blend(img_s, mask, img_t):
79
  ys, xs = np.where(mask == 1)
80
 
81
  # Precompute neighbor indices
82
- y_n = np.clip(np.stack([ys-1, ys+1, ys, ys]), 0, img_s_h-1)
83
- x_n = np.clip(np.stack([xs, xs, xs-1, xs+1]), 0, img_s_w-1)
 
 
 
 
84
 
85
  # Compute differences
86
  ds = img_s[ys, xs][:, np.newaxis] - img_s[y_n, x_n]
@@ -92,7 +97,7 @@ def mixed_blend(img_s, mask, img_t):
92
  # Construct sparse matrix A and vector b
93
  rows = np.repeat(np.arange(4*nnz), 2)
94
  cols = np.column_stack([np.repeat(im2var[ys, xs], 4), im2var[y_n, x_n].ravel()])
95
- data = np.column_stack([np.ones(4*nnz), -np.ones(4*nnz)]).ravel()
96
 
97
  mask_n = (im2var[y_n, x_n] != -1).ravel()
98
  rows = rows[mask_n]
@@ -101,7 +106,7 @@ def mixed_blend(img_s, mask, img_t):
101
 
102
  A = sp.sparse.csr_matrix((data, (rows, cols)), shape=(4*nnz, nnz))
103
  b = d.ravel()
104
- b[~mask_n] += img_t[y_n, x_n].ravel()[~mask_n]
105
 
106
  # Solve the system
107
  v = sp.sparse.linalg.lsqr(A, b)[0]
 
38
  ys, xs = np.where(mask == 1)
39
 
40
  # Precompute neighbor indices
41
+ y_n = np.array([ys-1, ys+1, ys, ys])
42
+ x_n = np.array([xs, xs, xs-1, xs+1])
43
+
44
+ # Clip indices to image boundaries
45
+ y_n = np.clip(y_n, 0, img_s_h-1)
46
+ x_n = np.clip(x_n, 0, img_s_w-1)
47
 
48
  # Compute differences
49
+ d = img_s[ys, xs][:, np.newaxis] - img_s[y_n, x_n]
50
 
51
  # Construct sparse matrix A and vector b
52
+ rows = np.repeat(np.arange(4*nnz), 2)
53
+ cols = np.column_stack([np.repeat(im2var[ys, xs], 4), im2var[y_n, x_n].ravel()])
54
+ data = np.tile([1, -1], 4*nnz)
 
 
 
 
 
 
 
55
 
56
+ mask_n = (im2var[y_n, x_n] != -1).ravel()
57
+ rows = rows[mask_n]
58
+ cols = cols[mask_n]
59
+ data = data[mask_n]
60
 
61
+ A = sp.sparse.csr_matrix((data, (rows, cols)), shape=(4*nnz, nnz))
62
  b = d.ravel()
63
+ b[~mask_n] += img_t[y_n.ravel()[~mask_n], x_n.ravel()[~mask_n]]
64
 
65
  # Solve the system
66
  v = sp.sparse.linalg.lsqr(A, b)[0]
 
80
  ys, xs = np.where(mask == 1)
81
 
82
  # Precompute neighbor indices
83
+ y_n = np.array([ys-1, ys+1, ys, ys])
84
+ x_n = np.array([xs, xs, xs-1, xs+1])
85
+
86
+ # Clip indices to image boundaries
87
+ y_n = np.clip(y_n, 0, img_s_h-1)
88
+ x_n = np.clip(x_n, 0, img_s_w-1)
89
 
90
  # Compute differences
91
  ds = img_s[ys, xs][:, np.newaxis] - img_s[y_n, x_n]
 
97
  # Construct sparse matrix A and vector b
98
  rows = np.repeat(np.arange(4*nnz), 2)
99
  cols = np.column_stack([np.repeat(im2var[ys, xs], 4), im2var[y_n, x_n].ravel()])
100
+ data = np.tile([1, -1], 4*nnz)
101
 
102
  mask_n = (im2var[y_n, x_n] != -1).ravel()
103
  rows = rows[mask_n]
 
106
 
107
  A = sp.sparse.csr_matrix((data, (rows, cols)), shape=(4*nnz, nnz))
108
  b = d.ravel()
109
+ b[~mask_n] += img_t[y_n.ravel()[~mask_n], x_n.ravel()[~mask_n]]
110
 
111
  # Solve the system
112
  v = sp.sparse.linalg.lsqr(A, b)[0]