homework.m.txt

来自「本代码是本人于前年完成的利用小波变换技术实现的图像分解与合成的MATLAB源代码」· 文本 代码 · 共 609 行 · 第 1/2 页

TXT
609
字号
%输出*.png文件
fn=strcat(fname,'_',wavetype,'_org.png');
imwrite(x,fn);

fn=strcat(fname,'_',wavetype,'_00.png');
imwrite(ya0/255,fn);%而存盘时,不用色谱表,需要换算

fn=strcat(fname,'_',wavetype,'_05.png');
imwrite(ya5/255,fn);

fn=strcat(fname,'_',wavetype,'_10.png');
imwrite(ya10/255,fn);

fn=strcat(fname,'_',wavetype,'_20.png');
imwrite(ya20/255,fn);

dispnumofzero(filename,wavetype,zero0,zero5,zero10,zero20);


%---------------------------256色彩图-----------------------------------------------------   
case '256'
   
%绘制原图像
   subplot(451);image(x);colormap(map);title('原 图 像');axis square;
   
%绘制色彩表   
c=zeros(16,16);
for i=1:16;
   for j=1:16;
      c(i,j)=(i-1)*10+(j-1);
   end 
end
subplot(4,5,11);image(c);colormap(map);title('256色彩表');axis square;

%1级分解及绘图
sx=size(x);
[ca1,ch1,cv1,cd1]=mydwt2(x,wavetype);
sx2=size(ca1);
subplot(452);imagesc(ca1);colormap(map);title('1次分解ca1');axis square;axis off;
subplot(453);imagesc(ch1);colormap(map);title('1次分解ch1');axis square;axis off;
subplot(454);imagesc(cv1);colormap(map);title('1次分解cv1');axis square;axis off;
subplot(455);imagesc(cd1);colormap(map);title('1次分解cd1');axis square;axis off;

%2级分解及绘图
[ca2,ch2,cv2,cd2]=mydwt2(ca1,wavetype);
sx4=size(ca2);
subplot(457);imagesc(ca2);colormap(map);title('2次分解ca2');axis square;axis off;
subplot(458);imagesc(ch2);colormap(map);title('2次分解ch2');axis square;axis off;
subplot(459);imagesc(cv2);colormap(map);title('2次分解cv2');axis square;axis off;
subplot(4,5,10);imagesc(cd2);colormap(map);title('2次分解cd2');axis square;axis off;

%3级分解及绘图
[ca3,ch3,cv3,cd3]=mydwt2(ca2,wavetype);
subplot(4,5,12);imagesc(ca3);colormap(map);title('3次分解ca3');axis square;axis off;
subplot(4,5,13);imagesc(ch3);colormap(map);title('3次分解ch3');axis square;axis off;
subplot(4,5,14);imagesc(cv3);colormap(map);title('3次分解cv3');axis square;axis off;
subplot(4,5,15);imagesc(cd3);colormap(map);title('3次分解cd3');axis square;axis off;

%阈值为0:统计“0”系数,合成图像及绘图
thr=0
[ya0,zero0]=threshall(ca3,ch3,cv3,cd3,ch2,cv2,cd2,ch1,cv1,cd1,thr,wavetype);
a0max=max(max(x64-ya0));a0max
yb0=round(ya0+1);%原x为unit8型,现ya0为double,引用256色彩表需要换算,并四舍五入。
subplot(4,5,17);image(yb0);colormap(map);title('阈值0重构图像');axis square;axis off;
zero0

%阈值为5:统计“0”系数,合成图像及绘图
thr=5
[ya5,zero5]=threshall(ca3,ch3,cv3,cd3,ch2,cv2,cd2,ch1,cv1,cd1,thr,wavetype);
a0max=max(max(x64-ya5));a0max
yb5=round(ya5+1);
subplot(4,5,18);image(yb5);colormap(map);title('阈值5重构图像');axis square;axis off;
zero5

%阈值为10:统计“0”系数,合成图像及绘图
thr=10
[ya10,zero10]=threshall(ca3,ch3,cv3,cd3,ch2,cv2,cd2,ch1,cv1,cd1,thr,wavetype);
a0max=max(max(x64-ya10));a0max
yb10=round(ya10+1);
subplot(4,5,19);image(yb10);colormap(map);title('阈值10重构图像');axis square;axis off;
zero10

%阈值为20:统计“0”系数,合成图像及绘图
thr=20
[ya20,zero20]=threshall(ca3,ch3,cv3,cd3,ch2,cv2,cd2,ch1,cv1,cd1,thr,wavetype);
a0max=max(max(x64-ya20));a0max
yb20=round(ya20+1);
subplot(4,5,20);image(yb20);colormap(map);title('阈值20重构图像');axis square;axis off;
zero20

%输出*.png文件
fn=strcat(fname,'_',wavetype,'_org.png');
imwrite(x,map,fn);

fn=strcat(fname,'_',wavetype,'_00.png');
imwrite(yb0,map,fn);

fn=strcat(fname,'_',wavetype,'_05.png');
imwrite(yb5,map,fn);

fn=strcat(fname,'_',wavetype,'_10.png');
imwrite(yb10,map,fn);

fn=strcat(fname,'_',wavetype,'_20.png');
imwrite(yb20,map,fn);

dispnumofzero(filename,wavetype,zero0,zero5,zero10,zero20);

end

%------------------------------------子程序thresh---------------------------------------------------
%子程序thresh:输入矩阵x,阈值thr, 阈值处理后输出矩阵y,0系数个数zeronum.
function [y,zeronum]=thresh(x,thr);
zeronum=0;
for i=1:size(x,1);
   for j=1:size(x,2);
      if abs(x(i,j))<=thr;
         zeronum=zeronum+1;
         y(i,j)=0;
      else
         y(i,j)=x(i,j);
      end
   end
end


%-----------------------------------子程序threshall--------------------------------------------
%子程序threshall: 输入三级分解矩阵xa3,xh3,xv3,xd3,二级分解矩阵xa2,xh2,xv2,xd2,一级分解矩阵xa1,xh1,xv1,xd1
%                 三级分解矩阵大小sx3,二级分解矩阵大小sx2,一级分解矩阵大小sx,小波类型wavetype,阈值thr
%                 输出经阈值处理重构的图像矩阵ya,小波分解矩阵0系数的总和zeroall

function [ya,zeroall]=threshall(xa3,xh3,xv3,xd3,xh2,xv2,xd2,xh1,xv1,xd1,thr,wavetype);
zeroall=0;
[ya3,zero]=thresh(xa3,thr);zeroall=zeroall+zero;
[yh3,zero]=thresh(xh3,thr);zeroall=zeroall+zero;
[yv3,zero]=thresh(xv3,thr);zeroall=zeroall+zero;
[yd3,zero]=thresh(xd3,thr);zeroall=zeroall+zero;
[yh2,zero]=thresh(xh2,thr);zeroall=zeroall+zero;
[yv2,zero]=thresh(xv2,thr);zeroall=zeroall+zero;
[yd2,zero]=thresh(xd2,thr);zeroall=zeroall+zero;
[yh1,zero]=thresh(xh1,thr);zeroall=zeroall+zero;
[yv1,zero]=thresh(xv1,thr);zeroall=zeroall+zero;
[yd1,zero]=thresh(xd1,thr);zeroall=zeroall+zero;
ya2=myidwt2(ya3,yh3,yv3,yd3,wavetype);
ya1=myidwt2(ya2,yh2,yv2,yd2,wavetype);
ya=myidwt2(ya1,yh1,yv1,yd1,wavetype);



%----------------------------子程序conbindRGB--------------------------------------------
%子程序conbindRGB:RGB矩阵合成真彩图像矩阵,保证y在[0,1]范围内。
function y=conbindRGB(yr,yg,yb)
y=zeros(size(yr,1),size(yr,2),3);
n=0;m=0;
for i=1:size(yr,1);
   for j=1:size(yr,2);
      y(i,j,1)=yr(i,j)/255;if y(i,j,1)>=1;y(i,j,1)=1;end;if y(i,j,1)<=0;y(i,j,1)=0;end;
      y(i,j,2)=yg(i,j)/255;if y(i,j,2)>=1;y(i,j,2)=1;end;if y(i,j,2)<=0;y(i,j,2)=0;end;
      y(i,j,3)=yb(i,j)/255;if y(i,j,3)>=1;y(i,j,3)=1;end;if y(i,j,3)<=0;y(i,j,3)=0;end;
      end
end
         

% my own dwt2() function
% mydwt2 Single-level discrete 2-D wavelet transform.
%       mydwt2 performs a single-level 2-D wavelet decomposition
%       with respect to either a particular wavelet .
%       [ca,ch,cv,cd] = mydwt2(x,wname) computes the approximation
%       coefficients matrix CA and details coefficients matrices 
%       CH, CV, CD, obtained by a wavelet decomposition of the 
%       input matrix X.
%       wname is a string containing the wavelet name.
function [ca,ch,cv,cd,mh,mv]=mydwt2(x,wname)
s=size(x);
m1=[];
m2=[];
ca=[];
ch=[];
cv=[];
cd=[];
for i=1:s(1)
    [c1,c2]=dwt(x(i,:),wname,'mode','per');
    m1=[m1;c1];
    m2=[m2;c2];
