File size: 2,310 Bytes
38d65f9
ab0cb11
 
 
38d65f9
 
ab0cb11
 
 
 
2b06fda
ab0cb11
 
 
 
 
 
 
 
0f5237c
ab0cb11
 
 
 
 
0f5237c
ab0cb11
 
 
 
 
 
 
 
 
 
 
 
 
2b06fda
ab0cb11
 
2b06fda
ab0cb11
38d65f9
04fbf12
38d65f9
ab0cb11
 
38d65f9
 
 
 
 
 
 
ab0cb11
38d65f9
 
0f5237c
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import numpy as np
import cv2
import matplotlib.pyplot as plt
from numba import jit
import gradio as gr

@jit(nopython=True, parallel=True)
def poisson_sharpening_rgb(image, alpha):
    height, width, channels = image.shape
    sharpened = np.zeros_like(image, dtype=np.float32)

    for c in range(channels):
        for i in range(height):
            for j in range(width):
                # Compute indices for neighboring pixels
                left = max(0, j - 1)
                right = min(width - 1, j + 1)
                top = max(0, i - 1)
                bottom = min(height - 1, i + 1)

                # Compute differences with neighboring pixels
                diff_left = float(image[i, j, c]) - float(image[i, left, c])
                diff_right = float(image[i, j, c]) - float(image[i, right, c])
                diff_top = float(image[i, j, c]) - float(image[top, j, c])
                diff_bottom = float(image[i, j, c]) - float(image[bottom, j, c])

                # Compute sharpened pixel value
                sharpened[i, j, c] = min(max(
                    float(image[i, j, c]) + alpha * (diff_left + diff_right + diff_top + diff_bottom),
                    0.0), 255.0)

    return sharpened.astype(np.uint8)

def sharpen_image(image, alpha):
    # Ensure the image is in RGB format
    if image.shape[2] == 4:  # If RGBA, convert to RGB
        image = cv2.cvtColor(image, cv2.COLOR_RGBA2RGB)
    elif len(image.shape) == 2:  # If grayscale, convert to RGB
        image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
    
    # Apply sharpening
    sharpened = poisson_sharpening_rgb(image, alpha)
    
    return sharpened

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

# Create the Gradio interface
iface = gr.Interface(
    fn=sharpen_image,
    inputs=[
        gr.Image(label="Input Image", type="numpy"),
        gr.Slider(minimum=1.0, maximum=15.0, step=0.01, value=2.0, label="Sharpening Strength (alpha)")
    ],
    outputs=gr.Image(label="Sharpened Image"),
    title="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()