MATLAB: Apply a low-pass filter to an image

8.3k views Asked by At

I am trying to implement a simple low-pass filter using "ones" function as a filter and "conv2" to compute the convolution of both matrices (the original image and the filter), which is the filtered image I want to get, but the result of imshow(filteredImage) is just an empty white image instead of a filtered image.

I have checked the matrice of the filtered image, it is a 256x256 double, but I don't know the reason why it isn't displayed properly.

I = imread('cameraman.tif');

filteredImage = conv2(double(I), double(ones(3,3)), 'same');

figure; subplot(1,2,1); imshow(filteredImage);title('filtered');
    subplot(1,2,2); imshow(I); title('original');

EDIT: I have also tried converting it to double first before calculating the convolution as it was exceeding 1, but it didn't give a low-pass filter effect, but the image's contrast got increased instead.

I = imread('cameraman.tif');
I1 = im2double(I);
filteredImage = conv2(I1, ones(2,2), 'same');

figure; subplot(1,2,1); imshow(filteredImage);title('filtered');
    subplot(1,2,2); imshow(I1); title('original');
1

There are 1 answers

0
Ouissal On BEST ANSWER

The following solution has fixed the range issue, the other solutions that were given were about a specific type of low-pass filters which is an averaging filte :

Img1 = imread('cameraman.tif');
Im=im2double(Img1);
filteredImage = conv2(Im, ones(3,3));
figure; subplot(1,2,1); imshow(filteredImage, []);title('filtered');
subplot(1,2,2); imshow(Im); title('original');

Instead of dividing by the kernel, I've used imshow(filteredImage, []).