Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,82 +1,33 @@
|
|
1 |
import cv2
|
2 |
-
import numpy as np
|
3 |
import gradio as gr
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
# Function to handle mouse events
|
10 |
-
def mouse_event(event, x, y, flags, param):
|
11 |
-
global roi_points, drawing
|
12 |
-
if event == cv2.EVENT_LBUTTONDOWN:
|
13 |
-
drawing = True
|
14 |
-
roi_points = [(x, y)]
|
15 |
-
elif event == cv2.EVENT_MOUSEMOVE:
|
16 |
-
if drawing:
|
17 |
-
roi_points.append((x, y))
|
18 |
-
elif event == cv2.EVENT_LBUTTONUP:
|
19 |
-
drawing = False
|
20 |
-
roi_points.append((x, y))
|
21 |
-
|
22 |
-
# Function to perform image segmentation using the watershed algorithm
|
23 |
-
def segment_image(input_image):
|
24 |
-
# Convert the input image (NumPy array) to a format that OpenCV can use
|
25 |
-
image = input_image.astype(np.uint8)
|
26 |
-
|
27 |
-
# Create a copy of the image for manual segmentation
|
28 |
-
manual_segmentation_image = image.copy()
|
29 |
-
|
30 |
-
# Create a mask for manual segmentation
|
31 |
-
mask = np.zeros(image.shape[:2], dtype=np.uint8)
|
32 |
-
|
33 |
-
# Create a window for manual segmentation
|
34 |
-
cv2.namedWindow("Manual Segmentation")
|
35 |
-
cv2.setMouseCallback("Manual Segmentation", mouse_event)
|
36 |
-
|
37 |
-
while True:
|
38 |
-
for point in roi_points:
|
39 |
-
cv2.circle(manual_segmentation_image, point, 5, (0, 0, 255), -1)
|
40 |
-
|
41 |
-
cv2.imshow("Manual Segmentation", manual_segmentation_image)
|
42 |
-
key = cv2.waitKey(1) & 0xFF
|
43 |
-
|
44 |
-
# Press 's' to perform segmentation
|
45 |
-
if key == ord("s"):
|
46 |
-
break
|
47 |
-
# Press 'r' to reset manual segmentation
|
48 |
-
elif key == ord("r"):
|
49 |
-
manual_segmentation_image = image.copy()
|
50 |
-
roi_points = []
|
51 |
-
|
52 |
-
# Close the manual segmentation window
|
53 |
-
cv2.destroyWindow("Manual Segmentation")
|
54 |
-
|
55 |
-
# Convert the image to grayscale
|
56 |
-
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
57 |
|
58 |
-
#
|
59 |
-
|
60 |
-
roi_points = np.array(roi_points, np.int32)
|
61 |
-
cv2.fillPoly(mask, [roi_points], 255)
|
62 |
|
63 |
-
# Apply the watershed algorithm
|
64 |
-
|
|
|
65 |
|
66 |
-
#
|
67 |
-
|
68 |
-
output_image[mask == -1] = [0, 0, 255] # Mark watershed boundaries in red
|
69 |
|
70 |
-
return
|
71 |
|
72 |
-
#
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
outputs="image",
|
77 |
-
title="Manual Image Segmentation using Watershed Algorithm",
|
78 |
-
description="Upload an image and perform manual image segmentation by drawing regions of interest (ROIs) before segmenting. Press 's' to segment and 'r' to reset ROIs.",
|
79 |
-
)
|
80 |
|
81 |
-
#
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import cv2
|
|
|
2 |
import gradio as gr
|
3 |
|
4 |
+
# Define a function for marker-based segmentation using the OpenCV watershed algorithm
|
5 |
+
def watershed_segmentation(input_image, markers_image):
|
6 |
+
# Load the input image
|
7 |
+
image = cv2.imdecode(input_image, cv2.IMREAD_COLOR)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
# Load the marker image (binary image with markers)
|
10 |
+
markers = cv2.imdecode(markers_image, cv2.IMREAD_GRAYSCALE)
|
|
|
|
|
11 |
|
12 |
+
# Apply the watershed algorithm
|
13 |
+
image_copy = image.copy()
|
14 |
+
cv2.watershed(image_copy, markers)
|
15 |
|
16 |
+
# Apply color mapping to the segmented regions
|
17 |
+
segmented_image = cv2.applyColorMap(markers, cv2.COLORMAP_JET)
|
|
|
18 |
|
19 |
+
return segmented_image
|
20 |
|
21 |
+
# Define Gradio interfaces for the input and output
|
22 |
+
input_image = gr.inputs.Image()
|
23 |
+
markers_image = gr.inputs.Image()
|
24 |
+
output_image = gr.outputs.Image()
|
|
|
|
|
|
|
|
|
25 |
|
26 |
+
# Create a Gradio app
|
27 |
+
gr.Interface(
|
28 |
+
fn=watershed_segmentation,
|
29 |
+
inputs=[input_image, markers_image],
|
30 |
+
outputs=output_image,
|
31 |
+
title="Marker-Based Segmentation with Watershed Algorithm",
|
32 |
+
description="Upload an image and a marker image to perform marker-based segmentation.",
|
33 |
+
).launch()
|