关于图像去噪的matlab代码

关于图像去噪的matlab代码,第1张

I = imread('apng');%读图像

J = imnoise(I,'salt & pepper',002);%加椒盐噪声

K = medfilt2(J);%用中值滤波去噪声。

imshow(K);

如果效果不好,加上问我。

其实都可以对彩色图像处理的,只是matlab里面的实现不一致。均值滤波和中值滤波matlab函数只考虑了单通道当然就必须转换成灰度图像;小波降噪的matlab函数不知道你是用的那个,肯定也需要把三通道的彩色图像先转换成单通道,分别去噪以后再整合成彩色图像。总的来说一般图像去噪都是对单通道来处理的,因为大部分的filter都是对二维矩阵来 *** 作的,要是对三通道处理也需要分别对不同的通道处理再整合。

不好意思,没看到维纳滤波,程序就不删了

%谱减法语音增强

%输入参数s 语音数据,fs 采样频率,p 下面有说明,共11个,可不输入,有默认值

%“过度减法(oversubtraction)”作减法的时候,保留一小部分原来的背景噪音,用这部分背景噪音来掩盖住音乐噪音的谱峰,从而消除了令人不悦的音乐噪音。

%通过给的参数p,估计噪音,做谱减法。从而消除噪音。

function [ss,po]=specsubm(s,fs,p)

%利用频谱相减(spectral subtraction)增强 [SS,PO]=(S,FS,P)

%

% implementation of spectral subtraction algorithm by R Martin (rather slow)

% algorithm parameters: t in seconds, f in Hz, k dimensionless

% 1: tg = smoothing time constant for signal power estimate (004): high=reverberant, low=musical

% 2: ta = smoothing time constant for signal power estimate

%

used in noise estimation (01)

% 3: tw = fft window length (will be rounded up to 2^nw samples)

% 4: tm = length of minimum filter (15): high=slow response to noise increase, low=distortion

% 5: to = time constant for oversubtraction factor (008)

% 6: fo = oversubtraction corner frequency (800): high=distortion, low=musical

% 7: km = number of minimisation buffers to use (4): high=waste memory, low=noise modulation

% 8: ks = oversampling constant (4)

% 9: kn = noise estimate compensation (15)

% 10:kf = subtraction floor (002): high=noisy, low=musical

% 11:ko = oversubtraction scale factor (4): high=distortion, low=musical

%检查函数的输入参数,如果输入少于三个,po为默认值,po的参数上面有说明

if nargin<3 po=[004 01 0032 15 008 400 4 4 15 002 4]'; else po=p; end

ns=length(s);

ts=1/fs;

ss=zeros(ns,1);

ni=pow2(nextpow2(fspo(3)/po(8)));

ti=ni/fs;

nw=nipo(8);

nf=1+floor((ns-nw)/ni);

nm=ceil(fspo(4)/(nipo(7)));

win=05hamming(nw+1)/108;win(end)=[];

zg=exp(-ti/po(1));

za=exp(-ti/po(2));

zo=exp(-ti/po(5));

px=zeros(1+nw/2,1);

pxn=px;

os=px;

mb=ones(1+nw/2,po(7))nw/2;

im=0;

