File size: 1,433 Bytes
4dac4f6
 
 
 
074fc66
4dac4f6
 
 
074fc66
 
4dac4f6
 
074fc66
 
 
 
4dac4f6
074fc66
4dac4f6
074fc66
4dac4f6
 
074fc66
4dac4f6
074fc66
4dac4f6
 
 
 
 
 
074fc66
 
4dac4f6
 
 
074fc66
 
4dac4f6
074fc66
4dac4f6
 
 
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
import cv2
import numpy as np
import gradio as gr

def extract_outline(image):
    # Convert to grayscale
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # Set default Gaussian blur kernel size
    blur_kernel_size = (5, 5)
    blurred = cv2.GaussianBlur(gray, blur_kernel_size, 0)

    # Set default Canny edge detection thresholds
    lower_threshold = 50
    upper_threshold = 150
    edges = cv2.Canny(blurred, lower_threshold, upper_threshold)

    # Morphological operations to close gaps
    kernel = np.ones((3, 3), np.uint8)
    closed_edges = cv2.morphologyEx(edges, cv2.MORPH_CLOSE, kernel)

    # Apply morphological thinning to get single-pixel-wide lines
    thinned = cv2.ximgproc.thinning(closed_edges)

    # Invert colors for white background and black outline
    skeleton_on_white = cv2.bitwise_not(thinned)

    return skeleton_on_white

# Define the Gradio interface
with gr.Blocks() as demo:
    gr.Markdown("## Basic Outline Extractor")
    gr.Markdown("Upload an image to extract its outline with default settings.")

    with gr.Row():
        image_input = gr.Image(type="numpy", label="Input Image")
        output_image = gr.Image(type="numpy", label="Output Outline Image")

    process_button = gr.Button("Generate Outline")
    process_button.click(fn=extract_outline, inputs=image_input, outputs=output_image)

# Launch the Gradio app
demo.launch()