Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,37 +1,93 @@
|
|
1 |
import cv2
|
2 |
import numpy as np
|
|
|
|
|
3 |
from numba import jit, prange
|
4 |
import gradio as gr
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
@jit(nopython=True, parallel=True)
|
7 |
-
def
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
laplacian = (
|
14 |
-
img[y-1, x] + img[y+1, x] + img[y, x-1] + img[y, x+1] - 4 * img[y, x]
|
15 |
-
)
|
16 |
-
output[y, x] = img[y, x] + alpha * laplacian
|
17 |
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
def sharpen_image(input_img, alpha):
|
21 |
-
|
22 |
-
img = cv2.cvtColor(input_img, cv2.COLOR_BGR2RGB).astype('float32') / 255.0
|
23 |
|
24 |
-
sharpen_img = np.
|
25 |
for b in range(3):
|
26 |
-
sharpen_img[:,:,b] =
|
27 |
|
28 |
-
|
29 |
-
return cv2.cvtColor((sharpen_img * 255).astype(np.uint8), cv2.COLOR_RGB2BGR)
|
30 |
|
31 |
# Create examples list
|
32 |
examples = [
|
33 |
-
["img1.jpg", 0
|
34 |
-
["img2.PNG", 0
|
35 |
]
|
36 |
|
37 |
# Create the Gradio interface
|
@@ -39,10 +95,10 @@ iface = gr.Interface(
|
|
39 |
fn=sharpen_image,
|
40 |
inputs=[
|
41 |
gr.Image(label="Input Image", type="numpy"),
|
42 |
-
gr.Slider(minimum=0
|
43 |
],
|
44 |
outputs=gr.Image(label="Sharpened Image"),
|
45 |
-
title="
|
46 |
description="Upload an image or choose from the examples, then adjust the sharpening strength to enhance edges and details.",
|
47 |
theme='bethecloud/storj_theme',
|
48 |
examples=examples,
|
|
|
1 |
import cv2
|
2 |
import numpy as np
|
3 |
+
import scipy as sp
|
4 |
+
import scipy.sparse.linalg
|
5 |
from numba import jit, prange
|
6 |
import gradio as gr
|
7 |
|
8 |
+
@jit(nopython=True)
|
9 |
+
def neighbours(i, j, max_i, max_j):
|
10 |
+
pairs = []
|
11 |
+
for n in [-1, 1]:
|
12 |
+
if 0 <= i+n <= max_i:
|
13 |
+
pairs.append((i+n, j))
|
14 |
+
if 0 <= j+n <= max_j:
|
15 |
+
pairs.append((i, j+n))
|
16 |
+
return pairs
|
17 |
+
|
18 |
+
@jit(nopython=True)
|
19 |
+
def build_poisson_matrix(img_h, img_w, alpha):
|
20 |
+
im2var = np.arange(img_h * img_w).reshape(img_h, img_w)
|
21 |
+
A_data = []
|
22 |
+
A_row = []
|
23 |
+
A_col = []
|
24 |
+
b = np.zeros(img_h*img_w*5)
|
25 |
+
|
26 |
+
e = 0
|
27 |
+
for y in range(img_h):
|
28 |
+
for x in range(img_w):
|
29 |
+
A_data.append(1)
|
30 |
+
A_row.append(e)
|
31 |
+
A_col.append(im2var[y, x])
|
32 |
+
e += 1
|
33 |
+
|
34 |
+
for n_y, n_x in neighbours(y, x, img_h-1, img_w-1):
|
35 |
+
A_data.append(1)
|
36 |
+
A_row.append(e)
|
37 |
+
A_col.append(im2var[y, x])
|
38 |
+
|
39 |
+
A_data.append(-1)
|
40 |
+
A_row.append(e)
|
41 |
+
A_col.append(im2var[n_y, n_x])
|
42 |
+
|
43 |
+
e += 1
|
44 |
+
|
45 |
+
return A_data, A_row, A_col, b, e
|
46 |
+
|
47 |
@jit(nopython=True, parallel=True)
|
48 |
+
def fill_b_vector(b, img, alpha, img_h, img_w):
|
49 |
+
e = 0
|
50 |
+
for y in prange(img_h):
|
51 |
+
for x in range(img_w):
|
52 |
+
b[e] = img[y, x]
|
53 |
+
e += 1
|
54 |
+
|
55 |
+
for n_y, n_x in neighbours(y, x, img_h-1, img_w-1):
|
56 |
+
b[e] = alpha * (img[y, x] - img[n_y, n_x])
|
57 |
+
e += 1
|
58 |
+
|
59 |
+
def poisson_sharpening(img: np.ndarray, alpha: float) -> np.ndarray:
|
60 |
+
"""
|
61 |
+
Returns a sharpened image with strength of alpha.
|
62 |
+
:param img: the image
|
63 |
+
:param alpha: edge threshold and gradient scaler
|
64 |
+
"""
|
65 |
+
img_h, img_w = img.shape[:2]
|
66 |
|
67 |
+
A_data, A_row, A_col, b, e = build_poisson_matrix(img_h, img_w, alpha)
|
68 |
+
fill_b_vector(b, img, alpha, img_h, img_w)
|
|
|
|
|
|
|
|
|
69 |
|
70 |
+
A = sp.sparse.csr_matrix((A_data, (A_row, A_col)), shape=(e, img_h*img_w))
|
71 |
+
v = sp.sparse.linalg.lsqr(A, b[:e])[0]
|
72 |
+
|
73 |
+
return np.clip(v.reshape(img_h, img_w), 0, 1)
|
74 |
+
|
75 |
+
def get_image(img):
|
76 |
+
return cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype('float32') / 255.0
|
77 |
|
78 |
def sharpen_image(input_img, alpha):
|
79 |
+
img = get_image(input_img)
|
|
|
80 |
|
81 |
+
sharpen_img = np.zeros(img.shape)
|
82 |
for b in range(3):
|
83 |
+
sharpen_img[:,:,b] = poisson_sharpening(img[:,:,b], alpha)
|
84 |
|
85 |
+
return (sharpen_img * 255).astype(np.uint8)
|
|
|
86 |
|
87 |
# Create examples list
|
88 |
examples = [
|
89 |
+
["img1.jpg", 9.0],
|
90 |
+
["img2.PNG", 7.0],
|
91 |
]
|
92 |
|
93 |
# Create the Gradio interface
|
|
|
95 |
fn=sharpen_image,
|
96 |
inputs=[
|
97 |
gr.Image(label="Input Image", type="numpy"),
|
98 |
+
gr.Slider(minimum=1.0, maximum=15.0, step=0.01, value=9.0, label="Sharpening Strength (alpha)")
|
99 |
],
|
100 |
outputs=gr.Image(label="Sharpened Image"),
|
101 |
+
title="Poisson Image Sharpening",
|
102 |
description="Upload an image or choose from the examples, then adjust the sharpening strength to enhance edges and details.",
|
103 |
theme='bethecloud/storj_theme',
|
104 |
examples=examples,
|