osf=po(11)(1+(0:nw/2)'fs/(nwpo(6)))^(-1);

imidx=[13 21]';

x2im=zeros(length(imidx),nf);

osim=x2im;

pnim=x2im;

pxnim=x2im;

qim=x2im;

for is=1:nf

idx=(1:nw)+(is-1)ni;

x=rfft(s(idx)win);

x2=xconj(x);

pxn=zapxn+(1-za)x2;

im=rem(im+1,nm);

if im

mb(:,1)=min(mb(:,1),pxn);

else

mb=[pxn,mb(:,1:po(7)-1)];

end

pn=po(9)min(mb,[],2);

%os= oversubtraction factor

os=zoos+(1-zo)(1+osfpn/(pn+pxn));

px=zgpx+(1-zg)x2;

q=max(po(10)sqrt(pn/x2),1-sqrt(ospn/px));

ss(idx)=ss(idx)+irfft(xq);

end

if nargout==0

soundsc([s; ss],fs);

end

Im=Icc;

In=Im;

for a=1:4

for i=2:m-1

for j=2:n-1

if Im(i,j)==1

if Im(i-1,j) + Im(i-1,j+1) +Im(i,j+1) + Im(i+1,j+1) + Im(i+1,j) + Im(i+1,j-1) + Im(i,j-1) + Im(i-1,j-1) <=3

In(i,j)=0;

end

end

if Im(i,j)==0

if Im(i-1,j) + Im(i-1,j+1) +Im(i,j+1) + Im(i+1,j+1) + Im(i+1,j) + Im(i+1,j-1) + Im(i,j-1) + Im(i-1,j-1) >=7

In(i,j)=1;

end

end

end

end

Im=In;

end

load wbarb; % 装载原始图像

subplot(221); % 新建窗口

image(X); % 显示图像

colormap(map); % 设置色彩索引图

title('原始图像'); % 设置图像标题

axis square; % 设置显示比例,生成含噪图像并图示

init=2055615866; % 初始值

randn('seed',init); % 随机值

XX=X+8randn(size(X)); % 添加随机噪声

subplot(222); % 新建窗口

image(XX); % 显示图像

colormap(map); % 设置色彩索引图

title('含噪图像'); % 设置图像标题

axis square; %用小波函数coif2 对图像XX 进行2 层分解

[c,l]=wavedec2(XX,2,'coif2'); % 分解

n=[1,2]; % 设置尺度向量

p=[1028,2408]; % 设置阈值向量,对高频小波系数进行阈值处理

%nc=wthcoef2('h',c,l,n,p,'s');

%nc=wthcoef2('v',c,l,n,p,'s');

nc=wthcoef2('d',c,l,n,p,'s');

X1=waverec2(nc,l,'coif2'); % 图像的二维小波重构

subplot(223); % 新建窗口

image(X1); % 显示图像

colormap(map); %设置色彩索引图

title('第一次消噪后的图像'); % 设置图像标题

axis square; % 设置显示比例,再次对高频小波系数进行阈值处理

%mc=wthcoef2('h',nc,l,n,p,'s');mc=wthcoef2('v',nc,l,n,p,'s');

mc=wthcoef2('d',nc,l,n,p,'s');

X2=waverec2(mc,l,'coif2'); % 图像的二维小波重构

subplot(224); % 新建窗口

image(X2); % 显示图像

colormap(map); % 设置色彩索引图

title('第二次消噪后的图像'); % 设置图像标题

axis square; % 设置显示比例

这个程序改一改吧

%利用小波变换对8位灰度图像进行去噪去噪处理,最后显示信噪比和均方误差

clear all;clc;

A=imread('d:/photo/fabric/cd4 - 复制bmp');

subplot(121),imshow(A);title('original imge');

[m,n]=size(A);

A=double(A);

%选取分解小波

display('选择分解的小波:');

display('enter 1 for haar wavelet');

display('enter 2 for db2 wavelet');

display('enter 3 for db4 wavelet');

display('enter 4 for sym wavelet');

display('enter 5 for sym wavelet');

display('enter 6 for bior wavelet');

display('enter 7 for bior wavelet');

display('enter 8 for mexh wavelet');

display('enter 9 for coif wavelet');

display('enter 10 for meyr wavelet');

display('enter 11 for morl wavelet');

display('enter 12 for rbio wavelet');

display('press any key to quit');

ww=input('enter your choice: ');

switch ww

case 1

wv='haar';

case 2

wv='db2';

case 3

wv='db4' ;

case 4

wv='sym2'

case 5

wv='sym4';

case 6

wv='bior11';

case 7

wv='bior68';

case 8

wv='mexh';

case 9

wv='coif5';

case 10

wv='dmey';

case 11

wv='mor1';

case 12

wv='jpeg97';

otherwise

quit;

end

%选取分解的层数

display('选择分解层数:');

levels=input('enter 1 or 2 : ');

filtertype=wv;

[C,S]=wavedec2(A,levels,filtertype); %小波分解

var=length(C)-S(size(S,1)-1,1)^2+1;

%{

%第一中去噪方法:用此种方法时,效果与选取分解的层数没有关系,只与选取的小波有关系

C(var:length(C))=0; %将对角线的高频系数置零

B=waverec2(C,S,filtertype); %重构图像

%}

%{

%第二种去噪方法:贝叶斯阈值去噪,对各个高频系数进行贝叶斯阈值去噪

display('选择软阈值或者硬阈值:');

display('enter 1 for soft thresholding');

display('enter 2 for hard thresholding');

sorh=input('sorh: ');

sigmahat=median(abs(C(var:length(C))))/06745; %Calculating sigmahat

st=(S(1,1)^2)+1; %低频系数的个数

bayesC=[C(1:st-1),zeros(1,length(st:1:length(C)))]; %只是保留低频信息

for jj=2:size(S,1)-1 %行数

%对于水平高频系数

coeh=C(st:st+S(jj,1)^2-1);

thr=bayes(coeh,sigmahat);

if sorh==1

bayesC(st:st+S(jj,1)^2-1)=sthresh(coeh,thr);

end

if sorh==2

bayesC(st:st+S(jj,1)^2-1)=hthresh(coeh,thr);

end

st=st+S(jj,1)^2;

%对于垂直高频系数

coev=C(st:st+S(jj,1)^2-1);

thr=bayes(coev,sigmahat);

if sorh==1

bayesC(st:st+S(jj,1)^2-1)=sthresh(coev,thr);

end

if sorh==2

bayesC(st:st+S(jj,1)^2-1)=hthresh(coev,thr);

end

st=st+S(jj,1)^2;

%对于对角高频系数

coed=C(st:st+S(jj,1)^2-1);

thr=bayes(coed,sigmahat);

if sorh==1

bayesC(st:st+S(jj,1)^2-1)=sthresh(coed,thr);

end

if sorh==2

bayesC(st:st+S(jj,1)^2-1)=hthresh(coed,thr);

end

st=st+S(jj,1)^2;

end

B=waverec2(bayesC,S,filtertype); %重构图像

%}

%{

%第三种方法:采用Donoho和Johnstone提出的固定阈值的方法进行去噪处理

display('选择软阈值或者硬阈值:');

display('enter 1 for soft thresholding');

display('enter 2 for hard thresholding');

sorh=input('sorh: ');

st=(S(1,1)^2)+1; %低频系数的个数

djC=[C(1:st-1),zeros(1,length(st:1:length(C)))]; %只是保留低频信息

for jj=2:size(S,1)-1 %行数

%对于水平高频系数

coeh=C(st:st+S(jj,1)^2-1);

sigmah=median(abs(coeh))/06745;

thr=sigmahsqrt(2log10(length(coeh)));

if sorh==1

djC(st:st+S(jj,1)^2-1)=sthresh(coeh,thr);

end

if sorh==2

djC(st:st+S(jj,1)^2-1)=hthresh(coeh,thr);

end

st=st+S(jj,1)^2;

%对于垂直高频系数

coev=C(st:st+S(jj,1)^2-1);

sigmav=median(abs(coev))/06745;

thr=sigmavsqrt(2log10(length(coev)));

if sorh==1

djC(st:st+S(jj,1)^2-1)=sthresh(coev,thr);

end

if sorh==2

djC(st:st+S(jj,1)^2-1)=hthresh(coev,thr);

end

st=st+S(jj,1)^2;

%对于对角高频系数

coed=C(st:st+S(jj,1)^2-1);

sigmad=median(abs(coed))/06745;

thr=sigmavsqrt(2log10(length(coed)));

if sorh==1

djC(st:st+S(jj,1)^2-1)=sthresh(coed,thr);

end

if sorh==2

djC(st:st+S(jj,1)^2-1)=hthresh(coed,thr);

end

st=st+S(jj,1)^2;

end

B=waverec2(djC,S,filtertype); %重构图像

%}

subplot(122),imshow(uint8(B));title('de-noised image');

%imwrite(B,'fab5bmp'); %保存图像在m文件的路径中

%计算信噪比

t=0;

for i=1:m

for j=1:n

t=t+(abs(B(i,j)-A(i,j)))^2;

end

end

mse=t/(mn); %图像均方误差

psnr=10log10((255^2)/mse); %峰值信噪比

display('mse:');

mse

display('psnr:');

psnr

以上就是关于关于图像去噪的matlab代码全部的内容,包括:关于图像去噪的matlab代码、matlab 怎么用EMD进行图像二维去噪、用Matlab语言编写一个语音信号或图象信号去噪的程序请高手帮忙等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/9334015.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-27
下一篇 2023-04-27

发表评论

登录后才能评论

评论列表(0条)

保存