Guest User

Bug: division returning zero

a guest
Mar 18th, 2015
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 1.56 KB | None | 0 0
  1. global L = 256; % grayscale.
  2. global K = 0.01;
  3. global C = (L * K) * (L * K); % constant used to avoid divide by zero.
  4.  
  5. function mu = mean(img)
  6.     % number of lines and columns
  7.     N = size(img)(1);
  8.     M = size(img)(2);
  9.    
  10.     s = sum(img(:));
  11.     mu = s / (N*M);
  12. endfunction
  13.  
  14. function dp = standard_deviation(img)
  15.     N = size(img)(1);
  16.     M = size(img)(2);
  17.  
  18.     mi = mean(img);
  19.     soma = 0.0;
  20.  
  21.     for i=1:N
  22.         for j=1:M
  23.             soma = soma + (img(i, j) - mi) * (img(i, j) - mi);
  24.         end
  25.     end
  26.  
  27.     variancia = soma / (N*M - 1);
  28.     dp = sqrt(variancia);
  29.  
  30. endfunction
  31.  
  32.  
  33. function corr = correlation(img1, img2)
  34.     N1 = size(img1)(1);
  35.     M1 = size(img1)(2);
  36.  
  37.     N2 = size(img2)(1);
  38.     M2 = size(img2)(2);
  39.  
  40.     N = min(N1, N2);
  41.     M = min(M1, M2);
  42.  
  43.     mi1 = mean(img1);
  44.     mi2 = mean(img2);
  45.     soma = 0.0;
  46.  
  47.     for i=1:N
  48.         for j=1:M
  49.             soma = soma + (img1(i, j) - mi1) * (img2(i, j) - mi2);     
  50.         end
  51.     end
  52.  
  53.     corr = soma / (N*M - 1);
  54.  
  55. endfunction
  56.  
  57.  
  58. function ec = struct_comp (img1, img2)
  59.     global C;
  60.     dp1 = standard_deviation(img1);
  61.     dp2 = standard_deviation(img2);
  62.     corr = correlation(img1, img2);
  63.     disp('C:');
  64.     disp(C);
  65.     disp('dp1');
  66.     disp(dp1);
  67.     disp('dp2');
  68.     disp(dp2);
  69.     disp('corr');
  70.     disp(corr);
  71.  
  72.     disp('(corr + C)');
  73.     disp((corr + C));
  74.     disp('(dp1 * dp2 + C)');
  75.     disp(dp1 * dp2 + C);
  76.     disp('(corr + C) / (dp1 * dp2 + C)');
  77.     disp((corr + C) / (dp1 * dp2 + C));
  78.     ec = (corr + C) / (dp1 * dp2 + C);
  79. endfunction
  80.  
  81. name1 = 'tux.jpg';
  82. name2 = 'monalisa.jpg';
  83.  
  84. img1 = int32(imread(name1));
  85. img2 = int32(imread(name2));
  86.  
  87. e = struct_comp(img1, img2);
  88. disp('Struct comp: ');
  89. disp(e);
Advertisement
Add Comment
Please, Sign In to add comment