Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
722 B
4
Indexable
import scipy.signal as signal

# Write a function that applies edge filter on a 2D matrix to return the outline of a mask
def edge_filter_mask(image):
    # Define the Sobel filter
    sobel_x = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])
    sobel_y = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]])
    
    # Apply the filter on the image
    image_x = signal.convolve2d(image, sobel_x, mode='same', boundary='symm')
    image_y = signal.convolve2d(image, sobel_y, mode='same', boundary='symm')
    
    # Compute the magnitude
    magnitude = np.sqrt(np.square(image_x) + np.square(image_y))
    
    # Compute the outline
    outline = np.zeros_like(magnitude)
    outline[magnitude > 0] = 1
    
    return outline