Performing 2D convolution
26 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello,
I am new to image processing and had a conceptual question. I am currently trying to perform a 2D convolution of an image and a kernel (filter) in simulink using the 2D convolution block. I have code that transposes the image, then flips it, then performs the 2D convolution with the kernel. However from my (very limited) understanding convolution already flips the filter that I want to apply. I was told that the output of me transposing and flipping the image, then convolving with the kernel looks correct, however from my understanding I would be transposing and flipping the image once then convolving, which involves another flip, so therefore not actually convolving? I have read that this would just produce instead a correlation?
Hoping someone who knows more may be able to help give me a better understanding.
Thank you!
0 commentaires
Réponses (2)
William Rose
le 8 Sep 2025 à 18:29
@Travis, 2D correlation and 2D convolution are not different if the kernel is symmetric (except for the complex conjugation that occurs with xcorr2, which is irrelevant, if your kernel is real). If the kernel is assymmetric, then conv2 and xcorr2 are not identical because the kernel is flipped by one and not the other. Which one flips it depends on what you consider the "default" behavior.
Illustrate with a simple example:
img1=zeros(5,3); img1(3,2)=1; % image 1 = 5x3 , zeros with a central one
A=[1 1 1; 2 1 1; 1 1 1]; % assymmetric kernel, 3x3
img2=conv2(img1,A);
img3=xcorr2(img1,A);
Display min, max difference between the convolution and correlation
fprintf('img3-img2 min, max difference = %.1f, %.1f\n',min(img3-img2,[],'all'),max(img3-img2,[],'all'));
Display the values of img2, img3, plus a column of NaNs in between:
disp([img2,NaN(7,1),img3])
The next example uses an assymmetric image and a symmetric kernel. It shows that conv2 and xcorr2 give the same results in such a case.
Make an image
img1=zeros(20); % 20x20 image array
img1(6:8,14:18)=[1 1 1 1 0; 1 1 1 1 1; 1 0 1 1 1]; % assymmetric non-zero region
Make a symmetric kernel and do convolution and correlation
A=[1 1 1 1 1 1 1;1 2 2 2 2 2 1;1 2 3 4 3 2 1;1 2 2 2 2 2 1;1 1 1 1 1 1 1]; % 5x7 kernel
img2=conv2(img1,A); % convolution
img3=xcorr2(img1,A); % correlation
Display min, max difference between the convolution and correlation
fprintf('img3-img2 min, max difference = %.1f, %.1f\n',min(img3-img2,[],'all'),max(img3-img2,[],'all'));
Display the images
subplot(2,2,1);
imshow(img1,InitialMagnification=1000); title('img1')
rng2=[min(img2,[],'all'),max(img2,[],'all')];;
subplot(2,2,3)
imshow(img2,InitialMagnification=1000,DisplayRange=rng2); title('conv2(img1,A)')
rng3=[min(img3,[],'all'),max(img3,[],'all')];
subplot(2,2,4)
imshow(img3,InitialMagnification=1000,DisplayRange=rng3); title('xcorr2(img1,A)')
0 commentaires
Image Analyst
le 9 Sep 2025 à 2:31
@William Rose gave a good answer. Bottom line, I wouldn't do anything to your image in advance. Simply call conv2 and pass it the original image. Or you can call imfilter. It doesn't flip the kernel (not sure you need to, and don't for simple things like low pass or high pass filters) and imfilter gives you additional options for how edge computations are computed.
0 commentaires
Voir également
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!