user366312
user366312

Reputation: 16896

2D convolution in Matlab/Octave

I = imread ("lena.jpg");
%imshow(I);
K = I;
C = conv2(I, K);
imshow(C);

I am expecting something like the following as indicated in this link.

enter image description here

But, my octave output is blank:

enter image description here

What could be the possible reason?

And, how can I obtain the expected output?

Upvotes: 0

Views: 1442

Answers (1)

obchardon
obchardon

Reputation: 10792

imshow() expect values between [0-255]. After your convolution all your value are way above 255. So of course when you use imshow(C), matlab/octave do a type conversion using uint8(). All your value equal 255 and the result is a white image. (0 = black, 255 = white).

You also should take into account severals things:

  1. add the option 'same' to your convolution to preserve the original size of your image: conv2(I,K,'same')

  2. If you only apply the convolution like that, you will obtain a strong border effect, because the central values of your image will be multiplied more time than the values in the border of your image. You should add a compensation matrix:

    border_compensation = conv2(ones(size(K)),ones(size(K)),'same') C = conv2(I,K,'same')./border_compensation

  3. Normalize the final result (Don't take into account the point 2. if you really want the kind of output that you pointed out in your question)

    C = uint8(C/max(C(:))*255)

Upvotes: 3

Related Questions