📄 homework.m.txt
字号:
function []=report()
%本程序可完成如下功能:
% 1. 可在MATLAB的命令窗口下直接输入 homework 进行调用;
% 2. 本程序重点对真彩图像使用haar小波或dbN(N=1,2,3...)型小波进行分解合成的每一个中间步骤进行了详细的展示。
% 运行结果是在四个不同的窗口中分别显示对所输入的文件采用所输入的小波类型进行三级小波变换的全过程、
% 重构的全过程、采用四种不同阈值(0,5,10,20)时的重构图像、原始图像及四种重构图像中的零的个数;
% 3. 可对*.bmp,*.jpg,*.tif,*.png,*.pcx,256色灰度、256色彩表文件使用haar小波或dbN(N=1,2,3...)型
% 小波进行3级非标准小波分解和合成,并将中间过程显示在窗口中,同时以文件的形式存储在当前目录中;
% 4. 本程序所采用的小波变换为非标准小波变换方式;
% 5. 中间过程所产生的图像及重构以后所形成的最终合成图像,均以*.png格式文件存储在当前目录中;
clear all;
close all;
clc;
filename=input('请输入所需处理的图像文件名及扩展名,按回车,本程序将自动使用缺省值.\lwh256.png : ','s');
if filename=='';
filename='.\lwh256.png';
end
wavetype=input('请输入所用小波名称(dbN or haar,按回车取缺省值为:haar): ','s');
if wavetype=='';
wavetype='haar' % 取小波缺省值haar
end
%-------------------------读取图像文件---------------------------------------------------
fname=filename(1:4);
[x,m]=imread(filename); %图像矩阵x(unit8数据类型)色谱矩阵m
clf
size(x);
size(m);
x64=double(x);
%------------------------判定图像类型----------------------------------------------------
if size(x,3)==3;
'this is full color picture'
colortype='full'; %真彩图,没有色谱矩阵
elseif size(x,3)==1 & size(m)==[256 3];
'this is 256 color picture'
colortype='256'; %256色彩图
map=m; %色谱矩阵map
elseif size(x,3)==1;
'this is 256 grayscale picture'
colortype='gray'; %256灰度图
map=gray(256); %色谱矩阵map
else
'undefined picture'
colortype='unknown'
end
switch colortype
%---------------------------真彩图-----------------------------------------------------
case 'full'
%分离RGB三色矩阵
xr=x( :, :, 1);
xg=x( :, :, 2);
xb=x( :, :, 3);
%绘制原图像
subplot(441);image(x);title('原 图 像');axis square;
%1级分解及绘图
[car1,chr1,cvr1,cdr1,mhr1,mvr1]=mydwt2(xr,wavetype);
[cag1,chg1,cvg1,cdg1,mhg1,mvg1]=mydwt2(xg,wavetype);
[cab1,chb1,cvb1,cdb1,mhb1,mvb1]=mydwt2(xb,wavetype);
h1=conbindRGB(mhr1,mhg1,mhb1);
v1=conbindRGB(mvr1,mvg1,mvb1);
fn=strcat(fname,'_dec_',wavetype,'_',num2str(1),'_','h.png');
fn_v=strcat(fname,'_dec_',wavetype,'_',num2str(1),'_','v.png');
imwrite(h1,fn);
imwrite(v1,fn_v);
subplot(442);image(h1);title('1级分解图像');axis square;axis off;
subplot(446);image(v1);axis square;axis off;
%2级分解及绘图
[car2,chr2,cvr2,cdr2,mhr2,mvr2]=mydwt2(car1,wavetype);
[cag2,chg2,cvg2,cdg2,mhg2,mvg2]=mydwt2(cag1,wavetype);
[cab2,chb2,cvb2,cdb2,mhb2,mvb2]=mydwt2(cab1,wavetype);
rh2=[mhr2,chr1;cvr1,cdr1];
gh2=[mhg2,chg1;cvg1,cdg1];
bh2=[mhb2,chb1;cvb1,cdb1];
rv2=[mvr2,chr1;cvr1,cdr1];
gv2=[mvg2,chg1;cvg1,cdg1];
bv2=[mvb2,chb1;cvb1,cdb1];
h2=conbindRGB(rh2,gh2,bh2);
v2=conbindRGB(rv2,gv2,bv2);
fn=strcat(fname,'_dec_',wavetype,'_',num2str(2),'_','h.png');
fn_v=strcat(fname,'_dec_',wavetype,'_',num2str(2),'_','v.png');
imwrite(h2,fn);
imwrite(v2,fn_v);
subplot(447);image(h2);title('2级分解图像');axis square;axis off;
subplot(4,4,11);image(v2);axis square;axis off;
%3级分解及绘图
[car3,chr3,cvr3,cdr3,mhr3,mvr3]=mydwt2(car2,wavetype);
[cag3,chg3,cvg3,cdg3,mhg3,mvg3]=mydwt2(cag2,wavetype);
[cab3,chb3,cvb3,cdb3,mhb3,mvb3]=mydwt2(cab2,wavetype);
hr31=[mhr3,chr2;cvr2,cdr2];
hg31=[mhg3,chg2;cvg2,cdg2];
hb31=[mhb3,chb2;cvb2,cdb2];
hr3=[hr31,chr1;cvr1,cdr1];
hg3=[hg31,chg1;cvg1,cdg1];
hb3=[hb31,chb1;cvb1,cdb1];
h3=conbindRGB(hr3,hg3,hb3);
fn=strcat(fname,'_dec_',wavetype,'_',num2str(3),'_','h.png');
imwrite(h3,fn);
subplot(4,4,12);image(h3);title('3级分解图像');axis square;axis off;
car22=[car3,chr3;cvr3,cdr3];
cag22=[cag3,chg3;cvg3,cdg3];
cab22=[cab3,chb3;cvb3,cdb3];
car221=[car22,chr2;cvr2,cdr2];
cag221=[cag22,chg2;cvg2,cdg2];
cab221=[cab22,chb2;cvb2,cdb2];
vr3=[car221,chr1;cvr1,cdr1];
vg3=[cag221,chg1;cvg1,cdg1];
vb3=[cab221,chb1;cvb1,cdb1];
v3=conbindRGB(vr3,vg3,vb3);
subplot(4,4,16);image(v3);axis square;axis off;
fn_v=strcat(fname,'_dec_',wavetype,'_',num2str(3),'_','v.png');
imwrite(v3,fn_v);
%重构及绘图
figure;
thr=0;
subplot(441);image(v3);title('重构前的图像');axis square;
[r1_p,r1_v_p,r2_p,r2_v_p,r3_p,r3_v_p]=myrec(car3,chr3,cvr3,cdr3,chr2,cvr2,cdr2,chr1,cvr1,cdr1,thr,wavetype);
[g1_p,g1_v_p,g2_p,g2_v_p,g3_p,g3_v_p]=myrec(cag3,chg3,cvg3,cdg3,chg2,cvg2,cdg2,chg1,cvg1,cdg1,thr,wavetype);
[b1_p,b1_v_p,b2_p,b2_v_p,b3_p,b3_v_p]=myrec(cab3,chb3,cvb3,cdb3,chb2,cvb2,cdb2,chb1,cvb1,cdb1,thr,wavetype);
rgb1_p=conbindRGB(r1_p,g1_p,b1_p);
rgb2_p=conbindRGB(r2_p,g2_p,b2_p);
rgb3_p=conbindRGB(r3_p,g3_p,b3_p);
rgb1_v_p=conbindRGB(r1_v_p,g1_v_p,b1_v_p);
rgb2_v_p=conbindRGB(r2_v_p,g2_v_p,b2_v_p);
rgb3_v_p=conbindRGB(r3_v_p,g3_v_p,b3_v_p);
fn_v=strcat(fname,'_rec_',wavetype,'_',num2str(1),'_','v.png');
imwrite(rgb1_v_p,fn_v);
fn_v=strcat(fname,'_rec_',wavetype,'_',num2str(2),'_','v.png');
imwrite(rgb2_v_p,fn_v);
fn_v=strcat(fname,'_rec_',wavetype,'_',num2str(3),'_','v.png');
imwrite(rgb3_v_p,fn_v);
fn=strcat(fname,'_rec_',wavetype,'_',num2str(1),'_','h.png');
imwrite(rgb1_p,fn);
fn=strcat(fname,'_rec_',wavetype,'_',num2str(2),'_','h.png');
imwrite(rgb2_p,fn);
fn=strcat(fname,'_rec_',wavetype,'_',num2str(3),'_','h.png');
imwrite(rgb3_p,fn);
subplot(442);image(rgb1_v_p);title('一级重构图');axis square;axis off;
subplot(446);image(rgb1_p);axis square;axis off;
subplot(447);image(rgb2_v_p);title('二级重构图');axis square;axis off;
subplot(4,4,11);image(rgb2_p);axis square;axis off;
subplot(4,4,12);image(rgb3_v_p);title('三级重构图');axis square;axis off;
subplot(4,4,16);image(rgb3_p);axis square;axis off;
figure;
%阈值为0:统计“0”系数,合成图像及绘图
thr=0
[yar0,zeror0]=threshall(car3,chr3,cvr3,cdr3,chr2,cvr2,cdr2,chr1,cvr1,cdr1,thr,wavetype);
[yag0,zerog0]=threshall(cag3,chg3,cvg3,cdg3,chg2,cvg2,cdg2,chg1,cvg1,cdg1,thr,wavetype);
[yab0,zerob0]=threshall(cab3,chb3,cvb3,cdb3,chb2,cvb2,cdb2,chb1,cvb1,cdb1,thr,wavetype);
ya0=conbindRGB(yar0,yag0,yab0);
zero0=zeror0+zerog0+zerob0
subplot(2,2,1);image(ya0);title('阈值0重构图像');axis square;
%阈值为5:统计“0”系数,合成图像及绘图
thr=5
[yar5,zeror5]=threshall(car3,chr3,cvr3,cdr3,chr2,cvr2,cdr2,chr1,cvr1,cdr1,thr,wavetype);
[yag5,zerog5]=threshall(cag3,chg3,cvg3,cdg3,chg2,cvg2,cdg2,chg1,cvg1,cdg1,thr,wavetype);
[yab5,zerob5]=threshall(cab3,chb3,cvb3,cdb3,chb2,cvb2,cdb2,chb1,cvb1,cdb1,thr,wavetype);
ya5=conbindRGB(yar5,yag5,yab5);
zero5=zeror5+zerog5+zerob5
subplot(2,2,2);image(ya5);title('阈值5重构图像');axis square;
%阈值为10:统计“0”系数,合成图像及绘图
thr=10
[yar10,zeror10]=threshall(car3,chr3,cvr3,cdr3,chr2,cvr2,cdr2,chr1,cvr1,cdr1,thr,wavetype);
[yag10,zerog10]=threshall(cag3,chg3,cvg3,cdg3,chg2,cvg2,cdg2,chg1,cvg1,cdg1,thr,wavetype);
[yab10,zerob10]=threshall(cab3,chb3,cvb3,cdb3,chb2,cvb2,cdb2,chb1,cvb1,cdb1,thr,wavetype);
ya10=conbindRGB(yar10,yag10,yab10);
zero10=zeror10+zerog10+zerob10
subplot(2,2,3);image(ya10);title('阈值10重构图像');axis square;
%阈值为20:统计“0”系数,合成图像及绘图
thr=20
[yar20,zeror20]=threshall(car3,chr3,cvr3,cdr3,chr2,cvr2,cdr2,chr1,cvr1,cdr1,thr,wavetype);
[yag20,zerog20]=threshall(cag3,chg3,cvg3,cdg3,chg2,cvg2,cdg2,chg1,cvg1,cdg1,thr,wavetype);
[yab20,zerob20]=threshall(cab3,chb3,cvb3,cdb3,chb2,cvb2,cdb2,chb1,cvb1,cdb1,thr,wavetype);
ya20=conbindRGB(yar20,yag20,yab20);
zero20=zeror20+zerog20+zerob20
subplot(2,2,4);image(ya20);title('阈值20重构图像');axis square;
%输出*.png文件
fn=strcat(fname,'_',wavetype,'_org.png');
imwrite(x,fn);
fn=strcat(fname,'_',wavetype,'_00.png');
imwrite(ya0,fn);
fn=strcat(fname,'_',wavetype,'_05.png');
imwrite(ya5,fn);
fn=strcat(fname,'_',wavetype,'_10.png');
imwrite(ya10,fn);
fn=strcat(fname,'_',wavetype,'_20.png');
imwrite(ya20,fn);
dispnumofzero(filename,wavetype,zero0,zero5,zero10,zero20);
%---------------------------256灰度图-----------------------------------------------------
%注意:为了便于看中间过程图像,用了256灰度表,而存盘时,不用色谱表,需要换算
case 'gray'
%绘制原图像
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)*16+(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 %unit8型转换成double型,a0max最大偏差量
subplot(4,5,17);image(ya0);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
subplot(4,5,18);image(ya5);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
subplot(4,5,19);image(ya10);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
subplot(4,5,20);image(ya20);colormap(map);title('阈值20重构图像');axis square;axis off;
zero20
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -