Bird
Raised Fist0
SciPydata~20 mins

Image filtering (gaussian_filter) in SciPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Gaussian Filter Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Gaussian filter on a simple 2D array
What is the output of applying gaussian_filter with sigma=1 to the following 2D array?
SciPy
import numpy as np
from scipy.ndimage import gaussian_filter

arr = np.array([[0, 0, 0], [0, 10, 0], [0, 0, 0]])
result = gaussian_filter(arr, sigma=1)
print(result.round(2))
A
[[0.46 0.91 0.46]
 [0.91 1.81 0.91]
 [0.46 0.91 0.46]]
B
[[0.00 0.00 0.00]
 [0.00 10.00 0.00]
 [0.00 0.00 0.00]]
C
[[1.00 1.00 1.00]
 [1.00 10.00 1.00]
 [1.00 1.00 1.00]]
D
[[0.10 0.20 0.10]
 [0.20 2.00 0.20]
 [0.10 0.20 0.10]]
Attempts:
2 left
💡 Hint
Think about how Gaussian smoothing spreads the value around neighbors.
🧠 Conceptual
intermediate
1:30remaining
Effect of increasing sigma in gaussian_filter
What happens to the output image when you increase the sigma parameter in gaussian_filter?
AThe image colors invert as sigma increases.
BThe image becomes sharper as sigma increases.
CThe image size increases as sigma increases.
DThe image becomes more blurred as sigma increases.
Attempts:
2 left
💡 Hint
Think about what smoothing means for an image.
🔧 Debug
advanced
1:30remaining
Identify the error in gaussian_filter usage
What error will this code raise when run?
SciPy
from scipy.ndimage import gaussian_filter

arr = [[1, 2], [3, 4]]
result = gaussian_filter(arr, sigma='two')
print(result)
ATypeError: sigma must be a number or sequence of numbers
BValueError: sigma cannot be a string
CSyntaxError: invalid syntax
DNo error, runs successfully
Attempts:
2 left
💡 Hint
Check the type expected for sigma parameter.
data_output
advanced
1:30remaining
Number of unique values after gaussian_filter
Given this 1D array, how many unique values does the output have after applying gaussian_filter with sigma=0.5?
SciPy
import numpy as np
from scipy.ndimage import gaussian_filter

arr = np.array([0, 0, 10, 0, 0])
result = gaussian_filter(arr, sigma=0.5)
unique_count = len(np.unique(result))
print(unique_count)
A1
B2
C3
D5
Attempts:
2 left
💡 Hint
Consider the symmetry of the array: edge positions are equal, next-to-center equal, center different.
🚀 Application
expert
2:00remaining
Choosing sigma for edge preservation in image smoothing
You want to smooth an image using gaussian_filter but keep edges sharp. Which sigma value strategy is best?
AUse a very large sigma to blur edges heavily.
BUse a very small sigma (close to 0) to minimize smoothing.
CUse sigma=0 to disable filtering.
DUse sigma=5 to smooth only edges.
Attempts:
2 left
💡 Hint
Less smoothing means edges stay sharper.

Practice

(1/5)
1. What is the main purpose of using gaussian_filter in image processing?
easy
A. To detect edges in the image
B. To smooth the image by reducing noise
C. To convert the image to grayscale
D. To increase the image resolution

Solution

  1. Step 1: Understand the function's role

    gaussian_filter applies a blur effect that smooths the image by averaging nearby pixels weighted by a Gaussian curve.
  2. Step 2: Identify the effect on image quality

    This smoothing reduces noise and small details, making the image less sharp but cleaner.
  3. Final Answer:

    To smooth the image by reducing noise -> Option B
  4. Quick Check:

    Gaussian blur = noise reduction [OK]
Hint: Gaussian filter smooths images by blurring noise away [OK]
Common Mistakes:
  • Thinking it increases resolution
  • Confusing it with edge detection
  • Assuming it changes color format
2. Which of the following is the correct way to import and apply a Gaussian filter with sigma=2 to a 2D numpy array named image?
easy
A. from scipy.ndimage import gaussian_filter filtered = gaussian_filter(image, sigma=2)
B. import scipy filtered = scipy.gaussian_filter(image, 2)
C. from scipy import gaussian_filter filtered = gaussian_filter(image, 2)
D. import gaussian_filter from scipy.ndimage filtered = gaussian_filter(image, sigma=2)

