Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -19,28 +19,20 @@ def neighbours(y, x, max_y, max_x):
|
|
19 |
return neighbors
|
20 |
|
21 |
@njit
|
22 |
-
def
|
23 |
-
img_h, img_w = img.shape
|
24 |
-
img_s = img.copy()
|
25 |
-
|
26 |
im2var = np.arange(img_h * img_w).reshape(img_h, img_w)
|
27 |
-
|
28 |
A_data = np.zeros(img_h * img_w * 5, dtype=np.float64)
|
29 |
A_row = np.zeros(img_h * img_w * 5, dtype=np.int32)
|
30 |
A_col = np.zeros(img_h * img_w * 5, dtype=np.int32)
|
31 |
b = np.zeros(img_h * img_w * 5, dtype=np.float64)
|
32 |
|
33 |
-
return _poisson_sharpening_inner(img_s, alpha, im2var, A_data, A_row, A_col, b, img_h, img_w)
|
34 |
-
|
35 |
-
@njit(parallel=True)
|
36 |
-
def _poisson_sharpening_inner(img_s, alpha, im2var, A_data, A_row, A_col, b, img_h, img_w):
|
37 |
e = 0
|
38 |
-
for y in
|
39 |
for x in range(img_w):
|
40 |
A_data[e] = 1
|
41 |
A_row[e] = e
|
42 |
A_col[e] = im2var[y, x]
|
43 |
-
b[e] =
|
44 |
e += 1
|
45 |
|
46 |
for n_y, n_x in neighbours(y, x, img_h-1, img_w-1):
|
@@ -53,11 +45,17 @@ def _poisson_sharpening_inner(img_s, alpha, im2var, A_data, A_row, A_col, b, img
|
|
53 |
A_row[e] = e - 1
|
54 |
A_col[e] = im2var[n_y, n_x]
|
55 |
|
56 |
-
b[e-1] = alpha * (
|
57 |
e += 1
|
58 |
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
return np.clip(v.reshape(img_h, img_w), 0, 1)
|
63 |
|
|
|
19 |
return neighbors
|
20 |
|
21 |
@njit
|
22 |
+
def build_poisson_matrix(img, alpha, img_h, img_w):
|
|
|
|
|
|
|
23 |
im2var = np.arange(img_h * img_w).reshape(img_h, img_w)
|
|
|
24 |
A_data = np.zeros(img_h * img_w * 5, dtype=np.float64)
|
25 |
A_row = np.zeros(img_h * img_w * 5, dtype=np.int32)
|
26 |
A_col = np.zeros(img_h * img_w * 5, dtype=np.int32)
|
27 |
b = np.zeros(img_h * img_w * 5, dtype=np.float64)
|
28 |
|
|
|
|
|
|
|
|
|
29 |
e = 0
|
30 |
+
for y in range(img_h):
|
31 |
for x in range(img_w):
|
32 |
A_data[e] = 1
|
33 |
A_row[e] = e
|
34 |
A_col[e] = im2var[y, x]
|
35 |
+
b[e] = img[y, x]
|
36 |
e += 1
|
37 |
|
38 |
for n_y, n_x in neighbours(y, x, img_h-1, img_w-1):
|
|
|
45 |
A_row[e] = e - 1
|
46 |
A_col[e] = im2var[n_y, n_x]
|
47 |
|
48 |
+
b[e-1] = alpha * (img[y, x] - img[n_y, n_x])
|
49 |
e += 1
|
50 |
|
51 |
+
return A_data[:e], A_row[:e], A_col[:e], b[:e], e
|
52 |
+
|
53 |
+
def poisson_sharpening(img: np.ndarray, alpha: float) -> np.ndarray:
|
54 |
+
img_h, img_w = img.shape
|
55 |
+
A_data, A_row, A_col, b, e = build_poisson_matrix(img, alpha, img_h, img_w)
|
56 |
+
|
57 |
+
A = sp.sparse.csr_matrix((A_data, (A_row, A_col)), shape=(e, img_h * img_w))
|
58 |
+
v = sp.sparse.linalg.lsqr(A, b)[0]
|
59 |
|
60 |
return np.clip(v.reshape(img_h, img_w), 0, 1)
|
61 |
|