Clear Filters
Clear Filters

Help with convolution in matlab

37 views (last 30 days)
There are many convolutions in my code and I am looking for a way to reduce the computation of the convolution. Since I am using conv2(a,b,'same'), I was wondering if it is possible to just calculate the center part that I want? Also, 'a' is a 2D matrix while 'b' is a 1D vector so will it be faster if I repmat 'b' to the same length as 'a'?
Looking at the convolution theorem, I do not understand how the 2D convolution works. I guess I understand the idea of sliding and summing them but from the convolution theorem, shouldn't the size of both matrix be the same?

Accepted Answer

Image Analyst
Image Analyst on 28 Feb 2023
Convolution is sliding a matrix ("template" or "kernel") of any size over another matrix of any size. Usually the "sliding" matrix is the smaller one and it can be a 1-D vector or a 2-D matrix. If you want a 1-D vector then just use it because if you replicate it in the other dimension the result will be different because you'd be including different elements in the computation. For example a 1x3 array give the average of 3 elements only in that row while a 3x3 kernel/template gives the average of 9 elements - both in the same row and the row above and below the target row, so they're not the same.
If you're using images, you'll need to cast from integer to double. And to keep the image in the same range you'll want the sum of the sliding elements (template) to be 1.
grayImage = imread('cameraman.tif');
template = ones(9,9); % Box blur / average in a 9x9 window.
template = template / sum(template(:))
template = 9×9
0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123
blurredImage = conv2(double(grayImage), template, 'same'); % Blur the image.
subplot(1, 2, 1);
imshow(grayImage)
subplot(1, 2, 2);
imshow(blurredImage, [])
If you want to filter only a part of the image/matrix you can crop it out, then convolve it, then assign it back in. See attached copy and paste demo.
The conv2 function, or imfilter function, are highly optimized, expecially for common situations (like uniform or symmetric templates). I doubt you would be able to improve on their speed, unless, like you said, you wanted to just filter a very small part of a larger matrix.
If you don't want edge effects, where the sliding template goes off the edge of the image you can use the 'valid' option to use only those parts where the sliding template is completely within the main image. If you use imfilter there are a lot more options for how to handle edge effects than conv2 has.
  12 Comments
Jiayun Liu
Jiayun Liu on 1 Mar 2023
I see. In that case I guess I will just have to try writing and compare the speed.
Thanks
Walter Roberson
Walter Roberson on 1 Mar 2023
I just checked back to R2013a, and conv2() was already built-in then...

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!