File size: 1,575 Bytes
38d65f9
 
04fbf12
38d65f9
 
04fbf12
 
 
 
38d65f9
04fbf12
 
 
 
 
 
38d65f9
04fbf12
38d65f9
 
7a48a84
04fbf12
38d65f9
04fbf12
38d65f9
04fbf12
38d65f9
7a48a84
 
38d65f9
04fbf12
38d65f9
04fbf12
 
38d65f9
 
 
 
 
 
 
04fbf12
38d65f9
 
04fbf12
38d65f9
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import cv2
import numpy as np
from numba import jit, prange
import gradio as gr

@jit(nopython=True, parallel=True)
def fast_poisson_sharpening(img, alpha):
    img_h, img_w = img.shape
    output = np.zeros_like(img)
    
    for y in prange(1, img_h - 1):
        for x in range(1, img_w - 1):
            laplacian = (
                img[y-1, x] + img[y+1, x] + img[y, x-1] + img[y, x+1] - 4 * img[y, x]
            )
            output[y, x] = img[y, x] + alpha * laplacian
    
    return np.clip(output, 0, 1)

def sharpen_image(input_img, alpha):
    # Convert BGR to RGB
    img = cv2.cvtColor(input_img, cv2.COLOR_BGR2RGB).astype('float32') / 255.0
    
    sharpen_img = np.zeros_like(img)
    for b in range(3):
        sharpen_img[:,:,b] = fast_poisson_sharpening(img[:,:,b], alpha)
    
    # Convert back to BGR for output
    return cv2.cvtColor((sharpen_img * 255).astype(np.uint8), cv2.COLOR_RGB2BGR)

# Create examples list
examples = [
    ["img1.jpg", 0.5],
    ["img2.PNG", 0.3],
]

# Create the Gradio interface
iface = gr.Interface(
    fn=sharpen_image,
    inputs=[
        gr.Image(label="Input Image", type="numpy"),
        gr.Slider(minimum=0.1, maximum=1.0, step=0.01, value=0.5, label="Sharpening Strength (alpha)")
    ],
    outputs=gr.Image(label="Sharpened Image"),
    title="Fast Poisson Image Sharpening",
    description="Upload an image or choose from the examples, then adjust the sharpening strength to enhance edges and details.",
    theme='bethecloud/storj_theme',
    examples=examples,
    cache_examples=True
)

iface.launch()