Image smoothing (Gaussian blur) in Signal Processing - Time & Space Complexity
We want to understand how the time to smooth an image changes as the image size grows.
How does the number of operations increase when we apply Gaussian blur to bigger images?
Analyze the time complexity of the following code snippet.
for y in range(height):
for x in range(width):
sum = 0
for ky in range(kernel_size):
for kx in range(kernel_size):
pixel = image[y + ky - offset][x + kx - offset]
weight = kernel[ky][kx]
sum += pixel * weight
output[y][x] = sum
This code applies a Gaussian blur by sliding a kernel over each pixel and computing a weighted sum.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Nested loops over image pixels and kernel elements.
- How many times: Outer loops run once per pixel (height x width), inner loops run once per kernel element (kernel_size²).
As the image gets bigger, the number of pixels grows, so the work grows too. The kernel size stays the same.
| Input Size (n = total pixels) | Approx. Operations |
|---|---|
| 10 x 10 = 100 | 100 x kernel_size² |
| 100 x 100 = 10,000 | 10,000 x kernel_size² |
| 1000 x 1000 = 1,000,000 | 1,000,000 x kernel_size² |
Pattern observation: Operations grow roughly in direct proportion to the number of pixels.
Time Complexity: O(n * k^2)
This means the time to smooth the image grows linearly with the number of pixels and quadratically with the kernel size.
[X] Wrong: "The time grows with the square of the image size because of nested loops."
[OK] Correct: The nested loops over pixels are two loops, but together they cover each pixel once, so total work is proportional to the number of pixels, not its square.
Understanding how image processing scales helps you explain efficiency clearly and shows you can think about real data sizes.
"What if the kernel size also grew with the image size? How would the time complexity change?"
Practice
Solution
Step 1: Understand Gaussian blur function
Gaussian blur smooths images by averaging pixels using a weighted filter based on a Gaussian function.Step 2: Identify the main effect on the image
This process reduces noise and smooths details, not increasing resolution or changing color or detecting edges.Final Answer:
To smooth the image by averaging pixels with a weighted filter -> Option AQuick Check:
Gaussian blur = smoothing with weighted average [OK]
- Confusing blur with edge detection
- Thinking blur increases resolution
- Assuming blur changes image color
img?Solution
Step 1: Recall OpenCV GaussianBlur syntax
The function requires the image, kernel size as a tuple (width, height), and sigma value.Step 2: Check each option's syntax
cv2.GaussianBlur(img, (5, 5), 1.0) uses correct function name and parameters:cv2.GaussianBlur(img, (5, 5), 1.0). Others have wrong function names or kernel size format.Final Answer:
cv2.GaussianBlur(img, (5, 5), 1.0) -> Option DQuick Check:
Correct syntax uses tuple kernel size [OK]
- Using kernel size as single integer instead of tuple
- Wrong function name capitalization
- Passing kernel size without parentheses
import cv2
img = cv2.imread('photo.jpg')
blurred = cv2.GaussianBlur(img, (7, 7), 2)
print(blurred.shape)Solution
Step 1: Understand GaussianBlur effect on image size
Gaussian blur smooths the image but does not change its dimensions.Step 2: Check output shape behavior
The output image has the same height, width, and channels as the input image.Final Answer:
Same as original image shape -> Option CQuick Check:
Gaussian blur keeps image size unchanged [OK]
- Assuming blur crops or resizes image
- Expecting output to be a single value
- Confusing blur with image scaling
import cv2
img = cv2.imread('image.png')
blur = cv2.GaussianBlur(img, 5, 1.5)
cv2.imshow('Blurred', blur)
cv2.waitKey(0)Solution
Step 1: Check GaussianBlur parameters
The kernel size must be a tuple like (5, 5), not a single integer.Step 2: Verify other function calls
Sigma can be float, cv2.imshow accepts image arrays, and cv2.waitKey(0) is valid for waiting indefinitely.Final Answer:
Kernel size should be a tuple, not an integer -> Option BQuick Check:
Kernel size = tuple (width, height) [OK]
- Passing kernel size as single integer
- Misunderstanding cv2.imshow input
- Incorrect sigma type assumptions
Solution
Step 1: Understand effect of kernel size and sigma
Small kernel and low sigma cause mild smoothing, preserving edges better.Step 2: Analyze options for noise reduction and edge preservation
Large kernel and high sigma cause heavy blur, losing edges. Sigma 0 lets OpenCV choose, which may be too strong. Kernel 1x1 means no blur.Final Answer:
Small kernel size (3x3) and low sigma (0.5) -> Option AQuick Check:
Small kernel + low sigma = smooth noise, keep edges [OK]
- Choosing too large kernel causing edge loss
- Using sigma 0 without understanding automatic effect
- Using kernel 1x1 which applies no blur
Ready to prove it?
Pass the test and this pattern moves unknown