Solution

  1. Step 1: Check the correct import statement

    The Gaussian filter is in scipy.ndimage, so import it with from scipy.ndimage import gaussian_filter.
  2. Step 2: Verify function usage

    Apply it by calling gaussian_filter(image, sigma=2) to blur with sigma 2.
  3. Final Answer:

    from scipy.ndimage import gaussian_filter filtered = gaussian_filter(image, sigma=2) -> Option A
  4. Quick Check:

    Correct import and sigma usage = from scipy.ndimage import gaussian_filter filtered = gaussian_filter(image, sigma=2) [OK]
Hint: Import from scipy.ndimage, use sigma as keyword [OK]
Common Mistakes:
  • Wrong import path for gaussian_filter
  • Passing sigma as positional without keyword
  • Using incorrect import syntax
3. Given the code below, what will be the output array after applying the Gaussian filter?
import numpy as np
from scipy.ndimage import gaussian_filter

image = np.array([[0, 0, 0],
                  [0, 10, 0],
                  [0, 0, 0]])
filtered = gaussian_filter(image, sigma=1)
print(np.round(filtered, 2))
medium
A. [[0.46 1.23 0.46] [1.23 2.99 1.23] [0.46 1.23 0.46]]
B. [[0 0 0] [0 10 0] [0 0 0]]
C. [[2.5 2.5 2.5] [2.5 2.5 2.5] [2.5 2.5 2.5]]
D. [[0.1 0.1 0.1] [0.1 10 0.1] [0.1 0.1 0.1]]

Solution

  1. Step 1: Understand Gaussian filter effect on sparse image

    The single bright pixel (10) will blur into neighbors, spreading intensity smoothly.
  2. Step 2: Calculate approximate blurred values

    Using sigma=1, the center pixel reduces from 10 to about 3, neighbors get values around 1.2 to 0.46.
  3. Final Answer:

    [[0.46 1.23 0.46] [1.23 2.99 1.23] [0.46 1.23 0.46]] -> Option A
  4. Quick Check:

    Blur spreads intensity smoothly = [[0.46 1.23 0.46] [1.23 2.99 1.23] [0.46 1.23 0.46]] [OK]
Hint: Gaussian blur spreads bright pixels softly to neighbors [OK]
Common Mistakes:
  • Expecting no change in array
  • Assuming uniform average instead of weighted blur
  • Misreading sigma effect as sharpening
4. Identify the error in the following code snippet that applies a Gaussian filter:
import numpy as np
from scipy.ndimage import gaussian_filter

image = np.ones((5,5))
filtered = gaussian_filter(image, sigma='2')
print(filtered)
medium
A. gaussian_filter cannot be applied to numpy arrays
B. The array shape is invalid for gaussian_filter
C. Missing import for numpy
D. sigma should be a number, not a string

Solution

  1. Step 1: Check parameter types

    The sigma parameter must be a numeric value (int or float), not a string.
  2. Step 2: Identify the cause of error

    Passing '2' as a string causes a type error when the filter tries to compute the blur.
  3. Final Answer:

    sigma should be a number, not a string -> Option D
  4. Quick Check:

    Numeric sigma required, string causes error [OK]
Hint: Use numeric sigma, not string, to avoid errors [OK]
Common Mistakes:
  • Passing sigma as string
  • Assuming gaussian_filter needs special array types
  • Ignoring import errors
5. You have a noisy grayscale image stored as a 2D numpy array. You want to smooth the image but keep edges relatively sharp. Which approach using gaussian_filter and its parameters is best?
hard
A. Apply gaussian_filter multiple times with sigma=3
B. Use a large sigma value like 5 to blur the image heavily
C. Use a small sigma value like 0.5 to reduce noise but preserve edges
D. Do not use gaussian_filter; use median filter instead

Solution

  1. Step 1: Understand sigma effect on smoothing

    Small sigma values cause light blur, preserving edges better than large sigma which blurs heavily.
  2. Step 2: Choose best sigma for noise reduction and edge preservation

    Using sigma=0.5 smooths noise but keeps edges sharper than larger sigma values or repeated blurring.
  3. Final Answer:

    Use a small sigma value like 0.5 to reduce noise but preserve edges -> Option C
  4. Quick Check:

    Small sigma = smooth noise + keep edges [OK]
Hint: Small sigma blurs less, preserving edges better [OK]
Common Mistakes:
  • Using large sigma blurs edges too much
  • Applying filter multiple times causes over-blur
  • Confusing gaussian_filter with median filter