Matrix convolution matlab (reprint)

Source: Internet
Author: User

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  
    1. [M1,N1] = size (A); [M2,N2] = size (B);
    2. m=m1+m2-1;n=n1+n2-1;
    3. AA = Padarray (a,[m2-1,n2-1], ' post ');%%% complement 0
    4. BB = Padarray (b,[m1-1,n1-1], ' post ');%%% complement 0
    5. AA =aa ';
    6. A1 = AA (:);%%%%

conversion b-->b1

[Plain]View plain copy  
  1. B2 (1,:) = BB (1,:);
  2. For I =2:m
  3. B2 (i,:) = BB (m-i+2,:);
  4. End%%% re-assignment of matrix A ~ E
  5. B4 = zeros (n,n);%%%%%%% Square of each line of transformation
  6. B1 = zeros (m*n,m*n);%%%%% the final matrix
  7. For I =1:m%%%%%%%% vector of several dimensions
  8. B = B2 (i,:);
  9. If SUM (SUM (ABS (B)) ==0)
  10. B4 = zeros (n,n);
  11. Else
  12. For j = 1:n%%%%%%% Element
  13. For k =0:n-1%%%%%%%% position (make a line vector into the form of a square)
  14. t = mod (j+k,n);
  15. If t==0
  16. t = N;
  17. End%%%end If
  18. B4 (t,k+1) = B (j);
  19. End%%%end for
  20. End%%%end for
  21. 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)
  22. t = mod (I+K,M);
  23. If t==0
  24. t = m;
  25. End%%%end If
  26. B1 (k*n+1: (k+1) *n, (t-1) *n+1:t*n) = B4;
  27. End%%%end for
  28. End%%%end If Else
  29. End%%%end for

Result Data conversion:

[Plain]View plain copy  
    1. Result = B1*A1;
    2. Result = reshape (result,n,m);
    3. 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  
  1. 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
  2. F_k = [3,4,5;6,7,8;3,2,1]; % 3 * 3
  3. Dd_data_f_k = conv2 (dd_data,f_k, ' full ');% Matlab Function Interface
  4. Dd_data1 = Padarray (dd_data,[2 2], ' both ');% expansion of raw data
  5. V_dd_data = Im2col (dd_data1,[3 3]);% block Data vectorization
  6. F_K1 = F_k (:);
  7. F_K1 = f_k1 (end: -1:1);
  8. F_K1 = f_k1 ';% convolution core representation
  9. DD_DATA_F_K1 = f_k1 * V_dd_data,% convolution converted to multiply
  10. DD_DATA_F_K1 = reshape (dd_data_f_k1,[7 7]),% converted to result data

Matrix convolution matlab (reprint)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.