使用中值滤波、均值滤波及选择掩膜平滑算法对图像进行处理(附matlab代码)

这个也是我们课程设计用到的,很简单,大家可以参考一下。

clc 
clear
img=imread('D:\matlab\bin\COD\1.jpg');
img=rgb2gray(img);
img1=imnoise(img,'salt & pepper',0.1);
img2=imnoise(img,'salt & pepper',0.1);
img3=imnoise(img,'salt & pepper',0.1);
[m,n]=size(img);
nimg=img1;
nnimg=img2;
nnnimg=img3;
%3*3十字窗口中值滤波
for i=3:n-2
    for j=3:m-2
        limg=[img1(j,i),img1(j,i-1),img1(j,i-2),img1(j,i+1),img1(j,i+2),
            img1(j-1,i),img1(j-2,i),img1(j+1,i),img1(j+2,i)];
        limg=sort(limg);
        nimg(j,i)=limg(1,5);
    end
end
%3*3九宫格窗口均值滤波
for i=2:m-1
    for j=2:n-1
        simg=[img2(i,j),img2(i,j-1),img2(i,j+1),img2(i+1,j),img2(i-1,j),
            img2(i-1,j-1),img2(i+1,j+1),img2(i+1,j-1),img2(i-1,j+1)];
        s=mean(simg);
        nnimg(i,j)=s; 
    end
end
%5*5选择式掩膜平滑算法
for i=1:m-4
    for j=1:n-4
        cz=[img3(i,j),img3(i,j+1),img3(i,j+2),img3(i,j+3),img3(i,j+4);
            img3(i+1,j),img3(i+1,j+1),img3(i+1,j+2),img3(i+1,j+3),img3(i+1,j+4);
            img3(i+2,j),img3(i+2,j+1),img3(i+2,j+2),img3(i+2,j+3),img3(i+2,j+4);
            img3(i+3,j),img3(i+3,j+1),img3(i+3,j+2),img3(i+3,j+3),img3(i+3,j+4);
            img3(i+4,j),img3(i+4,j+1),img3(i+4,j+2),img3(i+4,j+3),img3(i+4,j+4)];   
    nine=[img3(i+1,j+1),img3(i+1,j+2),img3(i+1,j+3),img3(i+2,j+1),img3(i+2,j+2),
        img3(i+2,j+3),img3(i+3,j+1),img3(i+3,j+2),img3(i+3,j+3)];
    five1=[img3(i+1,j),img3(i+1,j+1),img3(i+2,j),img3(i+2,j+1),img3(i+2,j+2),img3(i+3,j),img3(i+3,j+1)];
    five2=[img3(i,j+1),img3(i,j+2),img3(i,j+3),img3(i+1,j+1),img3(i+1,j+2),img3(i+1,j+3),img3(i+2,j+2)];
    five3=[img3(i+1,j+3),img3(i+1,j+4),img3(i+2,j+2),img3(i+2,j+3),img3(i+2,j+4),img3(i+3,j+3),img3(i+3,j+4)];
    five4=[img3(i+3,j+1),img3(i+3,j+2),img3(i+3,j+3),img3(i+4,j+1),img3(i+4,j+2),img3(i+4,j+3),img3(i+2,j+2)];
    six1=[img3(i,j),img3(i,j+1),img3(i+1,j), img3(i+1,j),img3(i+1,j+1),img3(i+1,j+2),img3(i+2,j+2)];
    six2=[img3(i,j+3),img3(i,j+4),img3(i+1,j+2),img3(i+1,j+3),img3(i+1,j+4),img3(i+2,j+2),img3(i+2,j+3)];
    six3=[img3(i+4,j+3),img3(i+4,j+4),img3(i+3,j+2),img3(i+3,j+3),img3(i+3,j+4),img3(i+2,j+2),img3(i+2,j+3)];
    six4=[img3(i+4,j),img3(i+4,j+1),img3(i+3,j),img3(i+3,j+1),img3(i+3,j+2),img3(i+2,j+1),img3(i+2,j+2)];
    jun=[mean(nine),mean(five1),mean(five2),mean(five3),mean(five4),mean(six1),mean(six2),mean(six3),mean(six4)];
    fang=[var(im2double(nine)),var(im2double(five1)),var(im2double(five2)),var(im2double(five3)),var(im2double(five4)),
        var(im2double(six1)),var(im2double(six2)),var(im2double(six3)),var(im2double(six4))];
    [o,p]=min(im2uint8(fang));
    nnnimg(i+2,j+2)=jun(1,p);
    end
end
figure(1);title('中值滤波');
subplot(2,2,1),imshow(img1),title('原始图像');
subplot(2,2,2),imshow(nimg),title('增强图像');
figure(2);title('均值滤波');
subplot(2,2,1),imshow(img2),title('原始图像');
subplot(2,2,2),imshow(nnimg),title('增强图像');
figure(3);title('选择式掩膜平滑算法');
subplot(2,2,1),imshow(img3),title('原始图像');
subplot(2,2,2),imshow(nnnimg),title('增强图像');

你可能感兴趣的:(matlab,算法,均值算法)