Basic Image Processing Operations
2025-08-08
Overview
Basic image processing operations are the essential tools for manipulating and enhancing digital images. These operations allow us to:
- Reduce noise
- Enhance edges or contrast
- Highlight features
- Prepare images for higher-level vision tasks such as object detection, segmentation, or feature extraction.
We’ll explore the following core operations with a mathematical and implementation-oriented perspective:
- Image Filtering
- Edge Detection
- Histogram Operations (Equalization and Enhancement)
1 Image Filtering (Linear and Non-linear)
What Is Image Filtering?
Filtering is the process of modifying the intensity of each pixel based on its neighbors. It's achieved by convolving the image with a kernel (also called a filter or mask).
Convolution Operation
Let I(x,y) be the input image, and K(i,j) be the kernel (of size m×n). The filtered image I′(x,y) is computed as:
I′(x,y)=i=−k∑kj=−l∑lK(i,j)⋅I(x+i,y+j)
- k=⌊2m⌋, l=⌊2n⌋
- This is a 2D discrete convolution (though in practice OpenCV often uses correlation).
Common Linear Filters
Filter | Kernel | Effect |
---|---|---|
Mean Filter (Box) | All ones, normalized | Blurs image uniformly |
Gaussian Filter | Weighted by 2D Gaussian | Smooths image while preserving edges slightly |
Laplacian | Second derivative | Detects rapid intensity changes (edges) |
Sobel / Prewitt | First derivative | Highlights edges in horizontal/vertical directions |
Gaussian Kernel (2D):
G(x,y)=2πσ21⋅e−2σ2x2+y2
- Used for Gaussian smoothing
- Parameter σ controls the degree of blurring
Non-linear Filters
Median Filter: Replaces each pixel with the median of its neighborhood
- Excellent for removing salt-and-pepper noise
- Not a convolution
Implementation Examples (OpenCV)
# Mean filter
blurred = cv2.blur(img, (5, 5))
# Gaussian filter
gauss = cv2.GaussianBlur(img, (5, 5), sigmaX=1.0)
# Median filter
median = cv2.medianBlur(img, 5)
2 Edge Detection
Edge detection identifies points in the image where intensity changes sharply, corresponding to object boundaries or texture changes.
Gradient-Based Methods
We approximate image gradients by convolving with derivative kernels.
First-Order Derivatives (Sobel Operator):
- Horizontal Gradient Gx:
Gx=−1−2−1000+1+2+1
- Vertical Gradient Gy:
Gy=+10−1+20−2+10−1
- Gradient Magnitude:
G=Gx2+Gy2
- Direction:
θ=arctan(GxGy)
Canny Edge Detection (Multi-stage)
A more robust and widely used edge detector. Steps:
- Gaussian smoothing
- Gradient computation (Sobel)
- Non-maximum suppression
- Double thresholding (hysteresis)
Parameters:
- Low and high threshold for edge strength
- Gaussian kernel size
edges = cv2.Canny(image, threshold1=100, threshold2=200)
3 Histogram Analysis and Equalization
What Is a Histogram?
An image histogram plots the frequency of each intensity level (usually 0–255 for uint8
images).
- For grayscale images: simple 1D histogram of intensity.
- For color images: compute separate histograms for each channel.
Histogram Equalization
Enhances the contrast of an image by spreading out the intensity distribution.
Let h(i) be the histogram, p(i)=Nh(i) the probability of intensity i, and the cumulative distribution function (CDF) be:
CDF(i)=j=0∑ip(j)
Then, the equalized value i′ is:
i′=round(255⋅CDF(i))
This mapping redistributes pixel values so that the histogram becomes approximately uniform.
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
equalized = cv2.equalizeHist(gray)
Adaptive Histogram Equalization (CLAHE)
- CLAHE = Contrast Limited Adaptive Histogram Equalization
- Divide the image into tiles, and apply equalization on each local tile (e.g., 8x8)
- Prevents overamplification of noise by histogram clipping
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
enhanced = clahe.apply(gray)
Summary Table
Operation | Math Principle | Common Use |
---|---|---|
Mean / Gaussian Blur | Convolution with smoothing kernel | Denoising, pre-processing |
Median Filter | Non-linear local sorting | Removing salt-and-pepper noise |
Sobel / Prewitt | First derivative (gradient) | Edge detection |
Laplacian | Second derivative | Edge enhancement |
Canny Edge | Multi-step gradient & thresholding | High-quality edge map |
Histogram Equalization | CDF-based mapping | Global contrast enhancement |
CLAHE | Local histogram equalization | Local contrast enhancement |
Suggested Exercises
- Manually implement a 3×3 mean filter using NumPy and compare with
cv2.blur
. - Plot the histogram of a grayscale image before and after equalization.
- Try Canny edge detection with different thresholds. Observe how edge maps change.
- Visualize Sobel gradients (Gx, Gy, and magnitude) using matplotlib.