Spaces:
Sleeping
Sleeping
minor algorithm changes
Browse files
app.py
CHANGED
|
@@ -2,62 +2,42 @@ import cv2
|
|
| 2 |
import numpy as np
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
-
def extract_outline(image
|
| 6 |
# Convert to grayscale
|
| 7 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
blur_kernel_size = (5
|
| 11 |
blurred = cv2.GaussianBlur(gray, blur_kernel_size, 0)
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
blockSize=max(block_size, 3) | 1, # Ensure block size is odd
|
| 18 |
-
C=c_value)
|
| 19 |
|
| 20 |
-
#
|
| 21 |
kernel = np.ones((3, 3), np.uint8)
|
| 22 |
-
|
| 23 |
|
| 24 |
# Apply morphological thinning to get single-pixel-wide lines
|
| 25 |
-
thinned = cv2.ximgproc.thinning(
|
| 26 |
|
| 27 |
-
# Invert colors
|
| 28 |
skeleton_on_white = cv2.bitwise_not(thinned)
|
| 29 |
|
| 30 |
return skeleton_on_white
|
| 31 |
|
| 32 |
# Define the Gradio interface
|
| 33 |
with gr.Blocks() as demo:
|
| 34 |
-
gr.Markdown("## Basic
|
| 35 |
-
gr.Markdown("Upload an image
|
| 36 |
|
| 37 |
with gr.Row():
|
| 38 |
image_input = gr.Image(type="numpy", label="Input Image")
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
blur_slider = gr.Slider(
|
| 42 |
-
minimum=0, maximum=5, value=2, step=1,
|
| 43 |
-
label="Gaussian Blur Level",
|
| 44 |
-
info="Higher values apply more blur to the image."
|
| 45 |
-
)
|
| 46 |
-
block_size_slider = gr.Slider(
|
| 47 |
-
minimum=3, maximum=21, value=11, step=2,
|
| 48 |
-
label="Adaptive Threshold Block Size",
|
| 49 |
-
info="Odd values control the size of the blocks for thresholding."
|
| 50 |
-
)
|
| 51 |
-
c_value_slider = gr.Slider(
|
| 52 |
-
minimum=0, maximum=20, value=5, step=1,
|
| 53 |
-
label="Adaptive Threshold Constant (C)",
|
| 54 |
-
info="Adjust the constant subtracted from the mean in adaptive thresholding."
|
| 55 |
-
)
|
| 56 |
-
|
| 57 |
-
output_image = gr.Image(type="numpy", label="Output Outline Image")
|
| 58 |
-
|
| 59 |
process_button = gr.Button("Generate Outline")
|
| 60 |
-
process_button.click(fn=extract_outline, inputs=
|
| 61 |
|
| 62 |
# Launch the Gradio app
|
| 63 |
demo.launch()
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
+
def extract_outline(image):
|
| 6 |
# Convert to grayscale
|
| 7 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 8 |
|
| 9 |
+
# Set default Gaussian blur kernel size
|
| 10 |
+
blur_kernel_size = (5, 5)
|
| 11 |
blurred = cv2.GaussianBlur(gray, blur_kernel_size, 0)
|
| 12 |
|
| 13 |
+
# Set default Canny edge detection thresholds
|
| 14 |
+
lower_threshold = 50
|
| 15 |
+
upper_threshold = 150
|
| 16 |
+
edges = cv2.Canny(blurred, lower_threshold, upper_threshold)
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# Morphological operations to close gaps
|
| 19 |
kernel = np.ones((3, 3), np.uint8)
|
| 20 |
+
closed_edges = cv2.morphologyEx(edges, cv2.MORPH_CLOSE, kernel)
|
| 21 |
|
| 22 |
# Apply morphological thinning to get single-pixel-wide lines
|
| 23 |
+
thinned = cv2.ximgproc.thinning(closed_edges)
|
| 24 |
|
| 25 |
+
# Invert colors for white background and black outline
|
| 26 |
skeleton_on_white = cv2.bitwise_not(thinned)
|
| 27 |
|
| 28 |
return skeleton_on_white
|
| 29 |
|
| 30 |
# Define the Gradio interface
|
| 31 |
with gr.Blocks() as demo:
|
| 32 |
+
gr.Markdown("## Basic Outline Extractor")
|
| 33 |
+
gr.Markdown("Upload an image to extract its outline with default settings.")
|
| 34 |
|
| 35 |
with gr.Row():
|
| 36 |
image_input = gr.Image(type="numpy", label="Input Image")
|
| 37 |
+
output_image = gr.Image(type="numpy", label="Output Outline Image")
|
| 38 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
process_button = gr.Button("Generate Outline")
|
| 40 |
+
process_button.click(fn=extract_outline, inputs=image_input, outputs=output_image)
|
| 41 |
|
| 42 |
# Launch the Gradio app
|
| 43 |
demo.launch()
|