MATLAB: How to divide the sum of convolution value in matlab syntax

convolutionkernel matrix

Hi,
I'm colvolving an image by using my own kernel as in the syntax. The question is, how can i get the average 1/9 for each of the convolution point as normally used in the smoothing process.
I = imread('C:\\MEKH\\conveg.jpg');
figure, imshow(I);
J=rgb2gray(I);
figure, imshow(J)
A = [1,1,1;1,1,1;1,1,1]
%how to get the average eg here 1/9
B = conv2(A,J)
figure, imshow(B)
Thanks

Best Answer

Use this kernel for smoothing the image
A = [1,1,1;1,1,1;1,1,1]/9;
Also, it is better to use the image as the first input to conv2 and the kernel as second
B = conv2(J,A)
in the default setting, there is no difference. However, if you specify a different 'shape' option, then your approach can cause problems.
If you have, image processing toolbox, you can also use imfilter(): https://www.mathworks.com/help/releases/R2020a/images/ref/imfilter.html
You can also create the kernel using following command
A = fspecial('average',3) % requivalent: A = [1,1,1;1,1,1;1,1,1]/9;