mysmooth.m

来自「该压缩包中的程序实现对图像的平滑和锐化」· M 代码 · 共 32 行

M
32
字号
function [psmoothed,method]=mysmooth(pin)
%通用的平滑滤波函数
[row col]=size(pin);   %原始图像的大小
%对图像进行零填充
pfilled=zeros(row+2,col+2);
pfilled(2:row+1,2:col+1)=pin;
%输入选用的平滑方法的代号
method=input('input a number to select filter method,1,2,or 3:\n');
%定义均值滤波法的模板
H=ones(3,3)/9;
%根据输入的参数对图像进行平滑
psmoothed=zeros(row,col);
for i=1:row
    for j=1:col
        if(method==1)
            array=[pfilled(i,j) pfilled(i,j+1) pfilled(i,j+2);pfilled(i+1,j) pfilled(i+1,j+1) pfilled(i+1,j+2);pfilled(i+2,j) pfilled(i+2,j+2) pfilled(i+2,j+2)];
            psmoothed(i,j)=sum(sum(H.*array));
        elseif(method==2)
            array=[pfilled(i,j) pfilled(i,j+1) pfilled(i,j+2) pfilled(i+1,j) pfilled(i+1,j+1) pfilled(i+1,j+2) pfilled(i+2,j) pfilled(i+2,j+2) pfilled(i+2,j+2)];
            psmoothed(i,j)=median(array);
        elseif(method==3)
            array=[pfilled(i,j) pfilled(i,j+1) pfilled(i,j+2) pfilled(i+1,j) pfilled(i+1,j+1) pfilled(i+1,j+2) pfilled(i+2,j) pfilled(i+2,j+2) pfilled(i+2,j+2)];
            psmoothed(i,j)=min(array);
        else
            disp('wrong number to select filter method');
            return;
        end
    end
end
psmoothed=uint8(psmoothed);

⌨️ 快捷键说明

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