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.
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:
Convolution Shape (full/same/valid) in MATLAB
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]