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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -38
app.py CHANGED
@@ -1,38 +1,49 @@
1
- import cv2
2
  import numpy as np
3
  import scipy as sp
4
- import scipy.sparse.linalg
5
- from numba import njit, prange
6
  import gradio as gr
7
 
 
 
 
 
8
  @njit
9
  def neighbours(y, x, max_y, max_x):
10
- neighbors = []
11
  if y > 0:
12
- neighbors.append((y-1, x))
13
  if y < max_y:
14
- neighbors.append((y+1, x))
15
  if x > 0:
16
- neighbors.append((y, x-1))
17
  if x < max_x:
18
- neighbors.append((y, x+1))
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,32 +56,27 @@ def build_poisson_matrix(img, alpha, img_h, img_w):
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
-
62
- def sharpen_image_channels(img, alpha):
 
 
 
 
 
63
  sharpen_img = np.zeros_like(img)
64
- for b in range(3):
65
  sharpen_img[:,:,b] = poisson_sharpening(img[:,:,b], alpha)
66
- return sharpen_img
67
-
68
- def get_image(img):
69
- return cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype('float32') / 255.0
70
-
71
- def sharpen_image(input_img, alpha):
72
- img = get_image(input_img)
73
- sharpen_img = sharpen_image_channels(img, alpha)
74
  return (sharpen_img * 255).astype(np.uint8)
75
 
76
  # Create examples list
 
 
1
  import numpy as np
2
  import scipy as sp
3
+ from numba import njit, prange, vectorize
 
4
  import gradio as gr
5
 
6
+ @vectorize(['float64(float64, float64)'], nopython=True)
7
+ def clip(a, max_value):
8
+ return min(max(a, 0), max_value)
9
+
10
  @njit
11
  def neighbours(y, x, max_y, max_x):
12
+ n = []
13
  if y > 0:
14
+ n.append((y-1, x))
15
  if y < max_y:
16
+ n.append((y+1, x))
17
  if x > 0:
18
+ n.append((y, x-1))
19
  if x < 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):
 
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):
69
+ # Convert alpha to float
70
+ alpha = float(alpha)
71
+
72
+ # Ensure the image is in the correct format
73
+ img = img.astype(np.float64) / 255.0
74
+
75
  sharpen_img = np.zeros_like(img)
76
+ for b in prange(3):
77
  sharpen_img[:,:,b] = poisson_sharpening(img[:,:,b], alpha)
78
+
79
+ # Convert back to uint8 for Gradio output
 
 
 
 
 
 
80
  return (sharpen_img * 255).astype(np.uint8)
81
 
82
  # Create examples list