Bird
Raised Fist0
MATLABdata~20 mins

Image filtering in MATLAB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Image Filtering Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of 3x3 averaging filter on a grayscale image
What is the output matrix after applying the given 3x3 averaging filter to the input image matrix?
MATLAB
input_img = [10 20 30; 40 50 60; 70 80 90];
filter = ones(3,3) / 9;
output_img = conv2(input_img, filter, 'valid');
disp(output_img);
A[30 40 50; 60 70 80; 90 100 110]
B[50]
C[50 60; 80 90]
D[10 20; 40 50]
Attempts:
2 left
💡 Hint
Remember that 'valid' convolution returns only positions where the filter fully overlaps the image.
🧠 Conceptual
intermediate
1:30remaining
Effect of zero-padding in image filtering
What is the main effect of using zero-padding when applying a filter to an image in MATLAB?
AIt keeps the output image size the same as the input and preserves edge information.
BIt decreases the output image size and removes noise.
CIt changes the filter coefficients automatically.
DIt converts the image to grayscale before filtering.
Attempts:
2 left
💡 Hint
Think about how padding affects the borders of the image during filtering.
🔧 Debug
advanced
2:00remaining
Identify the error in median filtering code
What error will this MATLAB code produce when applying a median filter to a grayscale image?
MATLAB
img = imread('cameraman.tif');
filtered_img = medfilt2(img, [3 3 3]);
imshow(filtered_img);
AError: SIZE must be a two-element vector.
BError: Image file not found.
CError: Unsupported image format.
DNo error, code runs correctly.
Attempts:
2 left
💡 Hint
Check the number of dimensions specified in the filter size argument.
📝 Syntax
advanced
1:30remaining
Syntax error in applying Gaussian filter
Which option contains the correct syntax to apply a Gaussian filter with standard deviation 2 to an image 'img' using MATLAB's imgaussfilt function?
Afiltered_img = imgaussfilt(img, sigma=2);
Bfiltered_img = imgaussfilt(img, 2, 'FilterSize');
Cfiltered_img = imgaussfilt(img, 'sigma', 2);
Dfiltered_img = imgaussfilt(img, 2);
Attempts:
2 left
💡 Hint
Check the function signature and how parameters are passed in MATLAB.
🚀 Application
expert
3:00remaining
Number of pixels changed after edge detection filter
Given a 5x5 grayscale image matrix with all values 100 except the center pixel which is 0, how many pixels will have a non-zero value after applying the following edge detection filter? filter = [-1 -1 -1; -1 8 -1; -1 -1 -1]; output_img = conv2(img, filter, 'same');
MATLAB
img = 100*ones(5,5);
img(3,3) = 0;
filter = [-1 -1 -1; -1 8 -1; -1 -1 -1];
output_img = conv2(img, filter, 'same');
nonzero_count = nnz(output_img);
A0
B1
C9
D25
Attempts:
2 left
💡 Hint
Consider how the filter highlights differences around the center pixel.

Practice

(1/5)
1. What is the main purpose of applying an image filter using a kernel in MATLAB?
easy
A. To convert the image to grayscale
B. To modify the image by emphasizing or reducing certain features
C. To increase the image file size
D. To change the image file format

Solution

  1. Step 1: Understand what a kernel does

    A kernel is a small matrix used to process an image by changing pixel values based on neighbors.
  2. Step 2: Identify the effect of filtering

    Filtering modifies the image to highlight edges, blur, or sharpen features, not file format or size.
  3. Final Answer:

    To modify the image by emphasizing or reducing certain features -> Option B
  4. Quick Check:

    Image filtering changes features = D [OK]
Hint: Filters change image features, not file type or size [OK]
Common Mistakes:
  • Confusing filtering with file format conversion
  • Thinking filtering changes image size
  • Assuming filtering only converts to grayscale
2. Which of the following is the correct syntax to apply a filter kernel h to an image img using MATLAB's imfilter function?
easy
A. filtered_img = filter(h, img);
B. filtered_img = filter(img, h);
C. filtered_img = imfilter(h, img);
D. filtered_img = imfilter(img, h);

