You want to apply a Gaussian filter to each color channel of an RGB image separately. Which code correctly does this?
hard📝 Application Q9 of 15
SciPy - Image Processing (scipy.ndimage)
You want to apply a Gaussian filter to each color channel of an RGB image separately. Which code correctly does this?
Aresult = gaussian_filter(img, sigma=[2,2,2])
Bresult = gaussian_filter(img, sigma=2)
Cresult = gaussian_filter(img, sigma=2, axis=2)
Dresult = np.stack([gaussian_filter(img[:,:,i], sigma=2) for i in range(3)], axis=2)
Step-by-Step Solution
Solution:
Step 1: Understand filtering per channel
Applying gaussian_filter directly to 3D image blurs across channels, which is usually undesired.
Step 2: Check each option
result = np.stack([gaussian_filter(img[:,:,i], sigma=2) for i in range(3)], axis=2) applies filter separately to each channel and stacks results, preserving channel info. result = gaussian_filter(img, sigma=2) filters all channels together. result = gaussian_filter(img, sigma=[2,2,2]) sets sigma per dimension including channels, blurring across color channels. result = gaussian_filter(img, sigma=2, axis=2) uses non-existent axis parameter.
Final Answer:
result = np.stack([gaussian_filter(img[:,:,i], sigma=2) for i in range(3)], axis=2) -> Option D
Quick Check:
Filter channels separately = C [OK]
Quick Trick:Filter each channel separately, then stack [OK]
Common Mistakes:
Filtering all channels together
Passing sigma as list
Using invalid parameters
Master "Image Processing (scipy.ndimage)" in SciPy
9 interactive learning modes - each teaches the same concept differently