pdfbcalc_imagekld.m
上传用户:l56789
上传日期:2022-02-25
资源大小:2422k
文件大小:3k
源码类别:

图形图像处理

开发平台:

Matlab

  1. % pdfbcalc_imagekld.m
  2. % written by: Duncan Po
  3. % Date: November 16, 2002
  4. % calculate the Kublick-Liebler distance between two images based on 
  5. % on their hidden markov tree parameters
  6. % Usage:    kld = pdfbcalc_imagekld(savename, sdir, model1, model2)
  7. %           kld = pdfbcalc_imagekld(savename, sdir, image1, model2, imformat1)
  8. %           kld = pdfbcalc_imagekld(savename, sdir, image1, image2, imformat1, 
  9. %                       imformat2)
  10. % Inputs:   model1      - first Hidden Markov Tree Model
  11. %           model2      - second Hidden Markov Tree Model
  12. %           image1      - first image file name
  13. %           image2      - second image file name
  14. %           imformat1   - format of the first image file
  15. %           imformat2   - format of the second image file
  16. %           savename    - filename to save the model of image1
  17. %                       - set to '' if no save desired
  18. %           sdir        - full pathname to save the model 1 (also set to ''
  19. %                           if savename == '')
  20. % Output:   kld         - the Kullback Liebler distance between the trees
  21. %           kld2        - KLD estimation using Monte Carlo method
  22. %function [kld, kld2] = pdfbcalc_imagekld(savename, sdir, model1, model2, ...
  23. %    imf1, imf2)
  24. function [kld, kld2] = pdfbcalc_imagekld(savename, sdir, model1, model2, ...
  25.     imf1, imf2)
  26. % initialize the kld
  27. kld = 0;
  28. kld2 = 0;
  29. if nargin == 6
  30.     % this case is when the images are provided but not the models
  31.     image1 = model1;
  32.     image2 = model2;
  33.     
  34.     % train the HMT models first
  35.     [model1, dummy] = pdfbtrainimagethmt(image1, imf1, '', 0.01);
  36.     [model2, dummy] = pdfbtrainimagethmt(image2, imf2, '', 0.01);
  37. elseif nargin == 5
  38.     % this case is when we have one model and one image, need to 
  39.     % train a second model using the image provided
  40.     image1 = model1;
  41.     
  42.     % train the HMT model
  43.     [model1, dummy] = pdfbtrainimagethmt(image1, imf1, '', 0.01);
  44.     
  45.     %there is also the case for two models and no image when nargin==3
  46.     %in that case, no training is needed
  47. end;
  48. if strcmp(savename, '') ~= 1
  49.     file = sprintf('%s%s', sdir, savename);
  50.     save(file,'model1');
  51. end;
  52. N = 60;
  53. nlevel = model1{1}.nlevels;
  54. for k = 1:nlevel
  55.     levndir(k) = log2(length(model1{1}.stdv{k}));
  56. end;
  57. % treat each tree as independent and using chain rule of KLD for
  58. % independent variables, simply add up the distances
  59. for index = 1:length(model1)
  60.     minkld = 10000;
  61.     dbmodels = pdfbcreate_equiv_models(model1{index});
  62.     for mmm = 1:length(dbmodels)
  63.         tkld = pdfbcalc_KLD(nlevel, levndir, model2{index}, dbmodels{mmm});
  64.         if tkld < minkld
  65.             minkld = tkld;
  66.         end;
  67.     end;
  68.     kld = kld + minkld;
  69. end;
  70. % Also apply the Monte Carlo method to verify the KLD estimation
  71. for index = 1:length(model1)
  72.     kld2 = kld2 + pdfbest_KLD(nlevel, levndir ,model2{index}, ...
  73.         model1{index}, N); 
  74. end;