Is it possible to convolve a stack of 'images' with a stack equal in amount of different kernels, without a for loop?

조회 수: 2 (최근 30일)
The only solutions I found so far is when one uses the same kernel. However, I have a different kernel for each image. Does someone know a smart way to omit a for loop here?
This is how my (super slow) script currently works:
images = randn(5,5,2) % 2 images in z dimension, each 5x5.
kernels = randn(3,3,2) % 2 kernels in z dimension, each 3x3.
% slow for loop method
for ii = size(images,3) % ii loops over images
images(:,:,ii) = conv2(images(:,:,ii),kernels(:,:,ii),'same')
end
Perhaps a fourier transform could do the job? But I am not so experienced with this.
Thanks in advance!

답변 (1개)

Vijeta
Vijeta 2023년 3월 28일
Hi Kevin,
It is possible to convolve a stack of images with a stack of kernels without using a for loop. One way to do this is by using the convn function in MATLAB, which performs N-dimensional convolution.
Here are some links you can refer to:https://www.mathworks.com/help/matlab/ref/conv.html
Thanks.
  댓글 수: 1
Kevin Jansen
Kevin Jansen 2023년 3월 28일
Hi Vijeta,
Thanks for your answer. However, I don't think that is what I am looking for. As far as I know, convn convolves 3D array A with 3D array B, in one go. It does not convolve every slice of array A with every slice of array B, what I would like.
e.g.:
images = randn(5,5,2); % 2 images in z dimension, each 5x5.
kernels = randn(3,3,2); % 2 kernels in z dimension, each 3x3.
imagesCopy = images; % copy for second method (convn)
% --- first method, slow for loop
for ii = size(images,3) % ii loops over images
images(:,:,ii) = conv2(images(:,:,ii),kernels(:,:,ii),'same');
end
% --- second method, convn
imagesCopy = convn(imagesCopy,kernels,'same');
% --- images and imagesCopy are not the same
isequal(images,imagesCopy)
Thanks for answering though!

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

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by