Carven
Carven

Reputation: 15648

Why does conv2() returns a white image instead of a filtered image?

In Matlab, I created a 5x5 gaussian kernel with fspecial(). I assigned the kernel to a variable called h. I read in an image through imread() and assigned the image to a variable called Im.

The image has some random noise on it and my intention is to see how I can remove the noise. Now, I want to convolute the image Im with the kernel h. I tried to use the function conv2() this way: conv2(Im, h);

But it turns out that I get an empty white picture when I do a imshow(). I expected the result to be a blurred version of the image Im after convoluting with the kernel h.

This is what I did:

>> Im = imread('image.jpg');
>> h = fspecial('gaussian', 5, 1.0);
>> C1 = conv2(Im, h);

I tried the same process with other pictures and I get an empty white picture when I do imshow() too. What have I done wrong?

Upvotes: 1

Views: 4307

Answers (2)

Dhyanesh T
Dhyanesh T

Reputation: 1

The white image is because you have not done normalization. After you conv2 your image with C1 = conv2(Im, h), in maplab if you check the C1 variable you will find the values are very high.

To normalize your image divide the image by 255 and perform imshow. imshow(c1/255);

Upvotes: 0

Shai
Shai

Reputation: 114796

It seems like you are working on a uint8 type image. In this case the filtering might saturate the pixels' values and cause artifacts. Try:

Im = im2double( imread( 'image.jpg' ) );
h = fspecial( 'gaussian', 5, 1.0 );
C1 = imfilter( Im, h );
figure; imshow( C1, [] ); title( 'filtered image' );

PS
I'm not sure about it, but I think that when reading Im as uint8 you have values in range [0..255], after conv2 you have double values in roughly the same range. However, image saturates pixels (for double images) at 1 (not 255), and this is the reason for the totally white image you see.

Upvotes: 4

Related Questions