clear all
I1=imread('1.jpg');
I=rgb2gray(I1);
J=imnoise(I,'gaussian',0,0.05); %添加均值為0,方差為0.05的高斯噪聲
K1=wiener2(J,[5,5]);
figure
imshow(J);
title('加入高斯噪聲圖像');
figure
imshow(K1);
title('5*5窗口自適應濾波');
小波軟閾值
clear all
I1=imread('1.jpg');
I=rgb2gray(I1);
J=imnoise(I,'gaussian',0,0.05); %添加均值為0,方差為0.05的高斯噪聲
[Cr, Sr] = wavedec2(J, 2, 'sym4');
thr= Donoho(J);
J_soft = wdenoise(xr, 'gbl', 's', thr, 'sym4', 2);
figure; imshow(J_soft);
/////////////////////////////////用到的函數
function thr = Donoho(x)
%用Donoho通用閾值公式計算閾值 x為要進行處理的圖像
% thr = delta * sqrt( 2 * log(n))
% n為信號的長度或尺寸
% delta = MAD / 0.6745 -經驗公式,其中MAD為小波分解後高子帶系數的中值
n = prod( size(x) ); %圖像尺寸
%計算delta
[C, S] = wavedec2(x, 1, 'db1'); %小波分解
d = C( prod( S(1,:) ) + 2 * prod( S(2,:) ) + 1 : end); %HH子帶系數
delta = median( abs(d) ) / 0.6745;
%計算閾值
thr = delta * sqrt(2*log(n));
////////////////////////////////////用到的函數
function X = wdenoise(x, measure, sorh, thr, wname, n)
% 閾值去噪函數
% x為帶噪聲圖像
% measure表示全局或局部
% sorh表示軟硬閾值方法
% thr為閾值
% wname為小波函數名
% n為分解層次
[C, S] = wavedec2(x, n, wname); % 對圖像進行小波分解
switch measure
case 'gbl' % 全局閾值方法
dcoef = C( prod(S(1, :)) + 1 : end); % 提取細節部分系數
switch sorh
case 'h' % 硬閾值
dcoef = dcoef .* (abs(dcoef) > thr);
case 's' % 軟閾值
temp = abs(dcoef) - thr;
temp = (temp + abs(temp)) / 2;
dcoef = sign(dcoef) .* temp;
end
C( prod(S(1, :)) + 1 : end) = dcoef;
case 'lvd' % 局部閾值方法
for i = n:-1:1 % 每層單獨處理
k = size(S,1) - i;
first = prod(S(1, :)) + ...
3 * sum(S(2:k-1, 1) .* S(2:k-1, 2)) + 1;
% 第i層細節系數的起始位置
last = first + 3*prod(S(k,:)) - 1; % 終止位置
dcoef = C(first : last); % 細節系數
switch sorh
case 'h' % 硬閾值
dcoef = dcoef .* (abs(dcoef) > thr(i));
case 's' % 軟閾值
temp = abs(dcoef) - thr(i);
temp = (temp + abs(temp)) / 2;
dcoef = sign(dcoef) .* temp;
end
C(first:last) = dcoef;
end
end
X = waverec2(C, S, wname); % 重構圖像