This repository contains a Python implementation of a custom convolution-based image downscaler. The code demonstrates how to apply a convolution operation manually (without relying heavily on libraries like OpenCV or SciPy) to reduce the resolution of an image while preserving its features.
The project consists of two main functions:
downscaler_kernel()→ Generates a custom kernel for downscaling.convolution()→ Applies the kernel to an image using convolution.
The goal is to reduce image resolution while maintaining structural integrity, simulating how some image processing pipelines downscale images before further analysis.
- Creates a square kernel of size
size × size. - The kernel has a central value of 1 (to preserve the main pixel) and 0.35 in surrounding positions (to softly blend neighboring pixels).
- Example for
size=5:[[0.35, 0.35, 0.35, 0.35, 0.35], [0.35, 0.35, 0.35, 0.35, 0.35], [0.35, 0.35, 1, 0.35, 0.35], [0.35, 0.35, 0.35, 0.35, 0.35], [0.35, 0.35, 0.35, 0.35, 0.35]]
- Applies the kernel to the input image using discrete convolution.
- Parameters:
image: Input grayscale image (NumPy array).kernel: The convolution kernel (2D list).step: Controls downscaling stride (higherstep→ more aggressive downscaling).
- How it works:
- Iterates over the image, skipping pixels based on
step. - For each position, computes a weighted sum of the kernel and the corresponding image region.
- Stores the result in a new downscaled image.
- Iterates over the image, skipping pixels based on
- Python 3.x
- OpenCV (
cv2) - NumPy (
numpy) - Matplotlib (
matplotlib)
Install dependencies:
pip install opencv-python numpy matplotlib- Place an image (e.g.,
mao.jpeg) in the same directory. - Run:
import cv2
import numpy as np
import matplotlib.pyplot as plt
from math import ceil
# Load image in grayscale
image = cv2.imread("mao.jpeg", cv2.IMREAD_GRAYSCALE)
# Generate kernel and apply convolution
kernel = downscaler_kernel(5) # 5x5 kernel
downscaled_image = convolution(image, kernel, step=5)
# Display results
plt.imshow(downscaled_image, cmap='gray')
plt.show()- The original image is downscaled while preserving edges and structures.
- Higher
stepvalues produce smaller output images.
✅ Manual Convolution → No reliance on cv2.filter2D or scipy.signal.convolve2d.
✅ Custom Kernel → Adjust weights to control blurring/downscaling effects.
✅ Stride Control → The step parameter allows flexible downscaling.