📖 Scenario: Imagine you have a photo that looks a bit noisy or sharp. You want to make it look smoother and softer, like when you gently blur a picture to reduce rough edges. This is called image smoothing.
🎯 Goal: You will create a simple program to apply a Gaussian blur to an image. This will help you understand how smoothing works in image processing.
📋 What You'll Learn
Create a 5x5 grayscale image as a 2D list with exact pixel values
Define a Gaussian kernel as a 2D list with exact values
Write code to apply the Gaussian blur by convolving the image with the kernel
Print the resulting blurred image as a 2D list
💡 Why This Matters
🌍 Real World
Image smoothing is used in photography and computer vision to reduce noise and make images look nicer.
💼 Career
Understanding image processing basics is important for jobs in data science, computer vision, and AI that work with pictures and videos.
Progress0 / 4 steps
1
Create the image data
Create a variable called image that is a 5x5 list of lists with these exact grayscale pixel values: [[10, 20, 30, 40, 50], [60, 70, 80, 90, 100], [110, 120, 130, 140, 150], [160, 170, 180, 190, 200], [210, 220, 230, 240, 250]]
Signal Processing
Hint
Use a list of lists to represent the 5 rows and 5 columns of pixels.
2
Define the Gaussian kernel
Create a variable called kernel that is a 3x3 list of lists with these exact values: [[1, 2, 1], [2, 4, 2], [1, 2, 1]]. This kernel will be used to smooth the image.
Signal Processing
Hint
The Gaussian kernel is a small matrix that helps blur the image by averaging nearby pixels with weights.
3
Apply Gaussian blur to the image
Create a new variable called blurred_image as a 5x5 list of lists filled with zeros. Then use nested for loops with variables i and j to go over each pixel of image except the edges. For each pixel, calculate the weighted sum of the pixel and its neighbors using kernel. Divide the sum by 16 and assign the integer result to blurred_image[i][j].
Signal Processing
Hint
Remember to skip the edges because the kernel needs neighbors around each pixel.
4
Print the blurred image
Use a for loop with variable row to print each row of blurred_image.
Signal Processing
Hint
Print each row on its own line to see the blurred image clearly.
Practice
(1/5)
1. What is the main purpose of applying a Gaussian blur to an image?
easy
A. To smooth the image by averaging pixels with a weighted filter
B. To increase the image resolution
C. To convert the image to grayscale
D. To detect edges in the image
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 A
Quick Check:
Gaussian blur = smoothing with weighted average [OK]
Hint: Gaussian blur smooths images by weighted averaging [OK]
Common Mistakes:
Confusing blur with edge detection
Thinking blur increases resolution
Assuming blur changes image color
2. Which of the following is the correct Python syntax to apply a Gaussian blur using OpenCV with kernel size 5x5 and sigma 1.0 on image img?
easy
A. cv2.GaussianBlur(img, (5), 1.0)
B. cv2.gaussian_blur(img, 5, 1.0)
C. cv2.GaussianBlur(img, 5, 1.0)
D. cv2.GaussianBlur(img, (5, 5), 1.0)
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 D
Quick Check:
Correct syntax uses tuple kernel size [OK]
Hint: Kernel size must be a tuple like (5, 5) in OpenCV [OK]
Common Mistakes:
Using kernel size as single integer instead of tuple
Wrong function name capitalization
Passing kernel size without parentheses
3. Given the following Python code using OpenCV, what will be the shape of the output image after applying Gaussian blur?
A. cv2.imshow requires image path, not image array
B. Kernel size should be a tuple, not an integer
C. Sigma value must be an integer, not float
D. cv2.waitKey needs a parameter greater than 1000
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 B
Quick Check:
Kernel size = tuple (width, height) [OK]
Hint: Kernel size must be tuple like (5, 5) in GaussianBlur [OK]
Common Mistakes:
Passing kernel size as single integer
Misunderstanding cv2.imshow input
Incorrect sigma type assumptions
5. You want to reduce noise in a grayscale image but keep edges sharp. Which Gaussian blur parameters should you choose to balance smoothing and edge preservation?
hard
A. Small kernel size (3x3) and low sigma (0.5)
B. Large kernel size (15x15) and high sigma (5.0)
C. Kernel size (7x7) and sigma 0 (automatic calculation)
D. Kernel size (1x1) and sigma 10
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 A