Image sharpening is an important technique in digital image processing used to enhance edges and fine details in an image.
In this tutorial, we learn how to create and apply custom sharpening filters (kernels) using:
- Python
- NumPy
- OpenCV
Instead of using built-in sharpening functions, we manually define convolution kernels and observe their effects on images.
Sharpening is a process that increases the visibility of edges and small details in an image.
A sharpened image usually has:
- Stronger edges
- Higher local contrast
- More visible details
This is achieved by applying a convolution operation using a predefined kernel.
A convolution kernel is a small matrix that slides over the image pixels and modifies each pixel based on its neighbors.
Example:
kernel = np.array([
[0, -1, 0],
[-1, 5, -1],
[0, -1, 0]
])The center value increases the contribution of the current pixel, while negative values reduce the influence of surrounding pixels.
sharpen_filter1 = np.array([
[ 0, -1, 0],
[-1, 5, -1],
[ 0, -1, 0]
])- Moderate sharpening effect
- Enhances edges while keeping a natural appearance
- Commonly used for general image enhancement
Mathematically, this kernel increases the original pixel intensity and subtracts neighboring information to highlight changes.
sharpen_filter2 = np.array([
[-1, -1, -1],
[-1, 9, -1],
[-1, -1, -1]
])- Stronger sharpening effect
- Produces more noticeable edges
- Can increase noise in images
This filter uses all neighboring pixels, making the sharpening effect more aggressive.
Using OpenCV, convolution can be applied with:
import cv2
sharpened_image = cv2.filter2D(
image,
-1,
sharpen_filter1
)Where:
imageβ input image-1β keeps the original image depthsharpen_filter1β custom convolution kernel
Different kernels create different sharpening behaviors:
| Filter | Strength | Result |
|---|---|---|
| Filter 1 | Medium | Natural sharpening |
| Filter 2 | Strong | More detailed but may amplify noise |
Original Image
|
β
Create Custom Kernel
|
β
Apply Convolution
|
β
Sharpened Image
After completing this tutorial, you will understand:
- What image sharpening means
- How convolution works in image processing
- How kernels affect image quality
- How to design custom filters using NumPy
- How OpenCV applies convolution operations
Install required libraries:
pip install numpy opencv-python matplotlib