Welcome to the story of the Laplacian and Laplacian of Gaussian filter.
In this blog, Let’s see the Laplacian filter and Laplacian of Gaussian filter and the implementation in Python. The story of the Laplacian filter starts from the Laplacian matrix in Graph theory which is the simplest method of representation of a graph in the matrix. The Laplacian of an image highlights regions of rapid intensity change. Any feature with a sharp discontinuity will be enhanced by a Laplacian operator. The Laplacian filter comes under the derivative filter category. It is a second-order filter used in image processing for edge detection and feature extraction. When we use first-order derivative filters, we must apply separate filters to detect vertical and horizontal edges and then combine both. But the Laplacian filter detects all the edges irrespective of directions.
Mathematically, Laplacian filter is defined as:
There exist 2 types of laplacian filters.
- Positive laplacian
- Negative laplacian
Positive laplacian operator uses a mask with center element as a negative value and corner elements as 0. This filter identifies the outward edges from an image. An example filter mask is given below.
Negative laplacian operator is used to find the inward edges of the image. It uses a standard mask with the center element as positive, corners as 0 and all other elements as -1. An example is given below.
In both cases, the sum of values in the filter should be 0. Different variations of the standard mask are available. You may try on it.
Zero-crossing Feature
A zero crossing is a point where the sign of a mathematical function changes in the graph of the function. In image processing, the edge detection using Laplacian filter takes place by marking the points that leads to zero in graph as potential edge points. This method works fine on images for finding edges in both directions, but it works poorly when noises are found in the image. So, we usually smooth the image applying Guassian filter prior to Laplacian filter. It’s often termed Laplacian of Guassian (LoG) filter. We can combine Guassian and Laplacian operations together and the mathematical representation of the combined filter is as follows:
Code Block
Method 1
The OpenCV built-in function method to implement LoG filter is mentioned below.
#OPENCV implementation
import cv2
import matplotlib.pyplot as plt
image = cv2.imread(r"E:\eye.png", cv2.IMREAD_COLOR)
image = cv2.GaussianBlur(image, (3, 3), 0)
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
filtered_image = cv2.Laplacian(image_gray, cv2.CV_16S, ksize=3)
# Plot the original and filtered images
plt.figure(figsize=(10, 5))
plt.subplot(121)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.subplot(122)
plt.imshow(filtered_image, cmap='gray')
plt.title('LoG Filtered Image')
plt.show()
Output of the program:
Method 2
The Python function for implementing LoG filter in openCV is shown below.
import cv2
import matplotlib.pyplot as plt
import numpy as np
def LoG_filter_opencv(image, sigma, size=None):
# Generate LoG kernel
if size is None:
size = int(6 * sigma + 1) if sigma >= 1 else 7
if size % 2 == 0:
size += 1
x, y = np.meshgrid(np.arange(-size//2+1, size//2+1), np.arange(-size//2+1, size//2+1))
kernel = -(1/(np.pi * sigma**4)) * (1 - ((x**2 + y**2) / (2 * sigma**2))) * np.exp(-(x**2 + y**2) / (2 * sigma**2))
kernel = kernel / np.sum(np.abs(kernel))
# Perform convolution using OpenCV filter2D
result = cv2.filter2D(image, -1, kernel)
return result
# Example usage:
image = cv2.imread(r"E:\eye.png", cv2.IMREAD_GRAYSCALE) # Replace 'path_to_your_image.png' with your image path
sigma = 2.0
filtered_image = LoG_filter_opencv(image, sigma)
filtered_image = cv2.convertScaleAbs(filtered_image)
plt.imshow(filtered_image, cmap="gray")
The output of the program:
Method 3
The Python function implementation of LoG filter using scipy package is given below.
import numpy as np
import matplotlib.pyplot as plt
from scipy.ndimage import convolve
from scipy import misc
def LoG_filter(image, sigma, size=None):
# Generate LoG kernel
if size is None:
size = int(6 * sigma + 1) if sigma >= 1 else 7
if size % 2 == 0:
size += 1
x, y = np.meshgrid(np.arange(-size//2+1, size//2+1), np.arange(-size//2+1, size//2+1))
kernel = -(1/(np.pi * sigma**4)) * (1 - ((x**2 + y**2) / (2 * sigma**2))) * np.exp(-(x**2 + y**2) / (2 * sigma**2))
kernel = kernel / np.sum(np.abs(kernel))
# Perform convolution
result = convolve(image, kernel)
return result
# Example usage:
image = cv2.imread(r"E:\eye.png", cv2.IMREAD_GRAYSCALE) # Replace 'path_to_your_image.png' with your image path
sigma = 2.0
filtered_image = LoG_filter(image, sigma)
# Plot the original and filtered images
plt.figure(figsize=(10, 5))
plt.subplot(121)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.subplot(122)
plt.imshow(filtered_image, cmap='gray')
plt.title('LoG Filtered Image')
plt.show()
Output of the program:
Hope you enjoyed reading.Here comes the link about another article on commonly used filters in image processing Different Filters for Image processing | by Raji Lini | Medium.