필터 지우기
필터 지우기

Help with convolution in matlab

조회 수: 38 (최근 30일)
Jiayun Liu
Jiayun Liu 2023년 2월 28일
댓글: Walter Roberson 2023년 3월 1일
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?

채택된 답변

Image Analyst
Image Analyst 2023년 2월 28일
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
Jiayun Liu
Jiayun Liu 2023년 3월 1일
I see. In that case I guess I will just have to try writing and compare the speed.
Thanks
Walter Roberson
Walter Roberson 2023년 3월 1일
I just checked back to R2013a, and conv2() was already built-in then...

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Geometric Transformation and Image Registration에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by