datafreak's picture
minor algorithm changes
074fc66 verified
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()