Muna
Muna

Reputation: 73

How to apply a filter in MATLAB?

My question is how do I apply a 5×5 Laplacian filter with 8 at the center in MATLAB?

I try this code but it does not work

kAvg = fspecial('average',[5 5]);
kLap = fspecial('laplacian');

Upvotes: 3

Views: 10293

Answers (1)

zenpoy
zenpoy

Reputation: 20126

According to the documentation, you can use imfilter to apply a filter:

I = imread('cameraman.tif');
kLap = fspecial('laplacian');
filtered = imfilter(I,kLap,'replicate');
imshow(filtered); title('Filtered Image');

EDIT: I just realized what you were asking for:

I = imread('cameraman.tif');

% simple high pass filter
kLap = [-1, -1, -1;
        -1,  8, -1;
        -1, -1, -1];

filtered_3x3 = imfilter(I,kLap,'replicate');
imshow(filtered_3x3); title('Filtered Image (3x3)');
pause();

% another simple high pass filter
kLap = [-1 -3 -4 -3 -1;
        -3  0  6  0 -3;
        -4  6 20  6 -4;
        -3  0  6  0 -3;
        -1 -3 -4 -3 -1];

filtered_5x5 = imfilter(I,kLap,'replicate');
imshow( filtered_5x5 ); title('Filtered Image (5x5)');

Upvotes: 2

Related Questions