Solution

  1. Step 1: Recall MATLAB's imfilter syntax

    The correct syntax is imfilter(image, kernel), where the image is first and kernel second.
  2. Step 2: Check each option

    filtered_img = imfilter(img, h); matches the correct syntax. Options B and D use 'filter' which is incorrect. filtered_img = imfilter(h, img); reverses arguments.
  3. Final Answer:

    filtered_img = imfilter(img, h); -> Option D
  4. Quick Check:

    imfilter(image, kernel) = A [OK]
Hint: imfilter(image, kernel) is the correct order [OK]
Common Mistakes:
  • Using 'filter' instead of 'imfilter'
  • Swapping image and kernel arguments
  • Missing semicolon or parentheses
3. Given the following MATLAB code:
img = [1 2 3; 4 5 6; 7 8 9];
h = [1 0 -1; 1 0 -1; 1 0 -1];
result = imfilter(img, h, 'replicate');

What is the value of result(2,2)?
medium
A. 0
B. 3
C. 6
D. 9

Solution

  1. Step 1: Understand the filter and padding

    The kernel h detects vertical edges. 'replicate' pads edges by repeating border values.
  2. Step 2: Calculate convolution at (2,2)

    Neighborhood of (2,2):
    1 2 3
    4 5 6
    7 8 9
    Apply kernel:
    (1*1)+(0*2)+(-1*3)+(1*4)+(0*5)+(-1*6)+(1*7)+(0*8)+(-1*9) = 1+0-3+4+0-6+7+0-9 = (1-3)+(4-6)+(7-9) = -2 -2 -2 = -6
    But imfilter flips kernel, so actual sum is 6.
  3. Final Answer:

    6 -> Option C
  4. Quick Check:

    imfilter result center = 6 [OK]
Hint: Remember imfilter flips kernel before applying [OK]
Common Mistakes:
  • Not flipping the kernel before convolution
  • Ignoring padding method 'replicate'
  • Calculating sum incorrectly
4. Consider this MATLAB code snippet:
img = imread('peppers.png');
h = fspecial('average', 3);
filtered_img = imfilter(img, h, 'same');

What is the error in this code?
medium
A. No error; the code runs correctly
B. imfilter requires grayscale images only
C. The kernel size in fspecial must be odd
D. The 'same' option is invalid for imfilter

Solution

  1. Step 1: Check fspecial usage

    fspecial('average', 3) creates a 3x3 averaging filter, which is valid.
  2. Step 2: Check imfilter usage

    imfilter supports RGB images and 'same' option returns output same size as input.
  3. Final Answer:

    No error; the code runs correctly -> Option A
  4. Quick Check:

    fspecial + imfilter with 'same' = A [OK]
Hint: imfilter works with color images and 'same' option [OK]
Common Mistakes:
  • Assuming imfilter only works on grayscale
  • Thinking 'same' is invalid option
  • Believing kernel size must be odd but 3 is odd and valid
5. You want to sharpen an image img in MATLAB by combining the original image with a filtered version using the Laplacian kernel h = fspecial('laplacian', 0.2);. Which code snippet correctly applies this sharpening?
hard
A. sharpened = img - imfilter(img, h, 'replicate');
B. sharpened = img + imfilter(img, h, 'replicate');
C. sharpened = imfilter(img, h, 'replicate') - img;
D. sharpened = imfilter(img, h, 'replicate') + img * 0.2;

Solution

  1. Step 1: Understand Laplacian sharpening

    Sharpening = original image minus the Laplacian filtered image to enhance edges.
  2. Step 2: Match code to formula

    sharpened = img - imfilter(img, h, 'replicate'); subtracts the filtered image from original, correctly applying sharpening.
  3. Final Answer:

    sharpened = img - imfilter(img, h, 'replicate'); -> Option A
  4. Quick Check:

    Sharpen = img - Laplacian filtered img = B [OK]
Hint: Sharpen by subtracting Laplacian filtered image [OK]
Common Mistakes:
  • Adding instead of subtracting Laplacian result
  • Multiplying original image incorrectly
  • Confusing Laplacian with averaging filter
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard