1.均值、中值、高斯、維納圖像去噪函數
clc clear g = rgb2gray(imread('chuzao.jpg')); zyy = imread('y.jpg'); g1 = imfilter(g, fspecial('average'));%均值濾波 g2 = medfilt2(g, [3 3]); %3*3中值濾波 h_gaosi1 = fspecial('gaussian',3,1); g3 = imfilter(g, h_gaosi1); %高斯平滑濾波 g4 = wiener2(g,[5 5]); %5*5維納濾波 subplot(3, 2, 1), imshow(zyy), title('原圖'); subplot(3, 2, 2), imshow(g), title('待處理噪聲'); subplot(3, 2, 3), imshow(g1), title('均值濾波'); subplot(3, 2, 4), imshow(g2), title('中值濾波'); subplot(3, 2, 5), imshow(g3), title('高斯平滑濾波'); subplot(3, 2, 6), imshow(g4), title('維納濾波'); %下面是計算PSNR和SSIM的程序口令 psnr(zyy,g) psnr(zyy,g1) psnr(zyy,g2) psnr(zyy,g3) psnr(zyy,g4) ssim(zyy,g1) ssim(zyy,g2) ssim(zyy,g3) ssim(zyy,g4)
2.小波重構除噪ui
clc clear; zyy = imread('y.jpg'); %讀取原圖像 subplot(221); imshow(zyy); title('原圖'); X=imread('chuzao.jpg'); X=rgb2gray(X); subplot(222); imshow(X); title('待處理噪聲'); X=double(X); %用小波函數coif2對圖像X進行2層 % 分解 [c,l]=wavedec2(X,2,'coif2'); % 設置尺度向量 n=[1,2]; % 設置閾值向量 , 對高頻小波係數進行閾值處理 p=[10.28,24.08]; nc=wthcoef2('h',c,l,n,p,'s'); % 圖像的二維小波重構 X1=waverec2(nc,l,'coif2'); subplot(223); imshow(uint8(X1)); %colormap(map); title(' 小波第一次消噪後的圖像 '); %再次對高頻小波係數進行閾值處理 mc=wthcoef2('v',nc,l,n,p,'s'); % 圖像的二維小波重構 X2=waverec2(mc,l,'coif2'); subplot(224); imshow(uint8(X2)); title(' 小波重構第二次消噪後的圖像 ');
3.NL-means除噪spa
clc clear g = rgb2gray(imread('chuzao.jpg')); %讀取待去噪圖 zyy = imread('y.jpg');%讀取原圖 g1 = double(g); %進行NL-means除噪 [m,n]=size(g1); ds=2;% block size for calculate weight Ds=5;% search block h=10;% decay factor offset=ds+Ds; PaddedImg = padarray(g1,[ds+Ds,ds+Ds],'symmetric','both');% 擴展圖像,便於處理 %距離加權核 %非均值核 [x,y]=meshgrid(-ds:ds,-ds:ds); kernel=1./(x.*x+y.*y+1); %均值核 % kernel=ones(2*ds+1,2*ds+1); kernel=kernel./((2*ds+1)*(2*ds+1)); dst=zeros(m,n);% output %---------------------------% % Non-Local Means Denoising %---------------------------% for i=1:m for j=1:n %當前點座標和鄰域窗口 i1=i+offset; j1=j+offset; W1=PaddedImg(i1-ds:i1+ds,j1-ds:j1+ds); %加權因子矩陣和圖像 weight=zeros(2*Ds+1,2*Ds+1); image=PaddedImg(i1-Ds:i1+Ds,j1-Ds:j1+Ds); for r=-Ds:Ds for s=-Ds:Ds %跳過當前點 if(r==0&&s==0) continue; end %待加權點座標和鄰域窗口 i2=i1+r; j2=j1+s; W2=PaddedImg(i2-ds:i2+ds,j2-ds:j2+ds); %核加權的距離和加權因子 distance=sum(sum(kernel.*(W1-W2).*(W1-W2))); weight(r+Ds+1,s+Ds+1)=exp(-distance/(h*h)); end end %最大權重賦給當前點,歸一化權重 weight(Ds+1,Ds+1)=max(max(weight)); weight=weight/(sum(weight(:))); dst(i,j)=sum(sum(image.*weight)); end end %---------------------------% % output %---------------------------% subplot(121),imshow(g1,[]),title('待處理'); subplot(122),imshow(dst,[]),title('NL-means除噪'); psnr(zyy,uint8(g1)) psnr(zyy,uint8(dst)) ssim(zyy,uint8(g1)) ssim(zyy,uint8(dst))
4.均值、中值、高斯、維納、小波、NL-means的PSNR、SSIM對比code
|
待除噪圖orm |
均值濾波blog |
中值除噪ci |
高斯除噪it |
維納除噪table |
小波除噪class |
NL-除噪 |
PSNR |
20.7648 |
23.724 |
24.2671 |
24.0838 |
26.0139 |
22.3159 |
26.8054 |
SSIM |
0.3504 |
0.5863 |
0.5518 |
0.6006 |
0.7006 |
0.3979 |
0.6385 |