Bird
Raised Fist0
MATLABdata~10 mins

Image filtering in MATLAB - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Image filtering
Load Image
↓
Define Filter Kernel
↓
Apply Filter (Convolution)
↓
Display Filtered Image
↓
End
The process starts by loading an image, then defining a filter kernel, applying the filter using convolution, and finally displaying the filtered image.
Execution Sample
MATLAB
img = imread('peppers.png');
filter = fspecial('average', [3 3]);
filtered_img = imfilter(img, filter);
imshow(filtered_img);
This code loads an image, applies a 3x3 average filter to blur it, and shows the result.
Execution Table
StepActionVariable/FunctionResult/Value
1Load image from fileimg = imread('peppers.png')img is a 3D matrix with image pixel data
2Create averaging filter kernelfilter = fspecial('average', [3 3])filter is a 3x3 matrix with all values 1/9
3Apply filter to imagefiltered_img = imfilter(img, filter)filtered_img is blurred version of img
4Display filtered imageimshow(filtered_img)Window shows blurred image
5End of script--
💡 All steps completed, filtered image displayed
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
imgundefinedLoaded image matrixLoaded image matrixLoaded image matrixLoaded image matrix
filterundefinedundefined3x3 averaging kernel3x3 averaging kernel3x3 averaging kernel
filtered_imgundefinedundefinedundefinedBlurred image matrixBlurred image matrix
Key Moments - 3 Insights
Why does the filtered image look blurry compared to the original?
Because the filter used is an averaging filter (see Step 2 and 3 in execution_table), it replaces each pixel with the average of its neighbors, smoothing details and causing blur.
What does the fspecial('average', [3 3]) function create?
It creates a 3x3 matrix where each element is 1/9, which is used to average the pixel values in a 3x3 neighborhood (see Step 2 in execution_table).
Why do we use imfilter instead of just multiplying the image by the filter?
imfilter performs convolution, sliding the filter over the image and combining pixel neighborhoods properly, unlike simple multiplication which doesn't consider neighbors (see Step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'filter' after Step 2?
AA 3x3 matrix with random values
BA 3x3 matrix with all elements equal to 1/9
CThe original image matrix
DUndefined
💡 Hint
Check the 'Result/Value' column for Step 2 in execution_table
At which step is the image actually blurred?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Action' and 'Result/Value' columns in execution_table for Step 3
If we change the filter size from [3 3] to [5 5], how would the variable 'filter' change?
AIt becomes a 5x5 matrix with random values
BIt stays a 3x3 matrix with elements 1/9
CIt becomes a 5x5 matrix with all elements equal to 1/25
DIt becomes undefined
💡 Hint
Refer to how fspecial('average', [3 3]) creates the filter in Step 2 and generalize to [5 5]
Concept Snapshot
Image filtering in MATLAB:
- Load image with imread
- Create filter kernel with fspecial
- Apply filter using imfilter (convolution)
- Display result with imshow
Filtering smooths or sharpens images by combining pixel neighborhoods.
Full Transcript
This example shows how MATLAB applies image filtering step-by-step. First, the image is loaded into a matrix. Then, a filter kernel is created using fspecial, here an averaging filter that smooths the image. Next, imfilter applies this kernel to the image by sliding it over every pixel and averaging neighbors, producing a blurred image. Finally, imshow displays the filtered image. Variables img, filter, and filtered_img change as the script runs, showing the image data and filter matrix. Key points include understanding the filter kernel values and why convolution is used instead of simple multiplication. Changing the filter size changes the kernel matrix size and values accordingly.

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