0
0
RosHow-ToBeginner · 3 min read

How to Apply High Pass Filter to Image for Edge Detection

To apply a high pass filter to an image, you can use convolution with a kernel that emphasizes high-frequency components like edges. This is done by subtracting a low pass filtered image from the original or directly using a high pass kernel such as the Laplacian filter in scipy.ndimage or cv2.filter2D.
📐

Syntax

Applying a high pass filter involves convolving the image with a filter kernel that highlights rapid intensity changes. The general syntax in Python using scipy.ndimage is:

  • filtered_image = scipy.ndimage.convolve(image, kernel)
  • image: 2D array representing the grayscale image
  • kernel: 2D array representing the high pass filter mask

Common kernels include the Laplacian kernel which detects edges by emphasizing differences between a pixel and its neighbors.

python
import numpy as np
from scipy.ndimage import convolve

# Example high pass filter kernel (Laplacian)
laplacian_kernel = np.array([[0, -1, 0],
                             [-1, 4, -1],
                             [0, -1, 0]])

# Apply convolution
filtered_image = convolve(image, laplacian_kernel)
💻

Example

This example shows how to apply a high pass filter to a grayscale image using the Laplacian kernel. It highlights edges by enhancing high-frequency details.

python
import numpy as np
import matplotlib.pyplot as plt
from scipy.ndimage import convolve
from imageio import imread

# Load a grayscale image
image = imread('https://upload.wikimedia.org/wikipedia/commons/thumb/2/24/Lenna.png/256px-Lenna.png', as_gray=True)

# Define Laplacian high pass filter kernel
laplacian_kernel = np.array([[0, -1, 0],
                             [-1, 4, -1],
                             [0, -1, 0]])

# Apply high pass filter
filtered_image = convolve(image, laplacian_kernel)

# Plot original and filtered images
plt.figure(figsize=(10,4))
plt.subplot(1,2,1)
plt.title('Original Image')
plt.imshow(image, cmap='gray')
plt.axis('off')

plt.subplot(1,2,2)
plt.title('High Pass Filtered Image')
plt.imshow(filtered_image, cmap='gray')
plt.axis('off')
plt.show()
Output
A figure window showing two images side by side: the original grayscale image on the left and the high pass filtered image on the right with edges highlighted.
⚠️

Common Pitfalls

  • Applying the filter to a color image without converting to grayscale can cause unexpected results.
  • Not normalizing or clipping the filtered image can produce values outside the displayable range, causing visualization issues.
  • Using a kernel that is not centered or incorrectly sized can distort the output.
  • Confusing high pass filtering with sharpening; sharpening often combines the original image with the high pass filtered result.
python
import numpy as np
from scipy.ndimage import convolve

# Wrong: Applying filter directly to color image
# filtered = convolve(color_image, laplacian_kernel)  # This will error or give wrong output

# Right: Convert to grayscale first
# from skimage.color import rgb2gray
# grayscale = rgb2gray(color_image)
# filtered = convolve(grayscale, laplacian_kernel)
📊

Quick Reference

Tips for applying high pass filters to images:

  • Always convert color images to grayscale before filtering.
  • Use kernels like Laplacian or custom high pass masks.
  • Normalize or clip output values to valid image ranges (0-1 or 0-255).
  • Visualize results side-by-side with original for comparison.

Key Takeaways

Use convolution with a high pass kernel like Laplacian to highlight edges in images.
Convert color images to grayscale before applying high pass filters to avoid errors.
Normalize or clip filtered image values to keep them in displayable range.
High pass filtering emphasizes rapid intensity changes, useful for edge detection.
Visualize original and filtered images side-by-side to understand filter effects.