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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -46
app.py CHANGED
@@ -1,59 +1,52 @@
1
  import numpy as np
2
- from numba import njit, prange, vectorize
 
 
3
  import gradio as gr
4
 
5
- @vectorize(['float64(float64, float64)'], nopython=True)
6
- def clip(a, max_value):
7
- return min(max(a, 0), max_value)
 
8
 
9
- @njit
10
- def neighbours(y, x, max_y, max_x):
11
- n = []
12
- if y > 0:
13
- n.append((y-1, x))
14
- if y < max_y:
15
- n.append((y+1, x))
16
- if x > 0:
17
- n.append((y, x-1))
18
- if x < max_x:
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):
40
- # Convert alpha to float
41
- alpha = float(alpha)
42
-
43
- # Ensure the image is in the correct format
44
- img = img.astype(np.float64) / 255.0
 
 
 
 
 
 
45
 
46
- sharpen_img = np.zeros_like(img)
47
- for b in prange(3):
48
- sharpen_img[:,:,b] = poisson_sharpening(img[:,:,b], alpha)
49
 
50
- # Convert back to uint8 for Gradio output
51
- return (sharpen_img * 255).astype(np.uint8)
52
 
53
  # Create examples list
54
  examples = [
55
- ["img1.jpg", 9.0],
56
- ["img2.PNG", 7.0],
57
  ]
58
 
59
  # Create the Gradio interface
@@ -61,7 +54,7 @@ iface = gr.Interface(
61
  fn=sharpen_image,
62
  inputs=[
63
  gr.Image(label="Input Image", type="numpy"),
64
- gr.Slider(minimum=1.0, maximum=15.0, step=0.01, value=9.0, label="Sharpening Strength (alpha)")
65
  ],
66
  outputs=gr.Image(label="Sharpened Image"),
67
  title="Poisson Image Sharpening",
 
1
  import numpy as np
2
+ import cv2
3
+ import matplotlib.pyplot as plt
4
+ from numba import jit
5
  import gradio as gr
6
 
7
+ @jit(nopython=True, parallel=True)
8
+ def poisson_sharpening_rgb(image, alpha):
9
+ height, width, channels = image.shape
10
+ sharpened = np.zeros_like(image, dtype=np.float32)
11
 
12
+ for c in range(channels):
13
+ for i in range(height):
14
+ for j in range(width):
15
+ # Compute indices for neighboring pixels
16
+ left = max(0, j - 1)
17
+ right = min(width - 1, j + 1)
18
+ top = max(0, i - 1)
19
+ bottom = min(height - 1, i + 1)
 
 
 
 
20
 
21
+ # Compute differences with neighboring pixels
22
+ diff_left = float(image[i, j, c]) - float(image[i, left, c])
23
+ diff_right = float(image[i, j, c]) - float(image[i, right, c])
24
+ diff_top = float(image[i, j, c]) - float(image[top, j, c])
25
+ diff_bottom = float(image[i, j, c]) - float(image[bottom, j, c])
 
 
 
 
 
 
 
 
 
 
26
 
27
+ # Compute sharpened pixel value
28
+ sharpened[i, j, c] = min(max(
29
+ float(image[i, j, c]) + alpha * (diff_left + diff_right + diff_top + diff_bottom),
30
+ 0.0), 255.0)
31
+
32
+ return sharpened.astype(np.uint8)
33
+
34
+ def sharpen_image(image, alpha):
35
+ # Ensure the image is in RGB format
36
+ if image.shape[2] == 4: # If RGBA, convert to RGB
37
+ image = cv2.cvtColor(image, cv2.COLOR_RGBA2RGB)
38
+ elif len(image.shape) == 2: # If grayscale, convert to RGB
39
+ image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
40
 
41
+ # Apply sharpening
42
+ sharpened = poisson_sharpening_rgb(image, alpha)
 
43
 
44
+ return sharpened
 
45
 
46
  # Create examples list
47
  examples = [
48
+ ["img1.jpg", 2.0],
49
+ ["img2.PNG", 2.0],
50
  ]
51
 
52
  # Create the Gradio interface
 
54
  fn=sharpen_image,
55
  inputs=[
56
  gr.Image(label="Input Image", type="numpy"),
57
+ gr.Slider(minimum=1.0, maximum=15.0, step=0.01, value=2.0, label="Sharpening Strength (alpha)")
58
  ],
59
  outputs=gr.Image(label="Sharpened Image"),
60
  title="Poisson Image Sharpening",