Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.2 kB
7
Indexable
Never
import numpy as np
import matplotlib.pyplot as plt
import scipy
import cv2



img = cv2.imread('/content/baboon.bmp')

plt.imshow(img, cmap='gray')
plt.show()


# Define the Laplacian of Gaussian filter
def LoG_filter(K, sigma):
    kernel = np.zeros((K,K))
    for i in range(-int((K-1)/2), int((K-1)/2)+1):
        for j in range(-int((K-1)/2), int((K-1)/2)+1):
            kernel[i+int((K-1)/2),j+int((K-1)/2)] = (-1/(np.pi*(sigma**4))) * (1 - ((i**2+j**2)/(2*(sigma**2)))) * np.exp(-(i**2+j**2)/(2*(sigma**2)))
    return kernel

# Apply the Laplacian of Gaussian filter
K = 3 # filter size
sigma = 3 # standard deviation of Gaussian
LoG = LoG_filter(K, sigma)
edges = cv2.filter2D(img, -1, LoG, borderType=cv2.BORDER_REPLICATE)

# Display the edge map
plt.imshow(edges, cmap='gray')
plt.title('Edge map before threshold (sigma={}, K={})'.format(sigma, K))
plt.show()

# Apply a threshold to the edge map
threshold = 3
edges_bin = np.zeros_like(edges)
edges_bin[edges > threshold] = 255

# Display the edge map
plt.imshow(edges_bin, cmap='gray')
plt.title('Edge map (sigma={}, K={}, threshold={})'.format(sigma, K, threshold))
plt.show()