[18]圖像增強-小波變換-matlab

%%%%%%%%%%%%%%%%%%%1.加載圖像%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%;
id = fopen('test.img','r');
header_data_read = fread(id, 100, 'uint8');% 讀取頭部數(shù)據(jù),如果不保存圖像,可以不讀取頭字節(jié)
header_size = 100; % 頭字節(jié)的大小
fseek(id,header_size,'bof');%跳過頭字節(jié)
nsize=512;%圖像像素尺寸
srcImg = fread(id,[nsize,nsize],'uint16');%讀取數(shù)據(jù)
srcImg=srcImg';
fclose(id); % 關(guān)閉文件

%%%%%%%%%%%%%%%%%%2.圖像降噪%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 設(shè)置濾波器大小
filter_size = 5;
% 對圖像進(jìn)行高斯降噪
I_denoised = medfilt2(srcImg, [filter_size filter_size]);

% %%%%%%%%%%%%%%%%%%%3.多尺度小波分級%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %30723072圖像-第三層水平信息有效,如果是10241024,就是第2層信息有效
% %%采用haar小波進(jìn)行圖像分解
%c為各層分解系數(shù),s為各層分解系數(shù)長度,也就是大小.
%c的結(jié)構(gòu):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中數(shù)組X的每個維的大小。

%第四層小波分解,提取小波系數(shù)
ca4=appcoef2(c,s,'haar',4);%提取多層小波分解結(jié)構(gòu)C和S的第4層小波交換的近似系數(shù)
chd4=detcoef2('h',c,s,4);%利用多層小波分解結(jié)構(gòu)C和S來提取圖像第4層的水平分量
cvd4=detcoef2('v',c,s,4);%利用多層小波分解結(jié)構(gòu)C和S來提取圖像第4層的垂直分量
cdd4=detcoef2('d',c,s,4);%利用多層小波分解結(jié)構(gòu)C和S來提取圖像第4層的對角分量
%第三層小波分解,,提取小波系數(shù)
ca3=appcoef2(c,s,'haar',3);%提取多層小波分解結(jié)構(gòu)C和S的第3層小波交換的近似系數(shù)
chd3=detcoef2('h',c,s,3);%利用多層小波分解結(jié)構(gòu)C和S來提取圖像第3層的水平分量
cvd3=detcoef2('v',c,s,3);%利用多層小波分解結(jié)構(gòu)C和S來提取圖像第3層的垂直分量
cdd3=detcoef2('d',c,s,3);%利用多層小波分解結(jié)構(gòu)C和S來提取圖像第3層的對角分量
%第二層小波分解,,提取小波系數(shù)
ca2=appcoef2(c,s,'haar',2);%提取多層小波分解結(jié)構(gòu)C和S的第2層小波交換的近似系數(shù)
chd2=detcoef2('h',c,s,2);%利用多層小波分解結(jié)構(gòu)C和S來提取圖像第2層的水平分量
cvd2=detcoef2('v',c,s,2);%利用多層小波分解結(jié)構(gòu)C和S來提取圖像第2層的垂直分量
cdd2=detcoef2('d',c,s,2);%利用多層小波分解結(jié)構(gòu)C和S來提取圖像第2層的對角分量
%第一層小波分解,提取小波系數(shù)
chd1=detcoef2('h',c,s,1);%利用多層小波分解結(jié)構(gòu)C和S來提取圖像第1層的水平分量
cvd1=detcoef2('v',c,s,1);%利用多層小波分解結(jié)構(gòu)C和S來提取圖像第1層的垂直分量
cdd1=detcoef2('d',c,s,1);%利用多層小波分解結(jié)構(gòu)C和S來提取圖像第1層的對角分量
cal1=ca2+chd2+cvd2+cdd2;%疊加重構(gòu)近似圖像
cal=appcoef2(c,s,'haar',1);%提取多層小波分解結(jié)構(gòu)C和S的第1層小波交換的近似系數(shù)
figure;
subplot(141),imshow(cal1,[]),title('1層分解的近似系數(shù)');
subplot(142),imshow(uint8(chd1),[]),title('水平分量');
subplot(143),imshow(uint8(cvd1),[]),title('垂直分量');
subplot(144),imshow(uint8(cdd1),[]),title('細(xì)節(jié)對角分量');
figure;
subplot(141),imshow(ca2,[]),title('2層分解的近似系數(shù)');
subplot(142),imshow(uint8(chd2),[]),title('水平分量');
subplot(143),imshow(uint8(cvd2),[]),title('垂直分量');
subplot(144),imshow(uint8(cdd2),[]),title('細(xì)節(jié)對角分量');
figure;
subplot(141),imshow(ca3,[]),title('3層分解的近似系數(shù)');
subplot(142),imshow(uint8(chd3),[]),title('水平分量');
subplot(143),imshow(uint8(cvd3),[]),title('垂直分量');
subplot(144),imshow(uint8(cdd3),[]),title('細(xì)節(jié)對角分量');
figure;
subplot(141),imshow(ca4,[]),title('4層分解的近似系數(shù)');%灰度值在0-1.5之間
subplot(142),imshow(uint8(chd4),[]),title('水平分量');
subplot(143),imshow(uint8(cvd4),[]),title('垂直分量');
subplot(144),imshow(uint8(cdd4),[]),title('細(xì)節(jié)對角分量');

%%%%%%%%%%%%%%%%%%%4.高頻分量系數(shù)增強%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
a = 10.0; % 增強系數(shù)
b = 10.0; % 增強系數(shù)
chd2=chd2a;
chd3=chd3
b;
figure;
subplot(121),imshow(chd2,[]),title('chd2');
subplot(122),imshow(chd3,[]),title('chd3');

%%%%%%%%%%%%%%%%%%%5.小波重構(gòu)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%利用upcoef2進(jìn)行3層重構(gòu)
hd3=upcoef2('h',chd3,'haar',3,siz);
% 利用upcoef2進(jìn)行2層重構(gòu)
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('分解重構(gòu)的近似圖形');

%%%%%%%%%%%%%%%%%%%保存圖像%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fid=fopen('test_save.img','w');%打開文件
fwrite(fid, header_data_read, 'uint8'); % 寫入頭字節(jié)
fwrite(fid, A' ,'uint16'); % 寫入成功的元素個數(shù)
fclose(fid);

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容