convolution2d.m

来自「convolution with matlab」· M 代码 · 共 37 行

M
37
字号
in=[0 0 0 0 0 0 ;
    0 0 0 0 0 1;
    0 0 0 0 1 1];
kernel=[0 0 0;
        0 0 1;
        0 1 1];
% in=in';   

rows=size(in,1);
cols=size(in,2);
kRows=size(kernel,1);
kCols=size(kernel,2);

% find center position of kernel (half of kernel size)
kCenterX = uint16(kCols / 2);
kCenterY = uint16(kRows / 2);

for i=1:1:rows+kCenterX              %// rows
    for j=1:1:cols+kCenterY
        sum = 0;  
        out(i,j)=0;
        for m=1:1:kRows
            mm = kRows -  m+1;
            for n=1:1:kCols
                nn = kCols -  n+1;
                %// index of input signal, used for checking boundary
                ii = i + (m - kCenterY);
                jj = j + (n - kCenterX);
                %// ignore input samples which are out of bound
                if  (ii >= 1) & (ii <= rows) & (jj >= 1) & (jj <= cols) 
                    out(i,j) =out(i,j)+ in(ii,jj) * kernel(mm,nn);
                end
            end
        end
   end
end
[out]

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?