Spaces:
Running
Running
gokaygokay
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,59 +1,52 @@
|
|
1 |
import numpy as np
|
2 |
-
|
|
|
|
|
3 |
import gradio as gr
|
4 |
|
5 |
-
@
|
6 |
-
def
|
7 |
-
|
|
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
n.append((y, x-1))
|
18 |
-
if x < max_x:
|
19 |
-
n.append((y, x+1))
|
20 |
-
return n
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
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 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
sharpen_img[:,:,b] = poisson_sharpening(img[:,:,b], alpha)
|
49 |
|
50 |
-
|
51 |
-
return (sharpen_img * 255).astype(np.uint8)
|
52 |
|
53 |
# Create examples list
|
54 |
examples = [
|
55 |
-
["img1.jpg",
|
56 |
-
["img2.PNG",
|
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=
|
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",
|