gokaygokay commited on
Commit
5d944e0
1 Parent(s): 2b06fda

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -41
app.py CHANGED
@@ -1,5 +1,4 @@
1
  import numpy as np
2
- import scipy as sp
3
  from numba import njit, prange, vectorize
4
  import gradio as gr
5
 
@@ -20,49 +19,21 @@ def neighbours(y, x, max_y, max_x):
20
  n.append((y, x+1))
21
  return n
22
 
23
- @njit(parallel=True)
24
- def poisson_sharpening(img, alpha):
25
- """
26
- Returns a sharpened image with strength of alpha.
27
- :param img: the image
28
- :param alpha: edge threshold and gradient scaler
29
- """
30
  img_h, img_w = img.shape
31
- img_s = img.copy()
32
-
33
- im2var = np.arange(img_h * img_w).reshape(img_h, img_w)
34
 
35
- A_data = np.zeros(img_h * img_w * 4 * 2, dtype=np.float64)
36
- A_row = np.zeros(img_h * img_w * 4 * 2, dtype=np.int32)
37
- A_col = np.zeros(img_h * img_w * 4 * 2, dtype=np.int32)
38
- b = np.zeros(img_h * img_w * 4 * 2, dtype=np.float64)
 
 
 
 
39
 
40
- e = 0
41
- for y in prange(img_h):
42
- for x in prange(img_w):
43
- A_data[e] = 1
44
- A_row[e] = e
45
- A_col[e] = im2var[y, x]
46
- b[e] = img_s[y, x]
47
- e += 1
48
-
49
- for n_y, n_x in neighbours(y, x, img_h-1, img_w-1):
50
- A_data[e] = 1
51
- A_row[e] = e
52
- A_col[e] = im2var[y, x]
53
- e += 1
54
-
55
- A_data[e] = -1
56
- A_row[e] = e - 1
57
- A_col[e] = im2var[n_y, n_x]
58
-
59
- b[e-1] = alpha * (img_s[y, x] - img_s[n_y, n_x])
60
- e += 1
61
-
62
- A = sp.sparse.csr_matrix((A_data[:e], (A_row[:e], A_col[:e])), shape=(e, img_h * img_w))
63
- v = sp.sparse.linalg.lsqr(A, b[:e])[0]
64
-
65
- return clip(v.reshape(img_h, img_w), 1.0)
66
 
67
  @njit(parallel=True)
68
  def sharpen_image(img, alpha):
 
1
  import numpy as np
 
2
  from numba import njit, prange, vectorize
3
  import gradio as gr
4
 
 
19
  n.append((y, x+1))
20
  return n
21
 
22
+ @njit
23
+ def poisson_sharpening(img, alpha, num_iterations=50):
 
 
 
 
 
24
  img_h, img_w = img.shape
25
+ v = img.copy()
 
 
26
 
27
+ for _ in range(num_iterations):
28
+ for y in range(img_h):
29
+ for x in range(img_w):
30
+ neighbors = neighbours(y, x, img_h-1, img_w-1)
31
+ num_neighbors = len(neighbors)
32
+ neighbor_sum = sum(v[ny, nx] for ny, nx in neighbors)
33
+ laplacian = neighbor_sum - num_neighbors * v[y, x]
34
+ v[y, x] += (laplacian + alpha * (img[y, x] - v[y, x])) / (num_neighbors + alpha)
35
 
36
+ return clip(v, 1.0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  @njit(parallel=True)
39
  def sharpen_image(img, alpha):