Image sharpening in Signal Processing - Time & Space Complexity
When sharpening an image, we apply a filter to every pixel to enhance details.
We want to know how the time to sharpen grows as the image size grows.
Analyze the time complexity of the following image sharpening code snippet.
for y in range(height):
for x in range(width):
sum = 0
for ky in range(-1, 2):
for kx in range(-1, 2):
pixel = image[y + ky][x + kx]
weight = kernel[ky + 1][kx + 1]
sum += pixel * weight
output[y][x] = sum
This code applies a 3x3 sharpening kernel to each pixel of the image.
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 9 times per pixel (3x3 kernel).
As the image gets bigger, the number of pixels grows, and so does the work.
| Input Size (pixels) | Approx. Operations |
|---|---|
| 10x10 = 100 | 100 x 9 = 900 |
| 100x100 = 10,000 | 10,000 x 9 = 90,000 |
| 1000x1000 = 1,000,000 | 1,000,000 x 9 = 9,000,000 |
Pattern observation: Operations grow roughly proportional to the number of pixels.
Time Complexity: O(n)
This means the time to sharpen grows linearly with the number of pixels in the image.
[X] Wrong: "The time grows with the square of the image size because of the nested loops."
[OK] Correct: The nested loops over the kernel are fixed size (3x3), so they do not grow with image size. Only the loops over pixels grow.
Understanding how image filters scale helps you explain performance in real projects and shows you can analyze nested loops clearly.
"What if we used a larger kernel, like 5x5? How would the time complexity change?"
Practice
Solution
Step 1: Understand image sharpening
Image sharpening enhances edges and details to make images clearer.Step 2: Compare options
Only To highlight edges and make the image clearer describes highlighting edges and clarity, which is the goal of sharpening.Final Answer:
To highlight edges and make the image clearer -> Option BQuick Check:
Image sharpening = highlight edges [OK]
- Confusing sharpening with blurring
- Thinking sharpening changes image size
- Assuming sharpening converts color formats
Solution
Step 1: Recall OpenCV function for convolution
OpenCV uses cv2.filter2D() to apply kernels for filtering images.Step 2: Check options
Only cv2.filter2D(image, -1, kernel) uses cv2.filter2D with correct parameters; others are invalid functions.Final Answer:
cv2.filter2D(image, -1, kernel) -> Option CQuick Check:
OpenCV convolution = filter2D [OK]
- Using non-existent OpenCV functions
- Confusing convolution with sharpening function names
- Incorrect parameter order
sharpened?
import cv2
import numpy as np
image = cv2.imread('photo.jpg')
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
sharpened = cv2.filter2D(image, -1, kernel)Solution
Step 1: Understand filter2D output
cv2.filter2D returns an image as a NumPy array after applying the kernel.Step 2: Analyze kernel and output
The kernel is a sharpening kernel, so output is sharpened image, still in color and array form.Final Answer:
A sharpened image as a NumPy array -> Option AQuick Check:
filter2D output = NumPy array image [OK]
- Assuming output is blurred instead of sharpened
- Thinking output is grayscale without conversion
- Believing kernel shape causes error
import cv2
import numpy as np
image = cv2.imread('img.png')
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
sharpened = cv2.filter2D(image, 1, kernel)
cv2.imshow('Sharpened', sharpened)
cv2.waitKey(0)
cv2.destroyAllWindows()Solution
Step 1: Check ddepth parameter in filter2D
ddepth=-1 means output image has same depth as input; 1 is invalid and may cause errors.Step 2: Validate other code parts
Kernel shape is valid, cv2.imshow supports color, waitKey(0) is correct usage.Final Answer:
The ddepth parameter should be -1, not 1 -> Option AQuick Check:
ddepth=-1 for same depth [OK]
- Using wrong ddepth value causing errors
- Thinking kernel shape is wrong
- Misunderstanding cv2.imshow capabilities
Solution
Step 1: Understand unsharp masking process
Unsharp masking involves blurring the image, subtracting this blur from original to get edges, then adding edges back.Step 2: Evaluate options
Only Blur the image, subtract the blurred from original, then add result back to original correctly describes this sequence; others miss key steps or add unrelated actions.Final Answer:
Blur the image, subtract the blurred from original, then add result back to original -> Option DQuick Check:
Unsharp masking = blur, subtract, add [OK]
- Skipping the blur step
- Adding noise instead of subtracting blur
- Applying sharpening kernel directly without unsharp steps
Ready to prove it?
Pass the test and this pattern moves unknown
