Open In App

Convolution Shape (full/same/valid) in MATLAB

Last Updated : 11 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Convolution is a mathematical operation. It is used in Image processing in MatLab. A mask/filter is used to convolve an image for image detection purposes. But MatLab offers three types of convolution. Here we shall explain the simple convolution.

The filter slides over the image matrix from left to right. The corresponding values of matrix and filter are multiplied and added together. The value of the matrix under the central value of the filter is replaced by the result of the convolution operation.

Depending on the nature of the filter sliding over the matrix, MatLab had three different types of convolutions.

Example 1:

Given an image matrix: 
                      1st row->[1 2 4]
                      2nd row->[2 1 3]
                      3rd row->[3 2 1]
   Given filter: 
                     1st row->[1 0 1]
                     2nd row->[1 0 1]
                     3rd row->[1 0 1] 
                      
Here we are going to apply the 'same' convolution. 
 The resultant matrix is:[3 10 3]
                        [5 14 5]
                        [3  9 3]
                        

Same Convolution

Now we see the Same Convolution. So in this type of convolution, the size of the resultant matrix is the same as the size of the input matrix. The center of the filter/mask is placed over the first element of the first row. Then the filter is slid over the matrix from the left to the right direction. Once a row is covered, the filter is slid down to the next row and it shifts from left to right side. The matrix is padded with 0’s when the filter hangs out of the matrix. 

Note: The size of the output matrix = the size of the input matrix.

Example 1: Average convolution 

Matrix (3, 4): 
1st row->[1     5     2     3]
2nd row->[6     7    10     2]
3rd row->[8     4    10     6]

Averaging Filter (3, 3): 
 1st row->[0.1111    0.1111    0.1111]
 2nd row->[0.1111    0.1111    0.1111]
 3rd row->[0.1111    0.1111    0.1111]
 
Same Convolution result (3, 4):
 1st row->[2.1111    3.4444    3.2222    1.8889]
 2nd row->[3.4444    5.8889    5.4444    3.6667]
 3rd row->[2.7778    5.0000    4.3333    3.1111]

Example 2: Normal convolution

Matrix (3, 4):
Row1->[1     5     2     3]
Row2->[6     7    10     2]
Row3->[8     4    10     6]
Normal Filter (3,3):
Row1->[1     1     1]
Row2->[0     0     0]
Row3->[-1   -1    -1]
Same Convolution result: 
Row1->[13    23    19    12]
Row2->[6     14    10    11]
Row3->[-13  -23   -19   -12]

Let’s take an example for the Same convolution and apply the mask on that,

Example:

Matlab

% MATLAB code for Same Convolution
% Matrix initialisation;
  K = [1     5     2     3; 
     6     7    10     2;
     8     4    10     6];
% Averaging mask creation
  mask1=ones(3,3).*1/9;
  
% Result-1
  R1=conv2(K,mask1,'same');
  
% Random mask 
  mask2=[1     1     1;
         0     0     0;
        -1   -1    -1];
% Result-2
  R2=conv2(K,mask2,'same');
  
% Show matrix-K, mask1 and result-1
  K
  mask1
  R1
  
% Show matrix-K, mask2 and result-2
  K
  mask2
  R2

                    

Output 1:

Output 2: 

Valid Convolution

In this type of convolution, the size of the resultant matrix reduces. The filter/mask is placed over the matrix in such a way that no portion of the filter is hanging out. The filter completely hovers over the matrix then it slides from left to right in rows and top to down in columns.

Size of resultant matrix: Size of matrix = N * N Size of filter = n * n Size of output = (N-n+1) * (N-n+1) If the matrix and filter are of 3 * 3 size, then  result matrix will be 1*1.    

Example: 

Matrix (3, 4):  
Row 1->[1     5     2     3]
Row 2->[6     7    10     2]
Row 3->[8     4    10     6]

Filter (3,3):      
Row 1->[1     1     1]
Row 2->[0     0     0]
Row 3->[-1   -1    -1] 

Valid Convolution result:
[14    10]

