datafreak commited on
Commit
074fc66
·
verified ·
1 Parent(s): 4dac4f6

minor algorithm changes

Browse files
Files changed (1) hide show
  1. app.py +16 -36
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, 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()
 
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()