7 May 2010

matlab数字图像小波分解(2)


更新:
指定function name和N,可以做n级小波分解

%% wavelet study demo2
% xenoszhATgmail.com
% 2010-05-06

function nWaveletTest(wname,n)

close all;

% get input image
[filename,pathname] = uigetfile('*.jpg;*.jpeg;*.bmp;*.png','Please pick up a picture:');
if ~filename
 disp('No input file.');
 return;
end
img = imread(strcat(pathname,filename));

% get grayscale image
img = rgb2gray(img); 

figure;
imshow(waveRec(img,n,wname));

end

%% sub function to reconstruct n level image
function recimage = waveRec(img,n,wname)
if nargin==3
 % get wavelet decomposition filter coefficients, using haar function
 [lod,hid] = wfilters(wname,'d'); 

 % n level wavelet decomposition
 [c,s] = wavedec2(img,n,lod,hid);
 
 % get the top level approximate signal
 an = appcoef2(c,s,wname,n);
 an = double2uint8(an);
 
 % reconstruct image recursively
 a = an;
 for i=n:-1:1
  curimg = curRec(a,c,s,i);
  a = curimg;
 end
 
 % return recontruct image
 recimage = curimg;
else
 error('please check the input');
end
end

%% sub function to get current level reconstruct image
function curRecimg = curRec(a,c,s,n)
if nargin==4
 h = detcoef2('h',c,s,n);
 v = detcoef2('v',c,s,n);
 d = detcoef2('d',c,s,n);
 a = imresize(a,size(h));
 curRecimg = [a,h; v,d];
else
 error('please check the input');
end
end
%% sub function to convert double image to uint8 image
function uint8image = double2uint8(dblimage)
if nargin==1 && isa(dblimage,'double')
 uint8image = 255*(dblimage-min(min(dblimage)))/(max(max(dblimage))-min(min(dblimage)));
 uint8image = uint8(uint8image);
else
 error('input image must be double');
end
end

1 comment :