Convolution ('Conv') in MATLAB

In summary, the person is having trouble with convoluting an image with an impulse response and receiving errors. They have tried zero-padding the impulse response, but still get an error. They suspect they are missing something simple and are running the code in the terminal. They later realize that the image needs to be converted to grayscale, but are still unsure how to format it properly. They eventually figure out the problem and consider the issue resolved.
  • #1
mushiman
10
0
I have a problem/error that I believe is very simple, but I am unable to determine what the issue is nonetheless.

I am trying to convolute an image with an impulse response (IE, h = [1 2 3]), but I get incessant errors and I am unsure as to why. My code is:

Code:
A = double(imread('image.jpg'));
B = zeros(514);
h = [1 2 3];

for k = 1:512
    B(k,:) = conv[A(k,:),h];

I get the error "? Subscripted assignment dimension mismatch.", or alternatively, MATLAB returns "? Error using ==> conv, A and B must be vectors." if I attempt to zero-pad my impulse response (h), then insert my matrix values of 1, 2, and 3.

I'm sure that I am missing something very simple here, but I do not know what it is. At the moment, I am running this code in the terminal, although I don't believe that this is the cause of the problem.

Thank you to anyone who helps -- I'm banging my head against the desk.

Edit: Just a quick note... apparently I forgot to convert to grayscale, and for that reason this image was a 3-dimensional matrix, which doesn't fly in this situation. However, I am still unsure as to how to get it formatted properly, as rgb2gray does not give me the output that I was looking for (it converts the image into a bright red color).
 
Last edited:
Physics news on Phys.org
  • #2
I figured out what the problem was; this can be closed/deleted.
 
  • #3



It looks like you have a few issues in your code that could be causing the errors you are experiencing.

Firstly, when using the conv function in MATLAB, the first argument should be the input signal and the second argument should be the filter (impulse response). In your code, you have these reversed, which could be causing the mismatch error.

Secondly, the conv function in MATLAB is designed to work with 1-dimensional signals, not 2-dimensional matrices. To convolve an image with a filter, you will need to use the conv2 function instead. This function takes in two 2-dimensional matrices and performs the convolution operation on them.

Finally, it seems like you are trying to zero-pad your impulse response by creating a 514x514 matrix for B. However, this will not work as the conv function expects the output to be the same size as the input. Instead, you can use the 'same' option in the conv2 function to automatically pad the input signal with zeros and return an output of the same size as the input.

So your code should look something like this:

A = double(imread('image.jpg'));
h = [1 2 3];
B = conv2(A, h, 'same');

I hope this helps you resolve your issue and get your convolution working correctly. Best of luck!
 

What is Convolution in MATLAB?

Convolution in MATLAB is a mathematical operation used to combine two signals or functions to create a third signal. It involves multiplying one signal by a flipped and shifted version of the other signal and then summing the results.

How is Convolution performed in MATLAB?

In MATLAB, convolution is performed using the "conv" function. This function takes two input signals as its arguments and outputs the convolved signal. The syntax is as follows:

y = conv(x1, x2)

where x1 and x2 are the input signals and y is the convolved signal.

What are the applications of Convolution in MATLAB?

Convolution in MATLAB has various applications in signal processing and image processing. It can be used for filtering, noise reduction, feature extraction, and edge detection in images. It is also commonly used in digital signal processing for smoothing, deconvolution, and cross-correlation of signals.

Can Convolution be performed on multidimensional signals in MATLAB?

Yes, convolution can be performed on multidimensional signals in MATLAB. The "conv" function can accept multidimensional arrays as inputs, and the resulting signal will also be a multidimensional array. This allows for convolution to be used in tasks such as image filtering and feature extraction.

Are there any other functions related to Convolution in MATLAB?

Yes, MATLAB has several other functions related to convolution, such as "conv2" for two-dimensional convolution, "convn" for n-dimensional convolution, and "xcorr" for cross-correlation. Additionally, there are functions for specific types of convolution, such as "convmtx" for creating convolution matrices and "convfreq" for calculating the frequency response of a convolution operation.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • Programming and Computer Science
Replies
1
Views
793
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
968
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top