File size: 1,371 Bytes
3673b9e
856189d
 
a6a05ca
bae6c47
 
 
 
 
 
 
 
 
 
 
 
 
8711191
bae6c47
 
8711191
bae6c47
 
 
 
 
 
 
 
 
 
 
 
 
be7826b
 
bae6c47
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
import gradio as gr
import cv2
import numpy as np

# Function to perform image segmentation using OpenCV Watershed Algorithm
def watershed_segmentation(input_image, scribble_image):
    # Load the input image and scribble image
    image = cv2.cvtColor(input_image.astype('uint8'), cv2.COLOR_RGBA2BGR)
    scribble = cv2.cvtColor(scribble_image.astype('uint8'), cv2.COLOR_RGBA2GRAY)

    # Convert scribble to markers (0 for background, 1 for unknown, 2 for foreground)
    markers = np.zeros_like(scribble)
    markers[scribble == 0] = 0
    markers[scribble == 255] = 1
    markers[scribble == 128] = 2

    # Apply watershed algorithm
    cv2.watershed(input_image, markers)

    # Create a segmented mask
    segmented_mask = np.zeros_like(input_image, dtype=np.uint8)
    segmented_mask[markers == 2] = [0, 0, 255]  # Red color for segmented regions

    return segmented_mask

# Gradio interface
input_image = gr.inputs.Image(type='pil', label="Upload an image")
scribble_image = gr.inputs.Image(type='pil', label="Scribble on the image")
output_image = gr.outputs.Image(type='pil', label="Segmented Image")

gr.Interface(
    fn=watershed_segmentation,
    inputs=[input_image, scribble_image],
    outputs=output_image,
    title="Swallowtail Image Segmentation Algorithm",
    description="Upload an image and scribble on it to perform segmentation.",
).launch()