Lets Learn together... Happy Reading

" Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference "-Robert Frost

Convolution in MATLAB

                 Let us try to understand convolution by performing spatial averaging on a matrix without using MATLAB built in function ‘conv2()’.

%CONVOLUTION IN MATLAB

%INPUT  MATRIX
A = zeros(5);
A(:) = 1:25;

%KERNEL
avg3 = ones(3)/9;

%CONVOLUTION
Result = conv2(A,avg3,'same');
display(Result);

Steps to be performed:





NOTE :
To define a kernel for spatial averaging, fill the kernel with ones and divide it by the number of elements in it.
For instance, consider kernel of size 4x4 , fill the  matrix with ones and divide it by 16. i.e the total number of elements in the matrix.

MATLAB CODE:
%INPUT  MATRIX
A = zeros(5);
A(:) = 1:25;

%KERNEL
avg3 = ones(3)/9;

%PAD THE MATRIX WITH ZEROS
B = padarray(A,[1 1]);
% PRE-ALLOCATE THE MATRIX
Output = zeros([size(A,1) size(A,2)]);

%PERFORM COONVOLUTION
for i = 1:size(B,1)-2
    for j = 1:size(B,2)-2
        Temp = B(i:i+2,j:j+2).*avg3;
        Output(i,j) = sum(Temp(:));
    end
 end

display(Output);

like button Like "IMAGE PROCESSING" page

10 comments:

Unknown said... Reply to comment

Its interesting and good for students.

Jacob martin said... Reply to comment

The key is to give the peruser a little look into your experience, which urges them to need to take in more by perusing your resume
Medical Science Homework Help

Victor said... Reply to comment

Congratulations very interesting and didactically, do you have a youtube channel? if no, think about it.

Anonymous said... Reply to comment

Man You are a life save, thanks for this easy explaining :) :)

Anonymous said... Reply to comment

Hey In understand your example very clearly. I have question. If i want to subtraction any pixel intensity from its 8 neighbors pixel intensity(this is for every pixels in the image) and then take the absolute sum of the subtraction. How should I define the kernel (avg3 in your example) and perform the task with conv2?

Unknown said... Reply to comment

Thanks was very helpful for my assignment!

Unknown said... Reply to comment

plz tell me how to use it , I cant understand...

fray_dog said... Reply to comment

Hello, I keep getting the following error "Subscript indices must either be real positive integers or logicals.

Error in test (line 16)
Output(i,j) = sum(Temp(:));"

do you have any advice? thank you

Michele said... Reply to comment

I tried with a kernel containing elements with different values (not a constant value as in your example) and the convolution with conv2 or with the for loop gave different results, why? I used the following code:

%CONVOLUTION IN MATLAB with conv2
clear
%INPUT MATRIX
A = zeros(5);
A(:) = 1:25;

%KERNEL
avg3 = rand(3);

%CONVOLUTION
Result = conv2(A,avg3,'same');
display(Result);



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% EXPLICIT calculation using the same kernel


%PAD THE MATRIX WITH ZEROS
B = padarray(A,[1 1]);
% PRE-ALLOCATE THE MATRIX
Output = zeros([size(A,1) size(A,2)]);

%PERFORM COONVOLUTION
for i = 1:size(B,1)-2
for j = 1:size(B,2)-2
Temp = B(i:i+2,j:j+2).*avg3;
Output(i,j) = sum(Temp(:));
end
end

display(Output);

Aaron Angel said... Reply to comment

@Michele

Rotate the convolution mask by 180 degrees.

Example:
Temp = B(i:i+2,j:j+2).*rot90(avg3,2);

The mask in the given example is symmetric so rotating it by 180 degree yielded the same mask.

Enjoyed Reading? Share Your Views

Previous Post Next Post Home
Google ping Hypersmash.com