end

mh=[m1,m2];         % 合成行变换后的图像矩阵

m1=m1';
m2=m2';

s=size(m1);
for i=1:s(1)
    [c1,c2]=dwt(m1(i,:),wname,'mode','per');
    ca=[ca;c1];
    cv=[cv;c2];
end
s=size(m2);
for i=1:s(1)
    [c1,c2]=dwt(m2(i,:),wname,'mode','per');
    ch=[ch;c1];
    cd=[cd;c2];
end
ca=ca';
ch=ch';
cv=cv';
cd=cd';

mv=[ca,ch;cv,cd];  % 合成行变换后再加列变换后的图像矩阵


% my own idwt2() function
%IDWT2  Single-level inverse discrete 2-D wavelet transform.
%    IDWT2 performs a single-level 2-D wavelet reconstruction
%    with respect to either a particular wavelet
%    ('wname', see WFILTERS for more information)
%    x=myidwt2(ca,ch,cv,cd,wname);
%    uses the wavelet 'wname' to compute the single-level 
%    reconstructed approximation coefficients matrix X,
%    based on approximation matrix CA and (horizontal, vertical,
%    and diagonal) details matrices CH, CV and CD.

function [x,xv]=myidwt2(ca,ch,cv,cd,wname)
ca=ca';
ch=ch';
cv=cv';
cd=cd';
m1=[];
m2=[];
x=[];
s=size(ca);
for i=1:s(1)
    r=idwt(ca(i,:),cv(i,:),wname,'mode','per');
    m1=[m1;r];
end
s=size(ch);
for i=1:s(1)
    r=idwt(ch(i,:),cd(i,:),wname,'mode','per');
    m2=[m2;r];
end
m1=m1';
m2=m2';

xv=[m1,m2];     %列方向变换后的图像矩阵
    
s=size(m1);
for i=1:s(1)
    r=idwt(m1(i,:),m2(i,:),wname,'mode','per');
    x=[x;r];
end


%my own reconstruction function
function [c1_f,c1_f_v,c2_f,c2_f_v,c3_f,c3_f_v]=myrec(xa3,xh3,xv3,xd3,xh2,xv2,xd2,xh1,xv1,xd1,thr,wname)

% 第一次重构
ca3_f=thresh(xa3,thr);
ch3_f=thresh(xh3,thr);
cv3_f=thresh(xv3,thr);
cd3_f=thresh(xd3,thr);
[ca2_f,ca2_f_v]=myidwt2(ca3_f,ch3_f,cv3_f,cd3_f,wname);

ch2_f=thresh(xh2,thr);
cv2_f=thresh(xv2,thr);
cd2_f=thresh(xd2,thr);

ca1_f=[ca2_f,ch2_f;cv2_f,cd2_f];
ca1_f_v=[ca2_f_v,ch2_f;cv2_f,cd2_f];

ch1_f=thresh(xh1,thr);
cv1_f=thresh(xv1,thr);
cd1_f=thresh(xd1,thr);

c1_f=[ca1_f,ch1_f;cv1_f,cd1_f];
c1_f_v=[ca1_f_v,ch1_f;cv1_f,cd1_f];

% 第二次重构
[ca1_f_2,ca1_f_2_v]=myidwt2(ca2_f,ch2_f,cv2_f,cd2_f,wname);
c2_f=[ca1_f_2,ch1_f;cv1_f,cd1_f];
c2_f_v=[ca1_f_2_v,ch1_f;cv1_f,cd1_f];

% 第三次重构
[c3_f,c3_f_v]=myidwt2(ca1_f_2,ch1_f,cv1_f,cd1_f,wname);


% count the number of zeros and display in the screen
function [nums]=dispnumofzero(file,wname,zere0,zere5,zere10,zere20)
figure;
s0=strcat('阈值为0时,图像',file,'采用',wname,'三级小波变换后,零的个数为: ',num2str(zere0));
s5=strcat('阈值为5时,图像',file,'采用',wname,'三级小波变换后,零的个数为: ',num2str(zere5));
s10=strcat('阈值为10时,图像',file,'采用',wname,'三级小波变换后,零的个数为: ',num2str(zere10));
s20=strcat('阈值为20时,图像',file,'采用',wname,'三级小波变换后,零的个数为: ',num2str(zere20));

nums=[s0,s5,s10,s20];

s=strcat(file(3:(length(file)-4)),'_',wname,'numofzero','.txt');
csvwrite(s,nums);

text(0,0.8,s0);
text(0,0.6,s5);
text(0,0.4,s10);
text(0,0.2,s20);
axis off;

⌨️ 快捷键说明

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