Undefined function 'conv2' for input arguments of type 'double' and attributes 'full 3d real'. - matlab

I am trying to filter an image in a spatial domain, so I use the conv2 function.

here is my code.

cd /home/samuelpedro/Desktop/APIProject/ close all clear all clc img = imread('coimbra_aerea.jpg'); %figure, imshow(img); size_img = size(img); gauss = fspecial('gaussian', [size_img(1) size_img(2)], 50); %figure, surf(gauss), shading interp img_double = im2double(img); filter_g = conv2(gauss,img_double); 

I got an error:

 Undefined function 'conv2' for input arguments of type 'double' and attributes 'full 3d real'. Error in test (line 18) filter_g = conv2(gauss,img_double); 

Now I am wondering if I can not use a 3-channel image, i.e. a color image.

+7
source share
3 answers

Color images are 3-dimensional arrays (x, y, color). conv2 defined only for 2-dimensional, so it will not work directly on a three-dimensional array.

Three options:

  • Use n-dimensional convolution, convn()

  • Convert the image to grayscale with rgb2gray() and filter in 2D:

    filter_g = conv2(gauss,rgb2gray(img_double));

  • Filter each color (RGB) separately in 2D:

     filter_g = zeros(size(im_double)); for i = 1:3 filter_g(:,:,i) = conv2(gauss, im_double(:,:,i); end 
+10
source

To enter nD you need to use convn .

+1
source

If you have R2015a or later, the IPT imgaussfilt function handles multi-plane 2nd convolution problems like this, just transfer the RGB image.

http://www.mathworks.com/help/images/ref/imgaussfilt.html

If you do not, imfilter also performs multi-line 2nd convolution.

Both will be faster for the Gauss filter, they both know how to do a tear-off trick for you.

+1
source

All Articles