Blurring and smoothing help reduce noise and details in images. This makes images easier to analyze or look nicer.
Blurring and smoothing (Gaussian, median, bilateral) in Computer Vision
Start learning this pattern below
Jump into concepts and practice - no test required
cv2.GaussianBlur(src, ksize, sigmaX) cv2.medianBlur(src, ksize) cv2.bilateralFilter(src, d, sigmaColor, sigmaSpace)
src is the input image.
ksize is the size of the filter window (must be odd number).
d is the diameter of each pixel neighborhood used during filtering (for bilateralFilter).
sigmaColor is filter sigma in the color space (for bilateralFilter).
sigmaSpace is filter sigma in the coordinate space (for bilateralFilter).
blurred = cv2.GaussianBlur(image, (5, 5), 0)
median = cv2.medianBlur(image, 3)bilateral = cv2.bilateralFilter(image, 9, 75, 75)
This code creates a noisy image and applies three types of blurring. It prints the average pixel brightness to show smoothing effects.
import cv2 import numpy as np # Create a noisy image (black with white noise) image = np.random.randint(0, 256, (100, 100), dtype=np.uint8) # Apply Gaussian blur gaussian = cv2.GaussianBlur(image, (5, 5), 0) # Apply Median blur median = cv2.medianBlur(image, 5) # Apply Bilateral filter bilateral = cv2.bilateralFilter(image, 9, 75, 75) # Calculate mean pixel values to compare smoothing effect print(f"Original mean: {np.mean(image):.2f}") print(f"Gaussian mean: {np.mean(gaussian):.2f}") print(f"Median mean: {np.mean(median):.2f}") print(f"Bilateral mean: {np.mean(bilateral):.2f}")
Gaussian blur uses a weighted average giving more importance to nearby pixels.
Median blur replaces each pixel with the median of neighbors, good for salt-and-pepper noise.
Bilateral filter smooths while keeping edges sharp, useful for photos.
Blurring reduces noise and detail in images.
Gaussian, median, and bilateral filters each smooth images differently.
Choose the filter based on the noise type and whether you want to keep edges.
Practice
Solution
Step 1: Understand salt-and-pepper noise characteristics
Salt-and-pepper noise appears as random black and white pixels, which median filters handle well by replacing each pixel with the median of neighbors.Step 2: Compare filters for edge preservation
Median filters remove noise without blurring edges, unlike Gaussian filters which blur edges, and bilateral filters which are more complex but less effective for salt-and-pepper noise.Final Answer:
Median filter -> Option BQuick Check:
Salt-and-pepper noise = Median filter [OK]
- Choosing Gaussian filter which blurs edges
- Confusing bilateral filter with median filter
- Using box filter which blurs noise and edges
img?Solution
Step 1: Identify Gaussian blur function syntax
OpenCV's GaussianBlur requires the image, kernel size as a tuple, and sigma (0 means auto).Step 2: Match options to syntax
cv2.GaussianBlur(img, (5,5), 0) matches the correct function and parameters for Gaussian blur with 5x5 kernel.Final Answer:
cv2.GaussianBlur(img, (5,5), 0) -> Option AQuick Check:
Gaussian blur syntax = cv2.GaussianBlur(img, (5,5), 0) [OK]
- Using medianBlur for Gaussian blur
- Passing kernel size as single integer instead of tuple
- Confusing bilateralFilter parameters
d=9, sigmaColor=75, sigmaSpace=75 on a noisy image?Solution
Step 1: Understand bilateral filter parameters
d controls neighborhood size; sigmaColor controls color similarity; sigmaSpace controls spatial closeness. Together they smooth noise but keep edges sharp.Step 2: Analyze filter effect on noisy image
Bilateral filter smooths noise while preserving edges, unlike Gaussian which blurs edges or median which targets salt-and-pepper noise.Final Answer:
Smooths noise while preserving edges -> Option DQuick Check:
Bilateral filter = noise smoothing + edge preservation [OK]
- Thinking bilateral filter blurs edges
- Confusing bilateral with median filter
- Assuming it only removes salt-and-pepper noise
Solution
Step 1: Understand median filter kernel size effect
Small kernel sizes may not cover enough pixels to remove noise effectively.Step 2: Identify correct kernel size usage
Median filter kernels must be odd-sized and larger kernels remove more noise but may blur details.Final Answer:
Kernel size is too small to remove noise effectively -> Option CQuick Check:
Increase kernel size for better noise removal [OK]
- Using even kernel sizes (invalid)
- Expecting median filter to blur edges heavily
- Thinking median filter can't remove noise
Solution
Step 1: Understand noise types and filter strengths
Gaussian noise is best reduced by Gaussian blur; bilateral filter preserves edges while smoothing remaining noise.Step 2: Determine filter order for best effect
Applying Gaussian blur first reduces Gaussian noise; then bilateral filter smooths while preserving edges, improving overall quality.Final Answer:
Apply Gaussian blur first, then bilateral filter -> Option AQuick Check:
Gaussian blur + bilateral filter = noise reduction + edge preservation [OK]
- Applying median filter first which doesn't target Gaussian noise well
- Using only one filter for mixed noise
- Applying bilateral filter before Gaussian blur
