Laplacian Pattern example with Simple Python
Python (NumPy) Example:
Here’s how you might apply a Laplacian filter manually to a small grayscale image:
import numpy as np from scipy.signal import convolve2d import matplotlib.pyplot as plt # Sample grayscale image image = np.array([ [10, 10, 10, 10, 10], [10, 50, 50, 50, 10], [10, 50, 90, 50, 10], [10, 50, 50, 50, 10], [10, 10, 10, 10, 10] ]) # Laplacian kernel laplacian_kernel = np.array([ [0, -1, 0], [-1, 4, -1], [0, -1, 0] ]) # Apply the filter laplacian_output = convolve2d(image, laplacian_kernel, mode='same') # Show results plt.subplot(1, 2, 1) plt.title("Original") plt.imshow(image, cmap='gray') plt.subplot(1, 2, 2) plt.title("Laplacian Edge") plt.imshow(laplacian_output, cmap='gray') plt.show()
Summary Table:
Feature | Description |
---|---|
Operator Type | Second derivative (isotropic) |
Detects | Edges, corners, small intensity changes |
Sensitive To | Noise (usually combined with Gaussian) |
Common Use | Edge detection in pre-processing |
Best With | Cleaned/blurred images |
Step-by-Step Python Example of Laplacian of Gaussian (LoG)
import numpy as np import matplotlib.pyplot as plt from scipy.ndimage import gaussian_filter, laplace # Create a sample image with sharp edges image = np.zeros((100, 100)) image[30:70, 30:70] = 255 # A white square in the center # Step 1: Smooth image using Gaussian filter blurred = gaussian_filter(image, sigma=2) # Step 2: Apply Laplacian operator log_result = laplace(blurred) # Visualize the results plt.figure(figsize=(12, 4)) plt.subplot(1, 3, 1) plt.title("Original") plt.imshow(image, cmap='gray') plt.subplot(1, 3, 2) plt.title("Gaussian Blurred") plt.imshow(blurred, cmap='gray') plt.subplot(1, 3, 3) plt.title("Laplacian of Gaussian (LoG)") plt.imshow(log_result, cmap='gray') plt.tight_layout() plt.show()
Output Description:
Image Part | Description |
---|---|
Original | A sharp-edged white square on black background |
Gaussian Blurred | Smooth transition from black to white (noise reduced) |
LoG Output | Edges of the square highlighted with bipolar transitions (black-white borders) |
Intuition:
- Why Gaussian first?
Laplacian is sensitive to noise. The Gaussian blur suppresses noise while preserving overall structure. - Why Laplacian after?
It finds the zero-crossings in second derivative, which signal the edges.