%%%%%%%%%%%%%%%%%%%1.加载图像%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%;
id = fopen('test.img','r');
header_data_read = fread(id, 100, 'uint8');% 读取头部数据,如果不保存图像,可以不读取头字节
header_size = 100; % 头字节的大小
fseek(id,header_size,'bof');%跳过头字节
nsize=512;%图像像素尺寸
srcImg = fread(id,[nsize,nsize],'uint16');%读取数据
srcImg=srcImg';
fclose(id); % 关闭文件
%%%%%%%%%%%%%%%%%%2.图像降噪%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 设置滤波器大小
filter_size = 5;
% 对图像进行高斯降噪
I_denoised = medfilt2(srcImg, [filter_size filter_size]);
% %%%%%%%%%%%%%%%%%%%3.多尺度小波分级%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %30723072图像-第三层水平信息有效,如果是10241024,就是第2层信息有效
% %%采用haar小波进行图像分解
%c为各层分解系数,s为各层分解系数长度,也就是大小.
%c的结构:c=[A(N)|H(N)|V(N)|D(N)|H(N-1)|V(N-1)|D(N-1)|H(N-2)|V(N-2)|D(N-2)|...|H(1)|V(1)|D(1)]
[c,s]=wavedec2(I_denoised,4,'haar');
siz=s(size(s,1),:);%返回带有ndims(X)元素的向量d中数组X的每个维的大小。
%第四层小波分解,提取小波系数
ca4=appcoef2(c,s,'haar',4);%提取多层小波分解结构C和S的第4层小波交换的近似系数
chd4=detcoef2('h',c,s,4);%利用多层小波分解结构C和S来提取图像第4层的水平分量
cvd4=detcoef2('v',c,s,4);%利用多层小波分解结构C和S来提取图像第4层的垂直分量
cdd4=detcoef2('d',c,s,4);%利用多层小波分解结构C和S来提取图像第4层的对角分量
%第三层小波分解,,提取小波系数
ca3=appcoef2(c,s,'haar',3);%提取多层小波分解结构C和S的第3层小波交换的近似系数
chd3=detcoef2('h',c,s,3);%利用多层小波分解结构C和S来提取图像第3层的水平分量
cvd3=detcoef2('v',c,s,3);%利用多层小波分解结构C和S来提取图像第3层的垂直分量
cdd3=detcoef2('d',c,s,3);%利用多层小波分解结构C和S来提取图像第3层的对角分量
%第二层小波分解,,提取小波系数
ca2=appcoef2(c,s,'haar',2);%提取多层小波分解结构C和S的第2层小波交换的近似系数
chd2=detcoef2('h',c,s,2);%利用多层小波分解结构C和S来提取图像第2层的水平分量
cvd2=detcoef2('v',c,s,2);%利用多层小波分解结构C和S来提取图像第2层的垂直分量
cdd2=detcoef2('d',c,s,2);%利用多层小波分解结构C和S来提取图像第2层的对角分量
%第一层小波分解,提取小波系数
chd1=detcoef2('h',c,s,1);%利用多层小波分解结构C和S来提取图像第1层的水平分量
cvd1=detcoef2('v',c,s,1);%利用多层小波分解结构C和S来提取图像第1层的垂直分量
cdd1=detcoef2('d',c,s,1);%利用多层小波分解结构C和S来提取图像第1层的对角分量
cal1=ca2+chd2+cvd2+cdd2;%叠加重构近似图像
cal=appcoef2(c,s,'haar',1);%提取多层小波分解结构C和S的第1层小波交换的近似系数
figure;
subplot(141),imshow(cal1,[]),title('1层分解的近似系数');
subplot(142),imshow(uint8(chd1),[]),title('水平分量');
subplot(143),imshow(uint8(cvd1),[]),title('垂直分量');
subplot(144),imshow(uint8(cdd1),[]),title('细节对角分量');
figure;
subplot(141),imshow(ca2,[]),title('2层分解的近似系数');
subplot(142),imshow(uint8(chd2),[]),title('水平分量');
subplot(143),imshow(uint8(cvd2),[]),title('垂直分量');
subplot(144),imshow(uint8(cdd2),[]),title('细节对角分量');
figure;
subplot(141),imshow(ca3,[]),title('3层分解的近似系数');
subplot(142),imshow(uint8(chd3),[]),title('水平分量');
subplot(143),imshow(uint8(cvd3),[]),title('垂直分量');
subplot(144),imshow(uint8(cdd3),[]),title('细节对角分量');
figure;
subplot(141),imshow(ca4,[]),title('4层分解的近似系数');%灰度值在0-1.5之间
subplot(142),imshow(uint8(chd4),[]),title('水平分量');
subplot(143),imshow(uint8(cvd4),[]),title('垂直分量');
subplot(144),imshow(uint8(cdd4),[]),title('细节对角分量');
%%%%%%%%%%%%%%%%%%%4.高频分量系数增强%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
a = 10.0; % 增强系数
b = 10.0; % 增强系数
chd2=chd2a;
chd3=chd3b;
figure;
subplot(121),imshow(chd2,[]),title('chd2');
subplot(122),imshow(chd3,[]),title('chd3');
%%%%%%%%%%%%%%%%%%%5.小波重构%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%利用upcoef2进行3层重构
hd3=upcoef2('h',chd3,'haar',3,siz);
% 利用upcoef2进行2层重构
hd2=upcoef2('h',chd2,'haar',2,siz);
A=srcImg+hd3+hd2;
% %降噪
A = medfilt2(A, [5 5]);
figure;
subplot(121),imshow(srcImg,[]),title('原图');
subplot(122),imshow(A,[]),title('分解重构的近似图形');
%%%%%%%%%%%%%%%%%%%保存图像%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fid=fopen('test_save.img','w');%打开文件
fwrite(fid, header_data_read, 'uint8'); % 写入头字节
fwrite(fid, A' ,'uint16'); % 写入成功的元素个数
fclose(fid);