Reprinted from: http://blog.csdn.net/anan1205/article/details/12313593
Two matrix convolution into matrix multiplication form--matlab application (here consider two-dimensional matrix, corresponding in the image) two image blur (Edge) operation, assuming that the matrix A, b,a represents the source image, B for the convolution template, then the value of B determines the result of the last operation.
application function--conv2 in MATLAB (two-dimensional convolution, one-dimensional correspondence conv)
The formula given by the function is defined as:
The same dimensional data convolution, the essence of it is to flip the convolution template image (rotation 180), here is equivalent to a one-dimensional signal rollover, and then the convolution template from top to bottom, from left to right to calculate the template and the original image of the intersection of the elements of the sum, the sum as the value after the convolution.
In order to verify the subsequent matrix convolution conversion to matrix multiplication, here is an example of the conv2 described:
Suppose that matrix A (4*3), B (2*3) is as follows:
First, B needs to rotate 180,
Command Rotation 2 times 90 can:
b = Rot90 (rot90 (b)), or B = rot90 (h,2); The result is:
Second: Command conv2 function:
C = Conv2 (A, B, ' shape '), the function of the specific operation diagram:
Until the end, the result data is:
There are three types of shape, full represents all data after the convolution, size is (ma+mb-1,na+nb-1) data, same represents the partial data of the original size (Ma,na) After the convolution is returned; valid returns a size of (ma-mb+1, NA-NB+1) data refers to the result data that the template element participates in all operations, i.e. the intersection of the source image and the template is a template.
Matrix convolution into matrix multiplication, there are many methods on the Internet, popularized expressed as:
AXB = B1*A1;
You need to transform the original data with the template data, and the transformation process is as follows:
start with the cycle continuation, 0:
M = Ma+mb-1 = 5; N = Na+nb-1 = 5, corresponding to the convolution after full data size.
So the first exchange of A and B is:
Secondly, the A1 and B1 are respectively transformed .
Conversion b1--for B1 and transformation matrix methods are:
The vector of each line in B1 is transformed into a square matrix ba~be by B, and then a new matrix B1 is combined according to the B matrix for each square matrix. The size of the B1 matrix is ((ma+mb-1) * (na+nb-1), (ma+mb-1) * (na+nb-1)).
conversion a1--stacking vector type
The A1 of the previous step is converted to a column vector by row Shang, then the size of the column vector is ((ma+mb-1) * (na+nb-1), 1).
For instance: The specific code is:
cycle Continuation:
conversion a-->a1
[Plain]View plain copy
- [M1,N1] = size (A); [M2,N2] = size (B);
- m=m1+m2-1;n=n1+n2-1;
- AA = Padarray (a,[m2-1,n2-1], ' post ');%%% complement 0
- BB = Padarray (b,[m1-1,n1-1], ' post ');%%% complement 0
- AA =aa ';
- A1 = AA (:);%%%%
conversion b-->b1
[Plain]View plain copy
- B2 (1,:) = BB (1,:);
- For I =2:m
- B2 (i,:) = BB (m-i+2,:);
- End%%% re-assignment of matrix A ~ E
- B4 = zeros (n,n);%%%%%%% Square of each line of transformation
- B1 = zeros (m*n,m*n);%%%%% the final matrix
- For I =1:m%%%%%%%% vector of several dimensions
- B = B2 (i,:);
- If SUM (SUM (ABS (B)) ==0)
- B4 = zeros (n,n);
- Else
- For j = 1:n%%%%%%% Element
- For k =0:n-1%%%%%%%% position (make a line vector into the form of a square)
- t = mod (j+k,n);
- If t==0
- t = N;
- End%%%end If
- B4 (t,k+1) = B (j);
- End%%%end for
- End%%%end for
- For k =0:m-1%%%%%%%% the position number of each transformation matrix in the large matrix (the position of the small square in the large array is transformed into the form of a large square)
- t = mod (I+K,M);
- If t==0
- t = m;
- End%%%end If
- B1 (k*n+1: (k+1) *n, (t-1) *n+1:t*n) = B4;
- End%%%end for
- End%%%end If Else
- End%%%end for
Result Data conversion:
[Plain]View plain copy
- Result = B1*A1;
- Result = reshape (result,n,m);
- result = Result ';
The resulting results are identical to the CONV2 data results:
Using the Matlab interface faster implementation method: or 5 * 5 of the original data and 3*3 convolution kernel as an example: The code is as follows:
[HTML]View plain copy
- Dd_data = [1,2,4,5,6;6,8,9,1,3;6,8,9,2,4;7,3,5,7,5;1,5,8,9,3]; % 5 * 5
- F_k = [3,4,5;6,7,8;3,2,1]; % 3 * 3
- Dd_data_f_k = conv2 (dd_data,f_k, ' full ');% Matlab Function Interface
- Dd_data1 = Padarray (dd_data,[2 2], ' both ');% expansion of raw data
- V_dd_data = Im2col (dd_data1,[3 3]);% block Data vectorization
- F_K1 = F_k (:);
- F_K1 = f_k1 (end: -1:1);
- F_K1 = f_k1 ';% convolution core representation
- DD_DATA_F_K1 = f_k1 * V_dd_data,% convolution converted to multiply
- DD_DATA_F_K1 = reshape (dd_data_f_k1,[7 7]),% converted to result data
Matrix convolution matlab (reprint)