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

波变换

开发平台:

Matlab

  1. function out1 = wp2dmngr(option,win_wptool,in3,in4,in5,in6,in7)
  2. %WP2DMNGR Wavelet packets 2-D general manager.
  3. %   OUT1 = WP2DMNGR(OPTION,WIN_WPTOOL,IN3,IN4,IN5,IN6,IN7)
  4. %   M. Misiti, Y. Misiti, G. Oppenheim, J.M. Poggi 12-Mar-96.
  5. %   Last Revision: 05-May-2003.
  6. %   Copyright 1995-2004 The MathWorks, Inc.
  7. %   $Revision: 1.20.4.2 $
  8. % Default values.
  9. %----------------
  10. max_lev_anal = 5;
  11. default_nbcolors = 255;
  12. % Memory Blocks of stored values.
  13. %================================
  14. % MB0.
  15. %-----
  16. n_InfoInit   = 'WP2D_InfoInit';
  17. ind_filename = 1;
  18. ind_pathname = 2;
  19. nb0_stored   = 2;
  20. % MB1.
  21. %-----
  22. n_param_anal   = 'WP2D_Par_Anal';
  23. ind_img_name   = 1;
  24. ind_wav_name   = 2;
  25. ind_lev_anal   = 3;
  26. ind_ent_anal   = 4;
  27. ind_ent_par    = 5;
  28. ind_img_size   = 6;
  29. ind_img_t_name = 7;
  30. ind_act_option = 8;
  31. ind_thr_val    = 9;
  32. nb1_stored     = 9;
  33. % MB2.
  34. %-----
  35. n_wp_utils = 'WP_Utils';
  36. ind_tree_lin  = 1;
  37. ind_tree_txt  = 2;
  38. ind_type_txt  = 3;
  39. ind_sel_nodes = 4;
  40. ind_gra_area  = 5;
  41. ind_nb_colors = 6;
  42. nb2_stored    = 6;
  43. % MB3.
  44. %-----
  45. n_structures = 'Structures';
  46. ind_tree_st  = 1;
  47. ind_data_st  = 2;
  48. nb3_stored   = 2;
  49. % MB4.
  50. %-----
  51. n_sav_struct    = 'Sav_Struct';
  52. ind_sav_tree_st = 1;
  53. ind_sav_data_st = 2;
  54. nb4_stored      = 2;
  55. switch option
  56.     case 'load_img'
  57. % Loading file.
  58.     %--------------
  59.     imgFileType = ['*.mat;*.bmp;*.hdf;*.jpg;' ...
  60.             '*.jpeg;*.pcx;*.tif;*.tiff;*.gif'];
  61.     [imgInfos,Img_Anal,map,ok] = utguidiv('load_img',win_wptool, ...
  62.         imgFileType,'Load Image',default_nbcolors);
  63.     if ~ok, return; end
  64.     % Cleaning. 
  65. %----------
  66. wwaiting('msg',win_wptool,'Wait ... cleaning');
  67. wp2dutil('clean',win_wptool,option,'');
  68.     % Setting Analysis parameters.
  69. %-----------------------------
  70. NB_ColorsInPal = size(map,1);
  71.         wmemtool('wmb',win_wptool,n_param_anal, ...
  72.                        ind_act_option,option,  ...
  73.        ind_img_name,imgInfos.name, ...
  74.        ind_img_t_name,imgInfos.true_name, ...
  75.        ind_img_size,imgInfos.size ...
  76.                         );
  77.         wmemtool('wmb',win_wptool,n_InfoInit,  ...
  78.        ind_filename,imgInfos.filename, ...
  79.        ind_pathname,imgInfos.pathname  ...
  80.                        );
  81.         wmemtool('wmb',win_wptool,n_wp_utils,  ...
  82.                        ind_nb_colors,NB_ColorsInPal);
  83.         % Setting GUI values.
  84.         %--------------------
  85.         wp2dutil('set_gui',win_wptool,option,'');
  86. if imgInfos.self_map , arg = map; else , arg = []; end
  87.         cbcolmap('set',win_wptool,'pal',{'pink',NB_ColorsInPal,'self',arg});
  88.         % Drawing.
  89.         %---------
  90.         wp2ddraw('sig',win_wptool,Img_Anal);
  91.         % Setting enabled values.
  92.         %------------------------
  93.         wp2dutil('enable',win_wptool,option);
  94.         % End waiting.
  95.         %-------------
  96.         wwaiting('off',win_wptool);
  97.     case 'load_dec'
  98.         switch nargin
  99.             case 2
  100.                 % Testing file.
  101.                 %--------------
  102.                 winTitle = 'Load Wavelet Packet Analysis (2D)';
  103.                 fileMask = {...
  104.                     '*.wp2;*.mat' , 'Decomposition  (*.wp2;*.mat)';
  105.                     '*.*','All Files (*.*)'};                
  106.                 [filename,pathname,ok] = utguidiv('load_wpdec',win_wptool, ...
  107.                                            fileMask,winTitle,4);
  108.                 if ~ok, return; end
  109.                 % Loading file.
  110.                 %--------------
  111.                 load([pathname filename],'-mat');
  112.                 if isa(tree_struct,'wptree') , data_struct = []; end
  113.                 if ~exist('map','var'), map = pink(default_nbcolors); end
  114.                 if ~exist('data_name','var') , data_name = 'no name'; end
  115.             case {3,4}
  116.                 filename    = '';
  117.                 pathname    = '';
  118.                 tree_struct = in3;
  119.                 if nargin==3
  120.                     if isa(tree_struct,'wptree')
  121.                         data_struct = [];
  122.                     else
  123.                         wwaiting('off',win_wptool);
  124.                         msg = strvcat(['The decomposition is not a'...
  125. ' valid two dimensional analysis'],' ');
  126.                         errordlg(msg,...
  127.                                 'Load Wavelet Packet Analysis (2D)','modal');
  128.                         return
  129.                     end
  130.                 else
  131.                     data_struct = in4;
  132.                 end
  133.                 map       = pink(default_nbcolors);
  134.                 data_name = 'input var';
  135.         end
  136.         % Cleaning.
  137.         %----------
  138.         wp2dutil('clean',win_wptool,option);
  139.         % Getting Analysis parameters.
  140.         %-----------------------------
  141.         if isa(tree_struct,'wptree')
  142.             [Wav_Name,Ent_Name,Ent_Par,Img_Size] = ...
  143.                 read(tree_struct,'wavname','entname','entpar','sizes',0);
  144.             Img_Size = fliplr(Img_Size);
  145.         else
  146.             Wav_Name = wdatamgr('read_wave',data_struct);
  147.             [Ent_Name,Ent_Par] = wdatamgr('read_tp_ent',data_struct);
  148.             sizes    = wdatamgr('rsizes',data_struct);
  149.             Img_Size = fliplr(sizes(:,1)');
  150.         end
  151.         Lev_Anal     = treedpth(tree_struct);
  152.         Img_Name       = data_name;
  153.         NB_ColorsInPal = size(map,1);
  154.         % Setting Analysis parameters
  155.         %-----------------------------
  156.         wmemtool('wmb',win_wptool,n_param_anal, ...
  157.                        ind_act_option,option,   ...
  158.                        ind_img_name,Img_Name,   ...
  159.                        ind_wav_name,Wav_Name,   ...
  160.                        ind_lev_anal,Lev_Anal,   ...
  161.                        ind_img_size,Img_Size,   ...
  162.                        ind_ent_anal,Ent_Name,   ...
  163.                        ind_ent_par,Ent_Par      ...
  164.                        );
  165.         wmemtool('wmb',win_wptool,n_InfoInit, ...
  166.                        ind_filename,filename, ...
  167.                        ind_pathname,pathname  ...
  168.                        );
  169.         wmemtool('wmb',win_wptool,n_wp_utils,    ...
  170.                        ind_nb_colors,NB_ColorsInPal ...
  171.                        );
  172.         % Writing structures.
  173.         %----------------------
  174.         wmemtool('wmb',win_wptool,n_sav_struct,     ...
  175.                        ind_sav_tree_st,tree_struct, ...
  176.                        ind_sav_data_st,data_struct);
  177.         wmemtool('wmb',win_wptool,n_structures,  ...
  178.                        ind_tree_st,tree_struct, ...
  179.                        ind_data_st,data_struct);
  180.         % Setting GUI values.
  181.         %--------------------
  182.         wp2dutil('set_gui',win_wptool,option);
  183.         % Setting Initial Colormap.
  184.         %--------------------------
  185.         cbcolmap('set',win_wptool,'pal',{'pink',NB_ColorsInPal,'self',[]});
  186.         % Computing Original Signal.
  187.         %--------------------------
  188.         if isa(tree_struct,'wptree')
  189.             Img_Anal = wprec2(tree_struct);
  190.         else
  191.             Img_Anal = wprec2(tree_struct,data_struct);
  192.         end
  193.         % Drawing.
  194.         %---------
  195.         wp2ddraw('sig',win_wptool,Img_Anal);
  196.         % Decomposition drawing
  197.         %----------------------
  198.         wp2ddraw('anal',win_wptool);
  199.         % Setting enabled values.
  200.         %------------------------
  201.         wp2dutil('enable',win_wptool,option);
  202.         % End waiting.
  203.         %-------------
  204.         wwaiting('off',win_wptool);
  205.     case 'demo'
  206.         % in3 = Img_Name
  207.         % in4 = Wav_Name
  208.         % in5 = Lev_Anal
  209.         % in6 = Ent_Name
  210.         % in7 = Ent_Par (optional)
  211.         %------------------
  212.         Img_Name = deblank(in3);
  213.         Wav_Name = deblank(in4);
  214.         Lev_Anal = in5;
  215.         Ent_Name   = deblank(in6);
  216.         if nargin==6 , Ent_Par = 0 ; else , Ent_Par = in7; end
  217. % Loading file.
  218.         %--------------
  219.         filename = [Img_Name '.mat'];
  220.         pathname = utguidiv('WTB_DemoPath',filename);
  221.         [imgInfos,Img_Anal,map,ok] = utguidiv('load_dem2D',win_wptool, ...
  222.             pathname,filename,default_nbcolors);
  223.         if ~ok, return; end
  224.         
  225.         % Cleaning.
  226.         %----------
  227.         wp2dutil('clean',win_wptool,option);
  228.         % Setting Analysis parameters
  229.         %-----------------------------
  230.         NB_ColorsInPal = size(map,1);
  231.         wmemtool('wmb',win_wptool,n_param_anal,    ...
  232.                        ind_act_option,option,      ...
  233.                        ind_img_name,imgInfos.name, ...
  234.                        ind_img_t_name,imgInfos.true_name, ...
  235.                        ind_wav_name,Wav_Name,      ...
  236.                        ind_lev_anal,Lev_Anal,      ...
  237.                        ind_img_size,imgInfos.size, ...
  238.                        ind_ent_anal,Ent_Name,      ...
  239.                        ind_ent_par,Ent_Par         ...
  240.                        );
  241.         wmemtool('wmb',win_wptool,n_InfoInit, ...
  242.                        ind_filename,imgInfos.filename, ...
  243.                        ind_pathname,imgInfos.pathname  ...
  244.                        );
  245.          wmemtool('wmb',win_wptool,n_wp_utils,    ...
  246.                        ind_nb_colors,NB_ColorsInPal ...
  247.                        );
  248.         % Setting GUI values.
  249.         %--------------------
  250.         wp2dutil('set_gui',win_wptool,option);
  251.         if imgInfos.self_map , arg = map; else , arg = []; end
  252.         cbcolmap('set',win_wptool,'pal',{'pink',NB_ColorsInPal,'self',arg});
  253.         % Drawing.
  254.         %---------
  255.         wp2ddraw('sig',win_wptool,Img_Anal);
  256.         % Calling Analysis.
  257.         %-----------------
  258.         wp2dmngr('step2',win_wptool,option);
  259.         % Setting enabled values.
  260.         %------------------------
  261.         wp2dutil('enable',win_wptool,option);
  262.         % End waiting.
  263.         %-------------
  264.         wwaiting('off',win_wptool);
  265.     case 'save_synt'
  266.         % Testing file.
  267.         %--------------
  268.         [filename,pathname,ok] = utguidiv('test_save',win_wptool, ...
  269.                                      '*.mat','Save Synthesized Image');
  270.         if ~ok, return; end
  271.         % Begin waiting.
  272.         %--------------
  273.         wwaiting('msg',win_wptool,'Wait ... saving');
  274.         % Getting colormap.
  275.         %------------------
  276.         map = cbcolmap('get',win_wptool,'self_pal');
  277.         if isempty(map)
  278.             nb_colors = wmemtool('rmb',win_wptool,n_wp_utils,ind_nb_colors);
  279.             map = pink(nb_colors);
  280.         end
  281.         % Getting Synthesized Image.
  282.         %---------------------------
  283.         hdl_node = wpssnode('r_synt',win_wptool);
  284.         if ~isempty(hdl_node)
  285.             X = get(hdl_node,'userdata');
  286.         else
  287.             % Reading structures.
  288.             %--------------------
  289.             [tree_struct,data_struct] = ...
  290.                     wmemtool('rmb',win_wptool,n_structures,...
  291.                                    ind_tree_st,ind_data_st);
  292.             if isa(tree_struct,'wptree')
  293.                 X = wprec2(tree_struct);
  294.             else
  295.                 X = wprec2(tree_struct,data_struct);
  296.             end
  297.         end
  298.         X = round(X);
  299.         % Saving file.
  300.         %--------------
  301.         [wname,valTHR] = wmemtool('rmb',win_wptool,n_param_anal,...
  302.                                   ind_wav_name,ind_thr_val);
  303.         [name,ext] = strtok(filename,'.');
  304.         if isempty(ext) | isequal(ext,'.')
  305.             ext = '.mat'; filename = [name ext];
  306.         end
  307.         saveStr = {'X','map','valTHR','wname'};
  308.         wwaiting('off',win_wptool);
  309.         try
  310.           save([pathname filename],saveStr{:});
  311.         catch
  312.           errargt(mfilename,'Save FAILED !','msg');
  313.         end
  314.     case 'save_dec'
  315.         % Testing file.
  316.         %--------------
  317.          fileMask = {...
  318.                '*.wp2;*.mat' , 'Decomposition  (*.wp2;*.mat)';
  319.                '*.*','All Files (*.*)'};                        
  320.         [filename,pathname,ok] = utguidiv('test_save',win_wptool, ...
  321.                                    fileMask,'Save Wavelet Packet Analysis (2D)');
  322.         if ~ok, return; end
  323.         % Begin waiting.
  324.         %--------------
  325.         wwaiting('msg',win_wptool,'Wait ... saving decomposition');
  326.         [name,ext] = strtok(filename,'.');
  327.         ext = '.wp2';
  328.         filename = [name ext];
  329.         % Getting Analysis parameters.
  330.         %-----------------------------
  331.         data_name = wmemtool('rmb',win_wptool,n_param_anal,ind_img_name);
  332.         [tree_struct,data_struct] = wmemtool('rmb',win_wptool,n_structures,...
  333.                                         ind_tree_st,ind_data_st);
  334.         valTHR = wmemtool('rmb',win_wptool,n_param_anal,ind_thr_val);
  335.         map = cbcolmap('get',win_wptool,'self_pal');
  336.         if isempty(map)
  337.             nb_colors = wmemtool('rmb',win_wptool,n_wp_utils,ind_nb_colors);
  338.             map = pink(nb_colors);
  339.         end
  340.         % Saving file.
  341.         %-------------
  342.         [name,ext] = strtok(filename,'.');
  343.         if isempty(ext) | isequal(ext,'.')
  344.             ext = '.wp2'; filename = [name ext];
  345.         end
  346.         if isa(tree_struct,'wptree')
  347.           saveStr = {'tree_struct','map','data_name','valTHR'};
  348.         else
  349.           saveStr = {'tree_struct','data_struct','map','data_name','valTHR'};
  350.         end
  351.         wwaiting('off',win_wptool);
  352.         try
  353.           save([pathname filename],saveStr{:});
  354.         catch
  355.           errargt(mfilename,'Save FAILED !','msg');
  356.         end
  357.     case 'anal'
  358.         active_option = wmemtool('rmb',win_wptool,n_param_anal,...
  359.                                         ind_act_option);
  360.         if ~strcmp(active_option,'load_img')
  361.             % Test for new Analysis.
  362.             %-----------------------
  363.             % new = wwaitans(win_wptool,'New Analysis ?');
  364.             % if new==0 , return; end
  365.             % Cleaning. 
  366.             %----------
  367.             wwaiting('msg',win_wptool,'Wait ... computing');
  368.             wp2dutil('clean',win_wptool,'load_img','new_anal');
  369.             wp2dutil('enable',win_wptool,'load_img');
  370.         else
  371.             wwaiting('msg',win_wptool,'Wait ... computing');
  372.             wmemtool('wmb',win_wptool,n_param_anal,ind_act_option,'anal');
  373.         end
  374.         % Setting Analysis parameters
  375.         %-----------------------------
  376.         [Wav_Name,Lev_Anal] = cbanapar('get',win_wptool,'wav','lev');
  377.         [Ent_Name,Ent_Par,err] = utentpar('get',win_wptool,'ent');
  378.         if err>0
  379.             wwaiting('off',win_wptool);
  380.             switch err
  381.               case 1 , msg = 'Invalid entropy parameter value! ';
  382.               case 2 , msg = 'Invalid entropy name! ';
  383.             end
  384.             errargt(mfilename,msg,'msg');
  385.             utentpar('set',win_wptool);
  386.             return
  387.         end
  388.         wmemtool('wmb',win_wptool,n_param_anal, ...
  389.                        ind_wav_name,Wav_Name,   ...
  390.                        ind_lev_anal,Lev_Anal,   ...
  391.                        ind_ent_anal,Ent_Name,   ...
  392.                        ind_ent_par,Ent_Par      ...
  393.                        );
  394.         % Calling Analysis.
  395.         %------------------
  396.         wp2dmngr('step2',win_wptool,option);
  397.         % Setting enabled values.
  398.         %------------------------
  399.         wp2dutil('enable',win_wptool,option);
  400.         % End waiting.
  401.         %-------------
  402.         wwaiting('off',win_wptool);
  403.     case 'step2'
  404.         % Begin waiting.
  405.         %---------------
  406.         wwaiting('msg',win_wptool,'Wait ... computing');
  407.         % Getting  Analysis parameters.
  408.         %------------------------------
  409.         [Img_Name,Img_Size,Wav_Name,Lev_Anal,...
  410.                         Img_True_Name,Ent_Name,Ent_Par] = ...
  411.                         wmemtool('rmb',win_wptool,n_param_anal, ...
  412.                                         ind_img_name,ind_img_size, ...
  413.                                         ind_wav_name,ind_lev_anal, ...
  414.                                         ind_img_t_name, ...
  415.                                         ind_ent_anal,ind_ent_par);
  416.         active_option = wmemtool('rmb',win_wptool,n_param_anal,ind_act_option);
  417.         [filename,pathname] = wmemtool('rmb',win_wptool,n_InfoInit, ...
  418.                                         ind_filename,ind_pathname);
  419.         if strcmp(active_option,'demo') | strcmp(active_option,'anal')
  420.             numopt = 1;
  421.         elseif strcmp(active_option,'load_dec')
  422.             numopt = 2;
  423.         end
  424.         % Computing.
  425.         %-----------
  426.         objVersion = wtbxmngr('get','objVersion');
  427.         if numopt==1
  428.             try
  429.                 [fileStruct,err] = wfileinf(pathname,filename);
  430.             catch
  431.                 err = 1;
  432.             end
  433.             if ~err
  434.                 try
  435.                   load([pathname filename],'-mat');
  436.                 catch
  437.                   msg = sprintf('File %s is not a valid file.', filename);
  438.                   err = 1;
  439.                 end
  440.             else
  441.                 [X,map,imgFormat,colorType,err] = ...
  442.                     utguidiv('direct_load_img',win_wptool,pathname,filename);
  443.                 if err
  444.                     msg = sprintf('File %s is not a valid file or is empty.', filename);
  445.                 end
  446.             end
  447.             if err==1
  448.                 msg = sprintf('File %s not found!', filename);
  449.                 wwaiting('off',win_wptool);
  450.                 errordlg(msg,'Load Image ERROR','modal');
  451.                 return
  452.             end
  453.             Img_Anal = double(eval(Img_True_Name));
  454.         elseif numopt==2    % second time only for load_dec
  455.             Img_Anal = get(wp2ddraw('r_orig',win_wptool),'Cdata');
  456.             tree_struct = wmemtool('rmb',win_wptool,n_structures,ind_tree_st);
  457.             if isa(tree_struct,'wptree')
  458.                 objVersion = 1;
  459.             else
  460.                 objVersion = 0;
  461.             end
  462.         end
  463.         if objVersion>0
  464.             tree_struct = ...
  465.                 wpdec2(Img_Anal,Lev_Anal,Wav_Name,Ent_Name,Ent_Par);
  466.             data_struct = [];
  467.         else
  468.             [tree_struct,data_struct] = ...
  469.                 wpdec2(Img_Anal,Lev_Anal,Wav_Name,Ent_Name,Ent_Par);
  470.         end
  471.         % Writing structures.
  472.         %--------------------
  473.         wmemtool('wmb',win_wptool,n_sav_struct, ...
  474.                         ind_sav_tree_st,tree_struct, ...
  475.                         ind_sav_data_st,data_struct);
  476.         wmemtool('wmb',win_wptool,n_structures, ...
  477.                         ind_tree_st,tree_struct, ...
  478.                         ind_data_st,data_struct);
  479.         % Decomposition drawing
  480.         %----------------------
  481.         wp2ddraw('anal',win_wptool);
  482.         % End waiting.
  483.         %-------------
  484.         wwaiting('off',win_wptool);
  485.     case 'comp'
  486.         mousefrm(0,'watch');
  487.         drawnow;
  488.         wp2dutil('enable',win_wptool,option);
  489.         out1 = feval('wp2dcomp','create',win_wptool);
  490.     case 'deno'
  491.         mousefrm(0,'watch');
  492.         drawnow;
  493.         wp2dutil('enable',win_wptool,option);
  494.         out1 = feval('wp2ddeno','create',win_wptool);
  495.     case {'return_comp','return_deno'}
  496.         % in3 = 1 : preserve compression
  497.         % in3 = 0 : discard compression
  498.         % in4 = hdl_img (optional)
  499.         %--------------------------------------
  500.         if in3==1
  501.             % Begin waiting.
  502.             %--------------
  503.             wwaiting('msg',win_wptool,'Wait ... drawing');
  504.             if strcmp(option,'return_comp')
  505.                 namesig = 'cs';
  506.             else
  507.                 namesig = 'ds';
  508.             end
  509.             NB_Col = wmemtool('rmb',win_wptool,n_wp_utils,ind_nb_colors);
  510.             wpssnode('plot',win_wptool,namesig,2,in4,NB_Col)
  511.             % End waiting.
  512.             %-------------
  513.             wwaiting('off',win_wptool);
  514.         end
  515.         wp2dutil('enable',win_wptool,option);
  516.     otherwise
  517.         errargt(mfilename,'Unknown Option','msg');
  518.         error('*');
  519. end