File size: 925 Bytes
601150c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ef4803
601150c
 
4ee9d59
601150c
 
 
 
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
import cv2
import numpy as np
import gradio as gr
import matplotlib.pyplot as plt

def gradient_magnitude_edge_detection(image_path, threshold):
    # Read the image
    img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)

    # Apply Sobel operator to calculate gradients
    gradient_x = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3)
    gradient_y = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3)

    # Calculate gradient magnitude
    gradient_magnitude = np.sqrt(gradient_x**2 + gradient_y**2)

    # Thresholding to obtain edges
    edges = (gradient_magnitude > threshold) * 255
    return edges

   

# Define the Gradio interface
iface = gr.Interface(
    fn=gradient_magnitude_edge_detection,
    inputs=[
        gr.Image(type="filepath", label="Select Image"),
        gr.Slider(minimum=0, maximum=255, value=50, label="Threshold")
    ],
    outputs="image",
    live=True
)

# Launch the Gradio interface
iface.launch()