Number of rows in output matrix = (3 - 3 + 1) = 1
Number of columns in output matrix = (4 - 3 + 1) = 2

Example:

Matlab

% MATLAB code for 
% Valid convolution.
% Define matrix -1
  matrix1 =[1 2 4 3;
         2 1 3 5; 
         3 2 1 6; 
         2 3 4 9];
           
% Define mask1
  mask1=[1 1 1; 
       0 0 0;
       1 1 1];
         
% Apply valid convolution.
  result1=conv2(matrix1,mask1,'valid'); 
  
% Show matrix, mask and result.
  matrix1 
  mask1
  result1
  
% Define mat-2
  matrix2=[1     5     2     3;
           6     7    10     2;
           8     4    10     6];
% Define mask2
  mask2=[1 1 1; 
         0 0 0; 
       -1 -1 -1];
% Apply valid convolution.
  result2=conv2(matrix2,mask2,'valid');
  
% Show matrix, mask and result.
  matrix2
  mask2
  result2

                    

Output 1:

Output 2:

Full Convolution

In this type of convolution, 

  • The first right bottom of the filter will hover top-left element of the matrix. Their product will be computed. The rest of the filter elements will be multiplied by 0 because the matrix is padded with 0 by default. If we are using an average filter then the average of the product will be computed.
  • The bottom row of the filter will slide over the matrix from the left to the right direction. The last operation in the first row will be computed when the leftmost element of the bottom row of the filter hovers the rightmost element of the first row of the matrix.
  • Thus, after sliding 1st row, the filter will come down by one step and again repeat the same steps.
  • The filter will slide until its first (top) row hovers the last (bottom) row of the matrix. Then will slide from left to right till the leftmost element of the filter hovers on the rightmost element of the last row of the matrix.

The size of resultant matrix: Size of matrix = N * N Size of filter = n * n Size of output = (N+n-1) * (N+n-1) If the matrix is 5*5 and filter is 3 * 3 size, then result matrix will be 7*7.

Example 1: 

Input matrix: 
            1st row->[1 2 4 3]
            2nd row->[2 1 3 5]
            3rd row->[3 2 1 6]
            4th row->[2 3 4 9]
Filter:           
            1st row->[1 0 1]
            2nd row->[0 1 0]
            3rd row->[1 0 1]
Resultant matrix:
           1st row->[1 2 5 5 4 3]
           2nd row->[2 2 7 10 6 5]
           3rd row->[4 6 10 16 10 9]
           4th row->[4 7 13 19 13 14]
           5th row->[3 4 7 12 10 6]
           6th row->[2 3 6 12 4 9]

Example 2: 

Matrix (3, 4):
[1     5     2     3]
[6     7    10     2]
[8     4    10     6]

Filter (3,3):
[1     1     1]
[0     0     0]
[-1    -1    -1]

Full convolution result:
[1     6     8    10     5     3]
[6    13    23    19    12     2]
[7     6    14    10    11     3]
[-6   -13   -23   -19   -12   -2]
[-8   -12   -22   -20   -16   -6]
Rows = (3 + 3 -1) = 5
Columns = (4 + 3 - 1) = 6

Example:

Matlab

% MATLAB code of 
% FULL convolution.
% Define mat-1
  matrix1=[1 2 4 3;
           2 1 3 5;
           3 2 1 6;
           2 3 4 9];
           
% Define mask1
  mask1=[1 0 1; 
         0 1 0;
         1 0 1];
         
% Apply FULL convolution.
  result1=conv2(matrix1,mask1,'full'); 
  
% Show matrix, mask and result.
  matrix1 
  mask1
  result1
  
% Define mat-2
  matrix2=[1     5     2     3; 
         6     7    10     2; 
         8     4    10     6];
           
% Define mask2
  mask2=[1 1 1; 
       0 0 0; 
       -1 -1 -1];
         
% Apply FULL convolution.
  result2=conv2(matrix2,mask2,'full');
  
% Show matrix, mask and result.
  matrix2
  mask2
  result2

                    

Output 1:

Output 2:

 

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads