wp2dcomp.m
上传用户:haiyisale
上传日期:2013-01-09
资源大小:3246k
文件大小:20k
源码类别:

波变换

开发平台:

Matlab

  1. function varargout = wp2dcomp(option,varargin)
  2. %WP2DCOMP Wavelet packets 2-D compression.
  3. %   VARARGOUT = WP2DCOMP(OPTION,VARARGIN)
  4. %   M. Misiti, Y. Misiti, G. Oppenheim, J.M. Poggi 12-Mar-96.
  5. %   Last Revision: 12-Mar-2003.
  6. %   Copyright 1995-2004 The MathWorks, Inc.
  7. %   $Revision: 1.18.4.2 $ $Date: 2004/03/15 22:43:09 $
  8. % Memory Blocks of stored values.
  9. %================================
  10. % MB1.
  11. %-----
  12. n_param_anal   = 'WP2D_Par_Anal';
  13. ind_img_name   = 1;
  14. ind_wav_name   = 2;
  15. ind_lev_anal   = 3;
  16. ind_ent_anal   = 4;
  17. ind_ent_par    = 5;
  18. ind_img_size   = 6;
  19. ind_img_t_name = 7;
  20. ind_act_option = 8;
  21. ind_thr_val    = 9;
  22. nb1_stored     = 9;
  23. % MB2 (main window).
  24. %-------------------
  25. n_wp_utils = 'WP_Utils';
  26. ind_tree_lin  = 1;
  27. ind_tree_txt  = 2;
  28. ind_type_txt  = 3;
  29. ind_sel_nodes = 4;
  30. ind_gra_area  = 5;
  31. ind_nb_colors = 6;
  32. nb2_stored    = 6;
  33. % MB3.
  34. %-----
  35. n_structures = 'Structures';
  36. ind_tree_st  = 1;
  37. ind_data_st  = 2;
  38. nb3_stored   = 2;
  39. % MB1 (local).
  40. %-------------
  41. n_misc_loc = [ 'MB1_' mfilename];
  42. ind_sav_menus  = 1;
  43. ind_status     = 2;
  44. ind_win_caller = 3;
  45. ind_axe_datas  = 4;
  46. ind_hdl_datas  = 5;
  47. nbLOC_1_stored = 5;
  48. % MB2 (local).
  49. %-------------
  50. n_thrDATA = 'thrDATA';
  51. ind_value = 1;
  52. nbLOC_2_stored = 1;
  53. if ~isequal(option,'create') , win_compress = varargin{1}; end
  54. switch option
  55.     case 'create'
  56.         % Get Globals.
  57.         %-------------
  58.         [Def_Btn_Height,Y_Spacing,Def_FraBkColor] = ...
  59.             mextglob('get','Def_Btn_Height','Y_Spacing','Def_FraBkColor');
  60.         % Calling figure.
  61.         %----------------
  62.         win_caller = varargin{1};
  63.         str_win_caller = sprintf('%.0f',win_caller);
  64.         % Window initialization.
  65.         %----------------------
  66.         win_name = 'Wavelet Packet 2-D  --  Compression';
  67.         [win_compress,pos_win,win_units,str_win_compress,...
  68.                 frame0,pos_frame0,Pos_Graphic_Area,pus_close] = ...
  69.                     wfigmngr('create',win_name,'','ExtFig_CompDeno', ...
  70.                         strvcat(mfilename,'cond'),1,1,0);
  71.         set(win_compress,'userdata',win_caller);
  72.         if nargout>0 , varargout{1} = win_compress; end
  73. % Add Help for Tool.
  74. %------------------
  75. wfighelp('addHelpTool',win_compress,'Image Compression','WP2D_COMP_GUI');
  76. % Add Help Item.
  77. %----------------
  78. wfighelp('addHelpItem',win_compress,'Compression Procedure','COMP_PROCEDURE');
  79. wfighelp('addHelpItem',win_compress,'Available Methods','COMP_DENO_METHODS');
  80.         % Menu construction for current figure.
  81.         %--------------------------------------
  82. m_save  = wfigmngr('getmenus',win_compress,'save');
  83.         sav_menus(1) = uimenu(m_save,...
  84.                               'Label','Compressed &Image ',...
  85.                               'Position',1,                   ...
  86.                               'Enable','Off',                 ...
  87.                               'Callback',                     ...
  88.                               [mfilename '(''save_synt'','    ...
  89.                                       str_win_compress ');']  ...
  90.                               );
  91.         sav_menus(2) = uimenu(m_save,...
  92.                               'Label','&Decomposition ', ...
  93.                               'Position',2,                  ...
  94.                               'Enable','Off',                ...
  95.                               'Callback',                    ...
  96.                               [mfilename '(''save_dec'','    ...
  97.                                     str_win_compress ');']   ...
  98.                               );
  99.         % Begin waiting.
  100.         %---------------
  101.         wwaiting('msg',win_compress,'Wait ... initialization');
  102.         % Getting variables from wp2dtool figure memory block.
  103.         %-----------------------------------------------------
  104.         [WP_Tree,WP_Data] = wmemtool('rmb',win_caller,n_structures,...
  105.                                            ind_tree_st,ind_data_st);
  106.         depth = treedpth(WP_Tree);
  107.         [Img_Name,Wav_Name,Img_Size,Ent_Nam,Ent_Par] =  ...
  108.                         wmemtool('rmb',win_caller,n_param_anal,   ...
  109.                                        ind_img_name,ind_wav_name, ...
  110.                                        ind_img_size,              ...
  111.                                        ind_ent_anal,ind_ent_par);
  112.         Wav_Fam = wavemngr('fam_num',Wav_Name);
  113.         isBior = wavemngr('isbior',Wav_Fam);
  114.         % General graphical parameters initialization.
  115.         %---------------------------------------------
  116.         dy = Y_Spacing;
  117.         % Command part of the window.
  118.         %============================
  119.         % Data, Wavelet and Level parameters.
  120.         %------------------------------------
  121.         xlocINI = pos_frame0([1 3]);
  122.         ytopINI = pos_win(4)-dy;
  123.         toolPos = utanapar('create_copy',win_compress, ...
  124.                     {'xloc',xlocINI,'top',ytopINI},...
  125.                     {'n_s',{Img_Name,Img_Size},'wav',Wav_Name,'lev',depth} ...
  126.                     );
  127.         % Entropy parameters.
  128.         %--------------------
  129.         ytopENT = toolPos(2)-dy;
  130.         toolPos = utentpar('create_copy',win_compress, ...
  131.                     {'xloc',xlocINI,'top',ytopENT,...
  132.                      'ent',{Ent_Nam,Ent_Par}} ...
  133.                     );
  134.         % Global Compression tool.
  135.         %-------------------------
  136.         ytopTHR = toolPos(2)-4*dy;
  137.         utthrgbl('create',win_compress,'toolOPT','wp2dcomp', ...
  138.                  'xloc',xlocINI,'top',ytopTHR, ...
  139.                  'isbior',isBior,   ...
  140.                  'caller',mfilename ...
  141.                  );
  142.         % Adding colormap GUI.
  143.         %---------------------
  144.         pop_pal_caller = cbcolmap('get',win_caller,'pop_pal');
  145.         prop_pal = get(pop_pal_caller,{'String','Value','Userdata'});
  146.         utcolmap('create',win_compress, ...
  147.                  'xloc',xlocINI, ...
  148.                  'bkcolor',Def_FraBkColor, ...
  149.                  'enable','on');
  150.         pop_pal_loc = cbcolmap('get',win_compress,'pop_pal');
  151.         set(pop_pal_loc,'String',prop_pal{1},'Value',prop_pal{2}, ...
  152.                         'Userdata',prop_pal{3});
  153.         set(win_compress,'Colormap',get(win_caller,'Colormap'));        
  154.         % Axes Construction.
  155.         %===================
  156.         % General graphical parameters initialization.
  157.         %--------------------------------------------
  158.         bdx     = 0.08*pos_win(3);
  159.         ecx     = 0.04*pos_win(3);
  160.         bdy     = 0.06*pos_win(4);
  161.         ecy     = 0.03*pos_win(4);
  162.         y_graph = 2*Def_Btn_Height+dy;
  163.         h_graph = pos_frame0(4)-y_graph-Def_Btn_Height;
  164.         w_graph = pos_frame0(1);
  165.         % Building axes for original image.
  166.         %----------------------------------
  167.         x_axe  = bdx;
  168.         w_axe  = (w_graph-ecx-3*bdx/2)/2;
  169.         h_axe  = (h_graph-3*bdy)/2;
  170.         y_axe  = y_graph+h_graph-h_axe-bdy;
  171.         cx_ori = x_axe+w_axe/2;
  172.         cy_ori = y_axe+h_axe/2;
  173.         cx_cmp = cx_ori+w_axe+ecx;
  174.         cy_cmp = cy_ori;
  175.         [w_used,h_used] = wpropimg(Img_Size,w_axe,h_axe,'pixels');
  176.         pos_axe      = [cx_ori-w_used/2 cy_ori-h_used/2 w_used h_used];
  177.         axe_datas(1) = axes(...
  178.                             'Parent',win_compress,...
  179.                             'Units',win_units,...
  180.                             'Position',pos_axe,...
  181.                             'Drawmode','fast',...
  182.                             'Box','On');
  183.         axe_orig = axe_datas(1);
  184.         % Displaying original image.
  185.         %---------------------------
  186.         cdata = get(wp2ddraw('r_orig',win_caller),'Cdata');
  187.         hdl_datas    = [NaN ; NaN];
  188.         hdl_datas(1) = image([1 Img_Size(1)],[1,Img_Size(2)],cdata, ...
  189.                              'Parent',axe_orig);
  190.         clear cdata
  191.         wtitle('Original image','Parent',axe_orig);
  192.         % Building axes for compressed image.
  193.         %------------------------------------
  194.         pos_axe = [cx_cmp-w_used/2 cy_cmp-h_used/2 w_used h_used];
  195.         xylim = get(axe_orig,{'Xlim','Ylim'});
  196.         axe_datas(2) = axes(...
  197.                             'Parent',win_compress,...
  198.                             'Units',win_units,...
  199.                             'Position',pos_axe,...
  200.                             'Drawmode','fast',...
  201.                             'Visible','off',...
  202.                             'Box','On',...
  203.                             'Xlim',xylim{1},...
  204.                             'Ylim',xylim{2});
  205.         axe_comp = axe_datas(2);
  206.         % Initializing global threshold.
  207.         %-------------------------------
  208.         [valTHR,maxTHR,thresVALUES,rl2SCR,n0SCR] = ...
  209.             wp2dcomp('compute_GBL_THR',win_compress,win_caller);
  210.         utthrgbl('set',win_compress,'thrBOUNDS',[0,valTHR,maxTHR]);
  211.         % Displaying perfos & legend.
  212.         %----------------------------
  213.         x_axe     = bdx;
  214.         y_axe     = y_graph+bdy;
  215.         pos_axe_perfo = [x_axe y_axe w_axe h_axe];
  216.         x_axe     = bdx+w_axe+ecx;
  217.         y_axe     = y_graph+w_axe/3+bdy;
  218.         h_axe     = w_axe/3;
  219.         pos_axe_legend = [x_axe y_axe w_axe h_axe];
  220.         utthrgbl('displayPerf',win_compress, ...
  221.                   pos_axe_perfo,pos_axe_legend,thresVALUES,n0SCR,rl2SCR,valTHR);
  222.         [perfl2,perf0] = utthrgbl('getPerfo',win_compress);
  223.         utthrgbl('set',win_compress,'perfo',[perfl2,perf0]);
  224.         drawnow
  225.         % Memory blocks update.
  226.         %----------------------
  227.         utthrgbl('set',win_compress,'handleORI',hdl_datas(1));
  228.         wmemtool('ini',win_compress,n_misc_loc,nbLOC_1_stored);
  229.         wmemtool('wmb',win_compress,n_misc_loc,  ...
  230.                        ind_status,0,             ...
  231.                        ind_sav_menus,sav_menus,  ...
  232.                        ind_win_caller,win_caller,...
  233.                        ind_axe_datas,axe_datas,  ...
  234.                        ind_hdl_datas,hdl_datas   ...
  235.                        );
  236.         wmemtool('ini',win_compress,n_thrDATA,nbLOC_2_stored);
  237.         % Axes attachment.
  238.         %-----------------
  239.         axe_cmd = [axe_orig axe_comp];
  240.         dynvtool('init',win_compress,[],axe_cmd,[],[1 1]);
  241.         % Setting units to normalized.
  242.         %-----------------------------
  243.         wfigmngr('normalize',win_compress);
  244.         % End waiting.
  245.         %-------------
  246.         wwaiting('off',win_compress);
  247.  
  248.     case 'compress'
  249.         % Waiting message.
  250.         %-----------------
  251.         wwaiting('msg',win_compress,'Wait ... computing');
  252.         % Handles.
  253.         %---------
  254.         wp2dcomp('clear_GRAPHICS',win_compress);
  255.         [axe_datas,hdl_datas] = wmemtool('rmb',win_compress,n_misc_loc, ...
  256.                                                ind_axe_datas,ind_hdl_datas);
  257.         axe_orig = axe_datas(1);
  258.         axe_comp = axe_datas(2);
  259.         % Getting memory blocks.
  260.         %-----------------------
  261.         win_caller = wmemtool('rmb',win_compress,n_misc_loc,ind_win_caller);
  262.         [WP_Tree,WP_Data]   = wmemtool('rmb',win_caller,n_structures,...
  263.                                              ind_tree_st,ind_data_st);
  264.         [Img_Size,Wav_Name] = wmemtool('rmb',win_caller,n_param_anal,...
  265.                                              ind_img_size,ind_wav_name);
  266.         isBior = wavemngr('isbior',Wav_Name);
  267.         % Compression.
  268.         %-------------
  269.         valTHR = utthrgbl('get',win_compress,'valthr');
  270.         thrParams = {'h','nobest',valTHR,1};
  271.         if isBior
  272.             if isa(WP_Tree,'wptree')
  273.                 [C_Img,C_Tree,perf0] = wpdencmp(WP_Tree,thrParams{:});
  274.                 C_Data = [];
  275.             else
  276.                 [C_Img,C_Tree,C_Data,perf0] = wpdencmp(WP_Tree,WP_Data,...
  277.                                                        thrParams{:});
  278.             end
  279.             img_orig = hdl_datas(1);
  280.             Img_Anal = get(img_orig,'Cdata');
  281.             normimg  = norm(Img_Anal(:));
  282.             if normimg<eps
  283.                 perfl2 = 100;
  284.             else
  285.                 perfl2 = 100*(norm(C_Img(:))/normimg)^2;
  286.             end
  287.             topTitle = 'Energy ratio ';
  288.         else
  289.             if isa(WP_Tree,'wptree')
  290.                 [C_Img,C_Tree] = wpdencmp(WP_Tree,thrParams{:});
  291.                 C_Data = [];
  292.             else
  293.                 [C_Img,C_Tree,C_Data] = wpdencmp(WP_Tree,WP_Data,thrParams{:});
  294.             end
  295.             [perfl2,perf0] = utthrgbl('getPerfo',win_compress);
  296.             topTitle = 'Retained energy ';
  297.         end
  298.         % Displaying compressed image.
  299.         %------------------------------
  300.         axes(axe_comp);
  301.         hdl_comp = hdl_datas(2);
  302.         if ishandle(hdl_comp)
  303.             set(hdl_comp,'Cdata',C_Img,'Visible','on');
  304.         else
  305.             hdl_comp = image([1 Img_Size(1)],[1,Img_Size(2)],C_Img, ...
  306.                               'Parent',axe_comp);
  307.             hdl_datas(2) = hdl_comp;
  308.             utthrgbl('set',win_compress,'handleTHR',hdl_datas(2));
  309.             wmemtool('wmb',win_compress,n_misc_loc,ind_hdl_datas,hdl_datas);
  310.         end
  311.         xylim = get(axe_orig,{'Xlim','Ylim'});
  312.         set(axe_comp,'Xlim',xylim{1},'Ylim',xylim{2},'Visible','on');
  313.         % Set a text as a super title.
  314.         %-----------------------------
  315.         wtitle('Compressed image','Parent',axe_comp);
  316.         strPerfo = [topTitle num2str(perfl2,'%5.2f') ...
  317.                     ' % -- Zeros ' num2str(perf0,'%5.2f') ' %'];
  318.         wtxttitl(axe_comp,strPerfo);
  319.         % Memory blocks update.
  320.         %----------------------
  321.         wmemtool('wmb',win_compress,n_thrDATA,ind_value, ...
  322.                  {C_Img,C_Tree,C_Data,valTHR});
  323.         wp2dcomp('enable_menus',win_compress,'on');
  324.         % End waiting.
  325.         %-------------
  326.         wwaiting('off',win_compress);
  327.     case 'compute_GBL_THR'
  328.         win_caller = varargin{2};
  329.         [numMeth,meth,sliVal] = utthrgbl('get_GBL_par',win_compress);
  330.         [WP_Tree,WP_Data] = wmemtool('rmb',win_caller,n_structures,...
  331.                                            ind_tree_st,ind_data_st);
  332.         thrFLAGS = 'wp2dcompGBL';
  333.         switch numMeth
  334.           case {1,3}
  335.             [valTHR,maxTHR,thresVALUES,rl2SCR,n0SCR] = ...
  336.                 wthrmngr(thrFLAGS,meth,WP_Tree,WP_Data);
  337.             if nargout==1 , varargout = {valTHR};
  338.             else          , varargout = {valTHR,maxTHR,thresVALUES,rl2SCR,n0SCR};
  339.             end
  340.           case 2
  341.             sig    = get(wp2ddraw('r_orig',win_caller),'Cdata');
  342.             valTHR = wthrmngr(thrFLAGS,meth,sig);
  343.             if isa(WP_Tree,'wptree')
  344.                 cfs = read(WP_Tree,'allcfs');
  345.             else
  346.                 cfs = wdatamgr('rallcfs',WP_Data);
  347.             end
  348.             maxTHR = max(abs(cfs));
  349.             valTHR = min(valTHR,maxTHR);
  350.             varargout = {valTHR};
  351.         end
  352.     case 'update_GBL_meth'
  353.         wp2dcomp('clear_GRAPHICS',win_compress);
  354.         win_caller = wmemtool('rmb',win_compress,n_misc_loc,ind_win_caller);
  355.         valTHR = wp2dcomp('compute_GBL_THR',win_compress,win_caller);
  356.         utthrgbl('update_GBL_meth',win_compress,valTHR);
  357.     case 'clear_GRAPHICS'
  358.         status = wmemtool('rmb',win_compress,n_misc_loc,ind_status);
  359.         if status == 0 , return; end
  360.         axe_datas = wmemtool('rmb',win_compress,n_misc_loc,ind_axe_datas);
  361.         wp2dcomp('enable_menus',win_compress,'off');
  362.         axe_comp = axe_datas(2);
  363.         set(findobj(axe_comp),'Visible','off');       
  364.         drawnow
  365.     case 'enable_menus'
  366.         enaVal = varargin{2};
  367.         sav_menus = wmemtool('rmb',win_compress,n_misc_loc,ind_sav_menus);
  368.         set(sav_menus,'Enable',enaVal);
  369.         utthrgbl('enable_tog_res',win_compress,enaVal);
  370.         if strncmpi(enaVal,'on',2) , status = 1; else , status = 0; end
  371.         wmemtool('wmb',win_compress,n_misc_loc,ind_status,status);
  372.     case 'save_synt'
  373.         % Testing file.
  374.         %--------------
  375.         [filename,pathname,ok] = utguidiv('test_save',win_compress, ...
  376.                                      '*.mat','Save Compressed Image');
  377.         if ~ok, return; end
  378.         % Begin waiting.
  379.         %--------------
  380.         wwaiting('msg',win_compress,'Wait ... saving');
  381.         % Getting Analysis values.
  382.         %-------------------------
  383.         win_caller = wmemtool('rmb',win_compress,n_misc_loc,ind_win_caller);
  384.         wname = wmemtool('rmb',win_caller,n_param_anal,ind_wav_name);
  385.         map = cbcolmap('get',win_caller,'self_pal');
  386.         if isempty(map)
  387.             nb_colors = wmemtool('rmb',win_caller,n_wp_utils,ind_nb_colors);
  388.             map = pink(nb_colors);
  389.         end
  390.         thrDATA = wmemtool('rmb',win_compress,n_thrDATA,ind_value);
  391.         X = round(thrDATA{1});
  392.         valTHR = thrDATA{4};
  393.         % Saving file.
  394.         %--------------
  395.         [name,ext] = strtok(filename,'.');
  396.         if isempty(ext) | isequal(ext,'.')
  397.             ext = '.mat'; filename = [name ext];
  398.         end
  399.         saveStr = {'X','map','valTHR','wname'};
  400.         wwaiting('off',win_compress);
  401.         try
  402.           save([pathname filename],saveStr{:});
  403.         catch
  404.           errargt(mfilename,'Save FAILED !','msg');
  405.         end
  406.     case 'save_dec'
  407.         % Testing file.
  408.         %--------------
  409.         [filename,pathname,ok] = utguidiv('test_save',win_compress, ...
  410.                                    '*.wp2','Save Wavelet Packet Analysis (2D)');
  411.         if ~ok, return; end
  412.         % Begin waiting.
  413.         %--------------
  414.         wwaiting('msg',win_compress,'Wait ... saving decomposition');
  415.         % Getting Analysis values.
  416.         %-------------------------
  417.         win_caller = wmemtool('rmb',win_compress,n_misc_loc,ind_win_caller);
  418.         data_name  = wmemtool('rmb',win_caller,n_param_anal,ind_img_name);
  419.         map = cbcolmap('get',win_caller,'self_pal');
  420.         if isempty(map)
  421.             nb_colors = wmemtool('rmb',win_caller,n_wp_utils,ind_nb_colors);
  422.             map = pink(nb_colors);
  423.         end
  424.         thrDATA = wmemtool('rmb',win_compress,n_thrDATA,ind_value);
  425.         tree_struct = thrDATA{2};
  426.         data_struct = thrDATA{3};
  427.         valTHR = thrDATA{4};
  428.         % Saving file.
  429.         %--------------
  430.         [name,ext] = strtok(filename,'.');
  431.         if isempty(ext) | isequal(ext,'.')
  432.             ext = '.wp2'; filename = [name ext];
  433.         end
  434.         if isa(tree_struct,'wptree')
  435.           saveStr = {'tree_struct','map','data_name','valTHR'};
  436.         else
  437.           saveStr = {'tree_struct','data_struct','map','data_name','valTHR'};
  438.         end
  439.         wwaiting('off',win_compress);
  440.         try
  441.           save([pathname filename],saveStr{:});
  442.         catch
  443.           errargt(mfilename,'Save FAILED !','msg');
  444.         end
  445.     case 'close'
  446.         [status,win_caller] = wmemtool('rmb',win_compress,n_misc_loc, ...
  447.                                              ind_status,ind_win_caller);
  448.         if status==1
  449.             % Test for Updating.
  450.             %--------------------
  451.             status = wwaitans(win_compress,...
  452.                               'Update the synthesized image ?',2,'cancel');
  453.         end
  454.         switch status
  455.             case 1
  456.               wwaiting('msg',win_compress,'Wait ... computing');
  457.               thrDATA = wmemtool('rmb',win_compress,n_thrDATA,ind_value);
  458.               valTHR  = thrDATA{4};
  459.               wmemtool('wmb',win_caller,n_param_anal,ind_thr_val,valTHR);
  460.               hdl_datas = wmemtool('rmb',win_compress,n_misc_loc,ind_hdl_datas);
  461.               img_comp  = hdl_datas(2);
  462.               wp2dmngr('return_comp',win_caller,status,img_comp);
  463.               wwaiting('off',win_compress);
  464.             case 0 , wp2dmngr('return_comp',win_caller,status);
  465.         end
  466.         if nargout>0 , varargout{1} = status; end
  467.     otherwise
  468.         errargt(mfilename,'Unknown Option','msg');
  469.         error('*');
  470. end