# How to use convolution operator in matlab? [closed]

  I = imread('13.jpg');
Ir = I(:,:,1);
Ig = I(:,:,2);
Ib = I(:,:,3);

%# Create the gaussian filter with hsize = [5 5] and sigma = 2
G = fspecial('gaussian',[10 10],1);
%# Filter it
Ig = imfilter(I,G,'same');
%# Display
imshow(Ig);

PSF = fspecial('gaussian',7,10);

Irc = conv2(Ir,PSF,'same');
Igc = conv2(Ig,PSF,'same');
Ibc = conv2(Ib,PSF,'same');


Why am I getting this error?

       Warning: CONV2 on values of class UINT8 is obsolete.
Use CONV2(DOUBLE(A),DOUBLE(B)) or
In uint8.conv2 at 11
In Gaussian_Filter_image at 19
Warning: CONV2 on values of class UINT8 is obsolete.
Use CONV2(DOUBLE(A),DOUBLE(B)) or

    In uint8.conv2 at 11
In Gaussian_Filter_image at 20
Undefined function 'conv2' for input arguments of type 'double'
and attributes 'full 3d real'.

      Error in uint8/conv2 (line 18)
y = conv2(varargin{:});

Error in Gaussian_Filter_image (line 20)
Igc = conv2(Ig,PSF,'same');


Here I want to perform convolution of image and gaussian filter but I am unable to do that ?

I have even tried the modification after using

   Ir = double(:,:,1);
Ig = double(:,:,2);
Ib = double(:,:,3);


then also I am getting the same error?

• It should be Ir = double(I(:,:,1)); ..., not Ir = double(:,:,1); ... Jan 15, 2013 at 12:04
• This is a programming question (or debugging question) about MATLAB, not a question about signa processing, and should be closed as off-topic. Jan 15, 2013 at 12:38

You are getting the error because image I is of class (data type) 'uint8', and the arguments to conv2 must be of class 'single' or 'double', i. e. floating point. You can get rid of the error by converting I to double. The best way to do that is to call I = im2double(I), which will re-scale the pixel values to be between 0 and 1.
But looking at the big picture, you should not use conv2 at all, and use imfilter instead, which does take 'uint8' images. Look at the help for it. By default it does correlation, but it can also do convolution.