Image Filtering

What is Image Filtering?

Image filtering applies a mathematical operation to each pixel based on its neighborhood.

Purpose:

  • Noise reduction (smoothing)
  • Edge enhancement (sharpening)
  • Feature detection (gradients, edges)
  • Preprocessing for other algorithms

Key concept: Output pixel = function of input pixel + neighbors

Types of Filters

Smoothing (Low-pass) Filters:

  • Blur/Average filter: Simple averaging
  • Gaussian filter: Weighted averaging
  • Median filter: Replaces with median value

Sharpening (High-pass) Filters:

  • Enhances edges and details
  • Opposite of smoothing

Edge Detection Filters:

  • Sobel, Prewitt, Scharr
  • Detect boundaries and gradients

Averaging (Box) Filter

Formula: Each output pixel = average of neighborhood

Effect: Smooths image, reduces noise, but also blurs edges

Trade-off: Larger kernel = more smoothing, more blur

Blur Filter in OpenCV

  1. import cv2
  2. import numpy as np
  3. # simple averaging blur
  4. blurred = cv2.blur(img, (5, 5))
  5. #ans: 5x5 kernel, averages 25 pixels
  6. # what does kernel size mean?
  7. # (5, 5) means 5 rows x 5 columns
  8. #ans: larger kernel = more blur
  9. # blur with different kernel sizes
  10. blur3 = cv2.blur(img, (3, 3))
  11. blur7 = cv2.blur(img, (7, 7))
  12. #ans: blur3 < blur7 (7x7 blurs more)

Gaussian Blur

Gaussian kernel: Weights center pixel more than edges

Advantages over box filter:

  • More natural blur (mimics optical blur)
  • Preserves edges better
  • Reduced ringing artifacts

Gaussian Blur in OpenCV

  1. # gaussian blur
  2. gaussian = cv2.GaussianBlur(img, (5, 5), 0)
  3. #ans: 5x5 kernel, sigma=0 (auto-calculated)
  4. # what is sigma?
  5. #ans: standard deviation, controls blur strength
  6. #ans: larger sigma = more blur
  7. # gaussian with specific sigma
  8. gaussian = cv2.GaussianBlur(img, (5, 5), sigmaX=2.0)
  9. #ans: sigma=2.0 controls spread of gaussian
  10. # kernel size must be odd
  11. gaussian = cv2.GaussianBlur(img, (4, 4), 0)
  12. #ans: ERROR! kernel size must be odd (3, 5, 7, etc.)

Median Filter

Median filter: Replaces pixel with median of neighborhood

Algorithm:

  1. Sort all pixels in neighborhood
  2. Pick middle value (median)
  3. Replace center pixel

Advantage: Excellent for salt-and-pepper noise (removes outliers)
Disadvantage: Slower than linear filters

Median Filter in OpenCV

  1. # median blur (good for salt-pepper noise)
  2. median = cv2.medianBlur(img, 5)
  3. #ans: 5x5 kernel, takes median of 25 pixels
  4. # kernel size must be odd and single value
  5. median = cv2.medianBlur(img, 7)
  6. #ans: 7x7 kernel
  7. # why median for noise?
  8. #ans: outliers (noise) don't affect median
  9. #ans: preserves edges while removing noise

Bilateral Filter

Bilateral filter: Smooths while preserving edges

How it works:

  • Considers both spatial distance AND intensity difference
  • Pixels far away or very different intensity get less weight

Formula: Weight = spatial_weight × intensity_weight

Use case: Noise reduction while keeping sharp edges (e.g., portrait smoothing)

Bilateral Filter in OpenCV

  1. # bilateral filter (edge-preserving)
  2. bilateral = cv2.bilateralFilter(img, 9, 75, 75)
  3. #ans: d=9 (diameter), sigmaColor=75, sigmaSpace=75
  4. # what is d?
  5. #ans: diameter of pixel neighborhood
  6. # what is sigmaColor?
  7. #ans: filter sigma in color space (intensity difference)
  8. # what is sigmaSpace?
  9. #ans: filter sigma in coordinate space (spatial distance)
  10. # when to use bilateral?
  11. #ans: noise reduction with edge preservation

Exercises - Part 1 (Concepts)

  1. # what does smoothing do?
  2. #ans: reduces noise, blurs image, averages neighborhood
  3. # difference between blur and Gaussian blur?
  4. #ans: Gaussian weights center more, more natural
  5. # what filter for salt-pepper noise?
  6. #ans: median filter (removes outliers)
  7. # why bilateral filter special?
  8. #ans: preserves edges while smoothing
  9. # what is kernel size?
  10. #ans: neighborhood size (e.g., 3x3, 5x5)

Exercises - Part 2 (Concepts)

  1. # larger kernel means?
  2. #ans: more smoothing, more blur, slower processing
  3. # why must kernel size be odd?
  4. #ans: needs center pixel (symmetric neighborhood)
  5. # what is sigma in Gaussian?
  6. #ans: standard deviation, controls blur strength
  7. # trade-off of smoothing?
  8. #ans: reduces noise but also blurs edges/details

Exercises - Part 3 (Coding)

  1. # apply 3x3 averaging blur
  2. #ans: blurred = cv2.blur(img, (3, 3))
  3. # apply 7x7 gaussian blur
  4. #ans: gaussian = cv2.GaussianBlur(img, (7, 7), 0)
  5. # apply median filter with kernel 5
  6. #ans: median = cv2.medianBlur(img, 5)
  7. # apply bilateral filter
  8. #ans: bilateral = cv2.bilateralFilter(img, 9, 75, 75)

Exercises - Part 4 (Coding)

  1. # gaussian blur with sigma=3
  2. #ans: gaussian = cv2.GaussianBlur(img, (0, 0), sigmaX=3)
  3. #ans: kernel size auto-calculated from sigma
  4. # compare blur strengths
  5. blur_small = cv2.blur(img, (3, 3))
  6. blur_large = cv2.blur(img, (9, 9))
  7. #ans: blur_large is much more blurred
  8. # apply median blur on grayscale
  9. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  10. #ans: median = cv2.medianBlur(gray, 5)

Exercises - Part 5 (Mixed)

  1. # what happens with even kernel size?
  2. blurred = cv2.blur(img, (4, 4))
  3. #ans: works for blur, but not for GaussianBlur/medianBlur
  4. # which is faster: median or gaussian?
  5. #ans: gaussian (linear filter) faster than median
  6. # bilateral with large sigmaColor?
  7. bilateral = cv2.bilateralFilter(img, 9, 150, 75)
  8. #ans: more aggressive smoothing of similar colors
  9. # bilateral with large sigmaSpace?
  10. bilateral = cv2.bilateralFilter(img, 9, 75, 150)
  11. #ans: considers pixels farther away spatially

Exercises - Part 6 (Mixed)

  1. # multiple blurs in sequence
  2. blur1 = cv2.GaussianBlur(img, (5, 5), 0)
  3. blur2 = cv2.GaussianBlur(blur1, (5, 5), 0)
  4. #ans: double blur = more smoothing
  5. # can you blur already blurred image?
  6. #ans: yes, accumulates blur effect
  7. # blur only one channel
  8. img[:, :, 0] = cv2.blur(img[:, :, 0], (5, 5))
  9. #ans: blurs blue channel only

Google tag (gtag.js)