Skip to main content
CNN-based Image Filtering and
Visualization
Understanding Edge Detection with
Sobel Filters
Overview
• This presentation demonstrates how a CNN
with a Sobel filter processes images:
• - Applies Sobel filter to original and blurred
images
• - Visualizes edge detection behavior
• - Uses a single Conv2D layer without activation
Step 1: Install Dependencies
• !pip install tensorflow opencv-python
matplotlib --quiet
Step 2: Download Image
• Download a sample image from Wikimedia:
• - URL: Golden retriever image
• - Saved as 'dog.jpg'
Step 3: Load & Preprocess Image
• - Convert to grayscale
• - Resize to 128x128
• - Normalize pixel values
• - Reshape to (1, 128, 128, 1)
Step 4: Build CNN Model
• - Single Conv2D layer
• - 1 filter, 3x3 kernel
• - No activation function
• - No bias
Step 5: Set Sobel Filter
• Manually set the Conv2D weights to a Sobel X
filter:
• [[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]
• Detects vertical edges
Step 6: Apply Filter to Original
• - Use model.predict on the original image
• - Get 2D filtered output
• - Shows detected edges
Step 7: Blur and Filter Again
• - Apply Gaussian blur to original
• - Filter blurred image using same CNN
• - Shows reduced edge intensity
Step 8: Visualization
• Show 4 subplots:
• - Original
• - CNN Filtered
• - Blurred
• - CNN Filtered on Blurred
Conclusion
• This example demonstrates:
• - How CNNs use filters for feature detection
• - Sobel filter behavior
• - Effect of blurring on edge detection
What is a CNN Model?
• A Convolutional Neural Network (CNN) is a
deep learning model used primarily for image
processing tasks.
• Key Components:
• 1. **Convolutional Layers** – Extract local
features from the input image using filters.
• 2. **Activation Function (ReLU)** – Adds non-
linearity to the network.