MATLAB - Laplacian of Gaussian Filter
MATLAB - Laplacian of Gaussian (LoG) for Edge Detection
Section titled “MATLAB - Laplacian of Gaussian (LoG) for Edge Detection”The Laplacian of Gaussian (LoG) is a powerful filter used in image processing for detecting edges. It combines two steps into one: smoothing the image with a Gaussian filter to reduce noise, and then applying a Laplacian filter to find regions of rapid intensity change (i.e., edges).
The LoG is known as a second-derivative operator. Edges in an image correspond to zero-crossings in the filtered output (where pixel values change from positive to negative). By adjusting the sigma (standard deviation) of the Gaussian component, you can detect edges at different scales—a small sigma finds fine details, while a large sigma finds broader, more prominent edges.
Modern Approach: Creating the LoG Filter Manually
Section titled “Modern Approach: Creating the LoG Filter Manually”While MATLAB has a legacy fspecial('log') function, the modern and more instructive approach is to create the filter kernel yourself. This gives you full control and a better understanding of the process.
Example 1: Manual LoG Filter Creation and Application
Section titled “Example 1: Manual LoG Filter Creation and Application”%% 1. Load and Prepare the ImageoriginalImage = imread('peppers.png');grayImage = rgb2gray(originalImage);
% Convert to double for accurate calculationsdoubleImage = double(grayImage);
%% 2. Define LoG Filter Parameters% Sigma controls the scale of edges to be detectedsigma = 2.0;
% Determine filter size based on sigma (a common rule of thumb)filterSize = 2 * ceil(3 * sigma) + 1;
%% 3. Create the LoG Filter Kernel% Create a grid of (x,y) coordinates[X, Y] = meshgrid(-floor(filterSize/2):floor(filterSize/2));
% The Laplacian of Gaussian formulalogKernel = -1 / (pi * sigma^4) * (1 - (X.^2 + Y.^2) / (2 * sigma^2)) .* exp(-(X.^2 + Y.^2) / (2 * sigma^2));
% Best practice: Normalize the kernel to sum to zero for a Laplacian operatorlogKernel = logKernel - mean(logKernel(:));
%% 4. Apply the Filter% 'replicate' handles border pixels by copying edge pixel valuesfilteredImage = imfilter(doubleImage, logKernel, 'replicate');
%% 5. Display the Resultsfigure;subplot(1, 2, 1);imshow(grayImage);title('Original Grayscale Image');
% The LoG output highlights edges. We use an empty colormap range `[]`% to autoscale the display to show both positive and negative values.subplot(1, 2, 2);imshow(filteredImage, []);title(sprintf('LoG Filtered (Sigma = %.1f)', sigma));Explanation
Section titled “Explanation”- Filter Creation: We first create a coordinate grid (
meshgrid). Then, we apply the mathematical formula for the 2D LoG function to this grid. This creates a kernel (a small matrix) that represents the filter. - Normalization: A key step for second-derivative filters is ensuring the kernel’s elements sum to zero. This guarantees that the filter has no response in flat, uniform areas of the image, only at changes in intensity.
- Filtering:
imfilterconvolves the image with our customlogKernel. We use the'replicate'option to avoid dark borders caused by zero-padding. - Displaying: The output of the LoG filter contains positive, negative, and zero values. To visualize this correctly,
imshow(filteredImage, [])scales the display range to fit the data, typically showing positive values as white, negative values as black, and zero-crossings (the edges) as gray.
Expected Output
Section titled “Expected Output”The output window will show the original image on the left. On the right, the filtered image will be mostly gray. Strong edges in the original image will appear as sharp, thin double lines (one black, one white). This black-to-white transition represents the zero-crossing, which precisely locates the edge.
Legacy Method: Using fspecial
Section titled “Legacy Method: Using fspecial”For quick tests or working with older code, you can still use fspecial. It’s less flexible but more concise.
Example 2: Comparing a Simple Laplacian with LoG
Section titled “Example 2: Comparing a Simple Laplacian with LoG”This example highlights why the Gaussian smoothing in LoG is so important. A pure Laplacian filter is very sensitive to noise.
I = imread('cameraman.tif');
% 1. Simple Laplacian Filter (noise-sensitive)h_lap = fspecial('laplacian', 0.2); % 0.2 is the alpha parameterI_lap = imfilter(I, h_lap, 'replicate');
% 2. Laplacian of Gaussian (LoG) Filter (noise-robust)h_log = fspecial('log', [5 5], 0.5); % 5x5 filter, sigma=0.5I_log = imfilter(I, h_log, 'replicate');
% Display comparisonfigure;subplot(1, 3, 1); imshow(I); title('Original');subplot(1, 3, 2); imshow(I_lap, []); title('Laplacian Only');subplot(1, 3, 3); imshow(I_log, []); title('Laplacian of Gaussian');Expected Output
Section titled “Expected Output”A figure will show three images. The ‘Original’ is the input. The ‘Laplacian Only’ image will show edges but also a lot of speckled noise across the entire image. The ‘Laplacian of Gaussian’ image will have much cleaner, more distinct edges with the background noise suppressed, demonstrating the effectiveness of the initial Gaussian smoothing.