- function X = meanfilter(x, m)
- % 均值滤波 原矩阵为x 使用的模板为m
- % 模板大小
- dim1 = size(m, 1);
- dim2 = size(m, 2);
- % 对边界的像素不处理 计算需要处理像素的范围
- row_min = (dim1+1) / 2;
- row_max = size(x, 1) - row_min + 1;
- col_min = (dim2+1) / 2;
- col_max = size(x, 2) - col_min + 1;
- X = x;
- for i = row_min : row_max
- for j = col_min : col_max
- X(i,j) = sum( sum( m .* x( i-(dim1-1)/2:i+(dim1-1)/2, j-(dim2-1)/2:j+(dim2-1)/2) )) / (dim1*dim2) ;
- end
- end