Skip to content

Latest commit

Β 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Β 
Β 
Β 
Β 
Β 
Β 

README.md

πŸ–ΌοΈ Image Sharpening Using Custom Convolution Filters

πŸ“Œ Introduction

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.


🧠 What is Image Sharpening?

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.


πŸ” Convolution 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.


✨ Sharpening Filter 1

sharpen_filter1 = np.array([
    [ 0, -1,  0],
    [-1,  5, -1],
    [ 0, -1,  0]
])

Characteristics:

  • 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.


πŸ”₯ Sharpening Filter 2

sharpen_filter2 = np.array([
    [-1, -1, -1],
    [-1,  9, -1],
    [-1, -1, -1]
])

Characteristics:

  • Stronger sharpening effect
  • Produces more noticeable edges
  • Can increase noise in images

This filter uses all neighboring pixels, making the sharpening effect more aggressive.


βš™οΈ Applying the Filter

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 depth
  • sharpen_filter1 β†’ custom convolution kernel

πŸ“Š Comparing Filters

Different kernels create different sharpening behaviors:

Filter Strength Result
Filter 1 Medium Natural sharpening
Filter 2 Strong More detailed but may amplify noise

πŸ–ΌοΈ Example Workflow

Original Image
       |
       ↓
Create Custom Kernel
       |
       ↓
Apply Convolution
       |
       ↓
Sharpened Image

πŸ“š Learning Goals

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

πŸ› οΈ Requirements

Install required libraries:

pip install numpy opencv-python matplotlib