Spaces:
Running
Running
main files
Browse files- app.py +63 -0
- requirements.txt +0 -0
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
def extract_outline(image, blur_level, block_size, c_value):
|
6 |
+
# Convert to grayscale
|
7 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
8 |
+
|
9 |
+
# Adjust Gaussian blur kernel size based on blur level
|
10 |
+
blur_kernel_size = (5 + (blur_level * 2), 5 + (blur_level * 2)) # Adjust kernel size for blur
|
11 |
+
blurred = cv2.GaussianBlur(gray, blur_kernel_size, 0)
|
12 |
+
|
13 |
+
# Use adaptive thresholding with specified block size and constant
|
14 |
+
binary = cv2.adaptiveThreshold(blurred, 255,
|
15 |
+
cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
|
16 |
+
cv2.THRESH_BINARY_INV,
|
17 |
+
blockSize=max(block_size, 3) | 1, # Ensure block size is odd
|
18 |
+
C=c_value)
|
19 |
+
|
20 |
+
# Dilate the binary image to enhance primary structure
|
21 |
+
kernel = np.ones((3, 3), np.uint8)
|
22 |
+
dilated = cv2.dilate(binary, kernel, iterations=1)
|
23 |
+
|
24 |
+
# Apply morphological thinning to get single-pixel-wide lines
|
25 |
+
thinned = cv2.ximgproc.thinning(dilated)
|
26 |
+
|
27 |
+
# Invert colors to get a white background and black outline
|
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 Structure Outline Extractor")
|
35 |
+
gr.Markdown("Upload an image and adjust the sliders to control the amount of detail captured in the outline.")
|
36 |
+
|
37 |
+
with gr.Row():
|
38 |
+
image_input = gr.Image(type="numpy", label="Input Image")
|
39 |
+
|
40 |
+
with gr.Column():
|
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=[image_input, blur_slider, block_size_slider, c_value_slider], outputs=output_image)
|
61 |
+
|
62 |
+
# Launch the Gradio app
|
63 |
+
demo.launch()
|
requirements.txt
ADDED
Binary file (2.32 kB). View file
|
|