Clear Filters
Clear Filters

How to divide the sum of convolution value in matlab syntax?

1 view (last 30 days)
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

Accepted Answer

Ameer Hamza
Ameer Hamza on 13 Jun 2020
Edited: Ameer Hamza on 13 Jun 2020
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;

More Answers (1)

Haritha GB
Haritha GB on 13 Jun 2020
I'm not entirely sure what your question means, since I am not familiar with the smoothing process.
From what I gather, you either want to a) find the sum of the entire output matrix elements and then divide by nine, or you want to b) divide each element of the output matrix by nine.
Either way the implementation line of code must come after calculating the convolution, i.e, put it after this line:
B = conv2(A,J)
So, for a)
m = length(B);
sum = 0;
for i = 1:m
sum = sum + B[i];
end
average = sum/9
And for b)
AVG = B/9
Hope I answered your question.
If you had intended otherwise with the question, let me know so I could answer it.

Community Treasure Hunt

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

Start Hunting!