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

波变换

开发平台:

Matlab

  1. function varargout = wp2ddeno(option,varargin)
  2. %WP2DDENO Wavelet packets 2-D de-noising.
  3. %   VARARGOUT = WP2DDENO(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.17.4.2 $ $Date: 2004/03/15 22:43:11 $
  8. % Memory Blocks of stored values.
  9. %================================
  10. % MB1 (main window).
  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 (main window).
  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. ind_cfsMode    = 6;
  48. nbLOC_1_stored = 6;
  49. % MB2 (local).
  50. %-------------
  51. n_thrDATA = 'thrDATA';
  52. ind_value = 1;
  53. nbLOC_2_stored = 1;
  54. if ~isequal(option,'create') , win_denoise = varargin{1}; end
  55. switch option
  56.     case 'create'
  57.         % Get Globals.
  58.         %-------------
  59.         [Def_Btn_Height,Y_Spacing,Def_FraBkColor] = ...
  60.             mextglob('get','Def_Btn_Height','Y_Spacing','Def_FraBkColor');
  61.         % Calling figure.
  62.         %----------------
  63.         win_caller = varargin{1};
  64.         str_win_caller = sprintf('%.0f',win_caller);
  65.         % Window initialization.
  66.         %----------------------
  67.         win_name = 'Wavelet Packet 2-D  --  De-noising';
  68.         [win_denoise,pos_win,win_units,str_win_denoise,...
  69.                 frame0,pos_frame0,Pos_Graphic_Area,pus_close] = ...
  70.                     wfigmngr('create',win_name,'','ExtFig_CompDeno', ...
  71.                                 strvcat(mfilename,'cond'),1,1,0);
  72.         set(win_denoise,'userdata',win_caller);
  73.         if nargout>0 , varargout{1} = win_denoise; end
  74. % Add Help Item.
  75. %----------------
  76. wfighelp('addHelpItem',win_denoise,'De-noising Procedure','DENO_PROCEDURE');
  77. wfighelp('addHelpItem',win_denoise,'Available Methods','COMP_DENO_METHODS');
  78.         % Menu construction for current figure.
  79.         %--------------------------------------
  80. m_save  = wfigmngr('getmenus',win_denoise,'save');
  81.         sav_menus(1) = uimenu(m_save,...
  82.                                 'Label','De-noised &Image ',    ...
  83.                                 'Position',1,                   ...
  84.                                 'Enable','Off',                 ...
  85.                                 'Callback',                     ...
  86.                                 [mfilename '(''save_synt'','    ...
  87.                                         str_win_denoise ');']   ...
  88.                                 );
  89.         sav_menus(2) =uimenu(m_save,...
  90.                                'Label','&Decomposition ',     ...
  91.                                'Position',2,                  ...
  92.                                'Enable','Off',                ...
  93.                                'Callback',                    ...
  94.                                [mfilename '(''save_dec'','    ...
  95.                                        str_win_denoise ');']  ...
  96.                                );
  97.         % Begin waiting.
  98.         %---------------
  99.         wwaiting('msg',win_denoise,'Wait ... initialization');
  100.         % Getting variables from wp2dtool figure memory block.
  101.         %-----------------------------------------------------
  102.         [WP_Tree,WP_Data] = wmemtool('rmb',win_caller,n_structures,...
  103.                                            ind_tree_st,ind_data_st);
  104.         depth = treedpth(WP_Tree);
  105.         [Img_Name,Wav_Name,Img_Size,Ent_Nam,Ent_Par] =  ...
  106.                   wmemtool('rmb',win_caller,n_param_anal, ...
  107.                                  ind_img_name,ind_wav_name, ...
  108.                                  ind_img_size,    ...
  109.                                  ind_ent_anal,ind_ent_par);
  110.         Wav_Fam = wavemngr('fam_num',Wav_Name);
  111.         NB_ColorsInPal = wmemtool('rmb',win_caller,n_wp_utils,ind_nb_colors);
  112.         % General graphical parameters initialization.
  113.         %--------------------------------------------
  114.         dy = Y_Spacing;
  115.         % Command part of the window.
  116.         %============================
  117.         % Data, Wavelet and Level parameters.
  118.         %------------------------------------
  119.         xlocINI = pos_frame0([1 3]);
  120.         ytopINI = pos_win(4)-dy;
  121.         toolPos = utanapar('create_copy',win_denoise, ...
  122.                     {'xloc',xlocINI,'top',ytopINI},...
  123.                     {'n_s',{Img_Name,Img_Size},'wav',Wav_Name,'lev',depth} ...
  124.                     );
  125.         % Entropy parameters.
  126.         %--------------------
  127.         ytopENT = toolPos(2)-4*dy;
  128.         toolPos = utentpar('create_copy',win_denoise, ...
  129.                     {'xloc',xlocINI,'top',ytopENT,...
  130.                      'ent',{Ent_Nam,Ent_Par}} ...
  131.                     );
  132.         % Global De-noising tool.
  133.         %------------------------        
  134.         ytopTHR = toolPos(2)-4*dy;
  135.         utthrwpd('create',win_denoise,'toolOPT','wpdeno2', ...
  136.                  'xloc',xlocINI,'top',ytopTHR ...
  137.                  );
  138.         % Adding colormap GUI.
  139.         %---------------------
  140.         pop_pal_caller = cbcolmap('get',win_caller,'pop_pal');
  141.         prop_pal = get(pop_pal_caller,{'String','Value','Userdata'});
  142.         utcolmap('create',win_denoise, ...
  143.                  'xloc',xlocINI, ...
  144.                  'bkcolor',Def_FraBkColor, ...
  145.                  'enable','on');
  146.         pop_pal_loc = cbcolmap('get',win_denoise,'pop_pal');
  147.         set(pop_pal_loc,'String',prop_pal{1},'Value',prop_pal{2}, ...
  148.                         'Userdata',prop_pal{3});
  149.         set(win_denoise,'Colormap',get(win_caller,'Colormap'));
  150.         % Axes construction.
  151.         %===================
  152.         % General graphical parameters initialization.
  153.         %--------------------------------------------
  154.         bdx     = 0.08*pos_win(3);
  155.         ecx     = 0.04*pos_win(3);
  156.         bdy     = 0.06*pos_win(4);
  157.         ecy     = 0.03*pos_win(4);
  158.         y_graph = 2*Def_Btn_Height+dy;
  159.         h_graph = pos_frame0(4)-y_graph;
  160.         w_graph = pos_frame0(1);
  161.         % Building axes for original image.
  162.         %----------------------------------
  163.         x_axe  = bdx;
  164.         w_axe  = (w_graph-ecx-3*bdx/2)/2;
  165.         h_axe  = (h_graph-3*bdy)/2;
  166.         y_axe  = y_graph+h_graph-h_axe-bdy;
  167.         cx_ori = x_axe+w_axe/2;
  168.         cy_ori = y_axe+h_axe/2;
  169.         cx_den = cx_ori+w_axe+ecx;
  170.         cy_den = cy_ori;
  171.         [w_used,h_used] = wpropimg(Img_Size,w_axe,h_axe,'pixels');
  172.         pos_axe  = [cx_ori-w_used/2 cy_ori-h_used/2 w_used h_used];
  173.         axe_datas(1) = axes('Parent',win_denoise,...
  174.                             'Units',win_units,...
  175.                             'Position',pos_axe,...
  176.                             'Drawmode','fast',...
  177.                             'Box','On');
  178.         % Displaying original image.
  179.         %---------------------------
  180.         Img_Anal  = get(wp2ddraw('r_orig',win_caller),'Cdata');
  181.         hdl_datas = [NaN NaN];
  182.         hdl_datas(1) = image([1 Img_Size(1)],[1,Img_Size(2)],Img_Anal,...
  183.                              'Parent',axe_datas(1));
  184.         wtitle('Original image','Parent',axe_datas(1));
  185.         % Building axes for de-noised image.
  186.         %-----------------------------------
  187.         pos_axe = [cx_den-w_used/2 cy_den-h_used/2 w_used h_used];
  188.         xylim   = get(axe_datas(1),{'Xlim','Ylim'});
  189.         axe_datas(2) = axes('Parent',win_denoise,...
  190.                             'Units',win_units,...
  191.                             'Position',pos_axe,...
  192.                             'Drawmode','fast',...
  193.                             'Visible','off',...
  194.                             'Box','On',...
  195.                             'Xlim',xylim{1},...
  196.                             'Ylim',xylim{2});
  197.         % Initializing threshold.
  198.         %------------------------
  199.         [valTHR,maxTHR,cfs] = ...
  200.             wp2ddeno('compute_GBL_THR',win_denoise,win_caller);
  201.         utthrwpd('setThresh',win_denoise,[0,valTHR,maxTHR]);
  202.         % Displaying perfos.
  203.         %-------------------
  204.         x_axe = bdx;
  205.         y_axe = y_graph+bdy;
  206.         pos_axe_perfo = [x_axe y_axe w_axe h_axe];
  207.         x_axe = bdx+w_axe+ecx;
  208.         y_axe = y_graph+bdy;
  209.         pos_axe_histo = [x_axe y_axe w_axe h_axe];
  210.         utthrwpd('displayPerf',win_denoise,pos_axe_perfo,pos_axe_histo,cfs);
  211.         
  212.         % Memory blocks update.
  213.         %----------------------      
  214.         utthrwpd('set',win_denoise,'handleORI',hdl_datas(1));        
  215.         wmemtool('ini',win_denoise,n_misc_loc,nbLOC_1_stored);
  216.         wmemtool('wmb',win_denoise,n_misc_loc,    ...
  217.                        ind_sav_menus,sav_menus,   ...
  218.                        ind_status,0,              ...
  219.                        ind_win_caller,win_caller, ...
  220.                        ind_axe_datas,axe_datas,   ...
  221.                        ind_hdl_datas,hdl_datas    ...
  222.                        );
  223.         wmemtool('ini',win_denoise,n_thrDATA,nbLOC_2_stored);
  224.         % Axes attachment.
  225.         %-----------------
  226.         axe_cmd = axe_datas;
  227.         dynvtool('init',win_denoise,[],axe_cmd,[],[1 1]);
  228.         % Setting units to normalized.
  229.         %-----------------------------
  230.         wfigmngr('normalize',win_denoise);
  231.  
  232.         % End waiting.
  233.         %-------------
  234.         wwaiting('off',win_denoise);
  235.     case 'denoise'
  236.         wp2ddeno('clear_GRAPHICS',win_denoise);
  237.         % Waiting message.
  238.         %-----------------
  239.         wwaiting('msg',win_denoise,'Wait ... computing');
  240.         % Getting memory blocks.
  241.         %-----------------------
  242.         [axe_datas,hdl_datas] = wmemtool('rmb',win_denoise,n_misc_loc, ...
  243.                                                ind_axe_datas,ind_hdl_datas);
  244.         win_caller = wmemtool('rmb',win_denoise,n_misc_loc,ind_win_caller);
  245.         [WP_Tree,WP_Data] = wmemtool('rmb',win_caller,n_structures,...
  246.                                            ind_tree_st,ind_data_st);
  247.         Img_Size = wmemtool('rmb',win_caller,n_param_anal,ind_img_size);
  248.         NB_ColorsInPal = wmemtool('rmb',win_caller,n_wp_utils,...
  249.                                         ind_nb_colors);
  250.         % De-noising depending on the selected thresholding mode.
  251.         %--------------------------------------------------------
  252.         [numMeth,meth,threshold,sorh] = utthrwpd('get_GBL_par',win_denoise);
  253.         thrParams = {sorh,'nobest',threshold,1};
  254.         if isa(WP_Tree,'wptree')
  255.             [C_Img,C_Tree] = wpdencmp(WP_Tree,thrParams{:}); C_Data = [];
  256.         else
  257.             [C_Img,C_Tree,C_Data] = wpdencmp(WP_Tree,WP_Data,thrParams{:});
  258.         end
  259.         % Displaying de-noised image.
  260.         %----------------------------
  261.         img_deno = hdl_datas(2);
  262.         if ~ishandle(img_deno)
  263.             xylim = get(axe_datas(1),{'Xlim','Ylim'});
  264.             img_deno = image([1 Img_Size(1)],[1,Img_Size(2)],C_Img, ...
  265.                              'Parent',axe_datas(2));
  266.             set(axe_datas(2),'Xlim',xylim{1},'Ylim',xylim{2});
  267.             hdl_datas(2) = img_deno;
  268.             utthrwpd('set',win_denoise,'handleTHR',hdl_datas(2));
  269.             wmemtool('wmb',win_denoise,n_misc_loc,ind_hdl_datas,hdl_datas);
  270.         else
  271.             set(img_deno,'Cdata',C_Img);
  272.         end
  273.         set(findobj(axe_datas(2)),'Visible','on');       
  274.         wtitle('De-noised image','Parent',axe_datas(2));
  275.         % Memory blocks update.
  276.         %----------------------
  277.         wmemtool('wmb',win_denoise,n_thrDATA,ind_value, ...
  278.                  {C_Img,C_Tree,C_Data,threshold});
  279.         wp2ddeno('enable_menus',win_denoise,'on');
  280.         % End waiting.
  281.         %-------------
  282.         wwaiting('off',win_denoise);
  283.     case 'compute_GBL_THR'
  284.         win_caller = varargin{2};
  285.         pause(0.01)
  286.         [numMeth,meth,threshold,sorh] = utthrwpd('get_GBL_par',win_denoise);
  287.         [WP_Tree,WP_Data] = wmemtool('rmb',win_caller,n_structures,...
  288.                                            ind_tree_st,ind_data_st);
  289.         [valTHR,maxTHR,cfs] = wthrmngr('wp2ddenoGBL',meth,WP_Tree,WP_Data);
  290.         if   nargout==1 , varargout = {valTHR};
  291.         else            , varargout = {valTHR,maxTHR,cfs};
  292.         end
  293.     case 'update_GBL_meth'
  294.         wp2ddeno('clear_GRAPHICS',win_denoise);
  295.         win_caller = wmemtool('rmb',win_denoise,n_misc_loc,ind_win_caller);
  296.         valTHR = wp2ddeno('compute_GBL_THR',win_denoise,win_caller);
  297.         utthrwpd('update_GBL_meth',win_denoise,valTHR);
  298.     case 'clear_GRAPHICS'
  299.         % Diseable save Menus.
  300.         %--------------------
  301.         wp2ddeno('enable_menus',win_denoise,'off');
  302.         % Get Handles.
  303.         %-------------
  304.         axe_datas = wmemtool('rmb',win_denoise,n_misc_loc,ind_axe_datas);
  305.         % Setting the de-noised coefs axes invisible.
  306.         %--------------------------------------------
  307.         axe_deno = axe_datas(2);
  308.         vis = get(axe_deno,'Visible');
  309.         if vis(1:2)=='on'
  310.             set(findobj(axe_deno),'Visible','off');
  311.             wtitle('Original signal','Parent',axe_datas(1));
  312.             drawnow
  313.         end
  314.     case 'enable_menus'
  315.         enaVal = varargin{2};
  316.         sav_menus = wmemtool('rmb',win_denoise,n_misc_loc,ind_sav_menus);
  317.         set(sav_menus,'Enable',enaVal);
  318.         utthrwpd('enable_tog_res',win_denoise,enaVal);
  319.         if strncmpi(enaVal,'on',2) , status = 1; else , status = 0; end
  320.         wmemtool('wmb',win_denoise,n_misc_loc,ind_status,status);
  321.     case 'save_synt'
  322.         % Testing file.
  323.         %--------------
  324.         [filename,pathname,ok] = utguidiv('test_save',win_denoise, ...
  325.                                      '*.mat','Save De-noised Image');
  326.         if ~ok, return; end
  327.         % Begin waiting.
  328.         %--------------
  329.         wwaiting('msg',win_denoise,'Wait ... saving');
  330.         % Getting Analysis values.
  331.         %-------------------------
  332.         win_caller = wmemtool('rmb',win_denoise,n_misc_loc,ind_win_caller);
  333.         wname = wmemtool('rmb',win_caller,n_param_anal,ind_wav_name);
  334.         map = cbcolmap('get',win_caller,'self_pal');
  335.         if isempty(map)
  336.             nb_colors = wmemtool('rmb',win_caller,n_wp_utils,ind_nb_colors);
  337.             map = pink(nb_colors);
  338.         end
  339.         thrDATA = wmemtool('rmb',win_denoise,n_thrDATA,ind_value);
  340.         X = round(thrDATA{1});
  341.         valTHR = thrDATA{4};
  342.         % Saving file.
  343.         %--------------
  344.         [name,ext] = strtok(filename,'.');
  345.         if isempty(ext) | isequal(ext,'.')
  346.             ext = '.mat'; filename = [name ext];
  347.         end
  348.         saveStr = {'X','map','valTHR','wname'};
  349.         wwaiting('off',win_denoise);
  350.         try
  351.           save([pathname filename],saveStr{:});
  352.         catch
  353.           errargt(mfilename,'Save FAILED !','msg');
  354.         end
  355.     case 'save_dec'
  356.         % Testing file.
  357.         %--------------
  358.         [filename,pathname,ok] = utguidiv('test_save',win_denoise, ...
  359.                                    '*.wp2','Save Wavelet Packet Analysis (2D)');
  360.         if ~ok, return; end
  361.         % Begin waiting.
  362.         %--------------
  363.         wwaiting('msg',win_denoise,'Wait ... saving decomposition');
  364.         % Getting Analysis values.
  365.         %-------------------------
  366.         win_caller = wmemtool('rmb',win_denoise,n_misc_loc,ind_win_caller);
  367.         map = cbcolmap('get',win_caller,'self_pal');
  368.         if isempty(map)
  369.             nb_colors = wmemtool('rmb',win_caller,n_wp_utils,ind_nb_colors);
  370.             map = pink(nb_colors);
  371.         end
  372.         data_name = wmemtool('rmb',win_caller,n_param_anal,ind_img_name);
  373.         thrDATA = wmemtool('rmb',win_denoise,n_thrDATA,ind_value);
  374.         tree_struct = thrDATA{2};
  375.         data_struct = thrDATA{3};
  376.         valTHR = thrDATA{4};
  377.         % Saving file.
  378.         %--------------
  379.         [name,ext] = strtok(filename,'.');
  380.         if isempty(ext) | isequal(ext,'.')
  381.             ext = '.wp2'; filename = [name ext];
  382.         end
  383.         if isa(tree_struct,'wptree')
  384.           saveStr = {'tree_struct','map','data_name','valTHR'};
  385.         else
  386.           saveStr = {'tree_struct','data_struct','map','data_name','valTHR'};
  387.         end
  388.         wwaiting('off',win_denoise);
  389.         try
  390.           save([pathname filename],saveStr{:});
  391.         catch
  392.           errargt(mfilename,'Save FAILED !','msg');
  393.         end
  394.     case 'close'
  395.         [status,win_caller] = wmemtool('rmb',win_denoise,n_misc_loc,...
  396.                                              ind_status,ind_win_caller);
  397.         if status==1
  398.             % Test for Updating.
  399.             %--------------------
  400.             status = wwaitans({win_denoise,'WP2D De-noising'},...
  401.                               'Update the synthesized image ?',2,'cancel');
  402.         end
  403.         switch status
  404.             case 1
  405.               wwaiting('msg',win_denoise,'Wait ... computing');
  406.               thrDATA = wmemtool('rmb',win_denoise,n_thrDATA,ind_value);
  407.               valTHR  = thrDATA{4};
  408.               wmemtool('wmb',win_caller,n_param_anal,ind_thr_val,valTHR);
  409.               hdl_datas = wmemtool('rmb',win_denoise,n_misc_loc,ind_hdl_datas);
  410.               img_deno = hdl_datas(2);
  411.               wp2dmngr('return_deno',win_caller,status,img_deno);
  412.               wwaiting('off',win_denoise);
  413.             case 0 , wp2dmngr('return_deno',win_caller,status);
  414.         end
  415.         if nargout>0 , varargout{1} = status; end
  416.     otherwise
  417.         errargt(mfilename,'Unknown Option','msg');
  418.         error('*');
  419. end