EBMA_Half_Pixel.m
上传用户:chongwen
上传日期:2021-11-24
资源大小:2k
文件大小:3k
源码类别:

matlab例程

开发平台:

VFP

  1. % clc;
  2. clear all;close all;
  3. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4. %%%  Import Relative Data  %%%
  5. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  6. f1 = fopen('foreman100.Y');
  7. f2 = fopen('foreman101.Y');
  8. f1 = fread(f1,[352,240]);
  9. f2 = fread(f2,[352,240]);
  10. f1 = double(f1)';
  11. f2 = double(f2)';
  12. [height,width] = size(f1);
  13. N = 16;
  14. R = input('nPlease indicate the search range Rn');
  15. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  16. %%%  Target Frame Interpolation  %%%
  17. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  18. f2_inter = zeros(2*height,2*width);
  19. for k = 1:height-1
  20.     for kk = 1:width-1
  21.         f2_inter(2*k-1,2*kk-1) = f2(k,kk);
  22.         f2_inter(2*k-1,2*kk) = (f2(k,kk) + f2(k+1,kk))/2;
  23.         f2_inter(2*k,2*kk-1) = (f2(k,kk) + f2(k,kk+1))/2;
  24.         f2_inter(2*k-1,2*kk-1) = (f2(k,kk) + f2(k,kk+1) + f2(k+1,kk) + f2(k+1,kk+1))/4;
  25.     end
  26. end
  27. for k = 1:height-1
  28.     f2_inter(2*k-1,2*width-1) = f2(k,width);
  29.     f2_inter(2*k,2*width-1) = (f2(k,width)+f2(k+1,width))/2;
  30. end
  31. for k = 1:width-1
  32.     f2_inter(2*height-1,2*k-1) = f2(height,k);
  33.     f2_inter(2*height-1,2*k) = (f2(height,k)+f2(height,k+1))/2;
  34. end
  35. for k = 1:2*height-1
  36.     f2_inter(k,2*width) = f2_inter(k,2*width-1);
  37. end
  38. for k = 1:2*width-1
  39.     f2_inter(2*height,k) = f2_inter(2*height-1,k);
  40. end
  41. f2_inter(2*height,2*width) = f2_inter(2*height-1,2*width-1);
  42. %%%%%%%%%%%%%%%%%%%%%%%%%%%%
  43. %%%  EBMA Main Function  %%%
  44. %%%%%%%%%%%%%%%%%%%%%%%%%%%%
  45. mvx = zeros(height/N,width/N); 
  46. mvy = zeros(height/N,width/N); 
  47. for i = 1 : N : height - N +1 % for every block in the anchor frame : height
  48.     for j = 1 : N : width - N +1 % for every block in the anchor frame : width
  49.         MAD_min = 256 * N * N;
  50.         for k = -R : 0.5 : R
  51.             for kk = -R : 0.5 : R % for every search candidate
  52.                 if (2*(i+k)-1>0)&&(2*(j+kk)-1>0)&&(2*(i+k+N-1)-1<2*height+1)&&(2*(j+kk+N-1)-1<2*width+1) 
  53.                     % evaluate before MAD calculation 
  54.                     flag1_1 = 2*(i+k)-1;flag1_2 = 2*(i+k+N-1)-1;
  55.                     flag2_1 = 2*(j+kk)-1;flag2_2 = 2*(j+kk+N-1)-1;
  56.                     flag = f2_inter(flag1_1:2:flag1_2,flag2_1:2:flag2_2);
  57.                     MAD = sum(sum(abs(f1(i:i+N-1,j:j+N-1)-flag)));
  58.                     % calculate MAD for this candidate
  59.                     if MAD < MAD_min
  60.                         MAD_min = MAD;
  61.                         dy = k;
  62.                         dx = kk;
  63.                     end
  64.                 end
  65.             end
  66.         end
  67.         fp(i:i+N-1,j:j+N-1) = f2_inter(2*(i+dy)-1:2:2*(i+dy+N-1)-1,2*(j+dx)-1:2:2*(j+dx+N-1)-1); 
  68.         % put the best matching block in the predicted image
  69.         iblk = floor((i-1)/N)+1; % block index
  70.         jblk = floor((j-1)/N)+1; % block index
  71.         mvx(iblk,jblk) = dx; % record the estimated MV
  72.         mvy(iblk,jblk) = dy; % record the estimated MV
  73.     end
  74. end
  75. %%%%%%%%%%%%%%%%
  76. %%%  Figure  %%%
  77. %%%%%%%%%%%%%%%%
  78. subplot(2,2,1);quiver(mvx,mvy);title('Move Vector - Half Pixel EBMA');
  79. subplot(2,2,2);imshow(fp,[]);title('Predicted Image - Half Pixel EBMA')
  80. subplot(2,2,3);imshow(f1,[]);title('Anchor Frame - Half Pixel EBMA');
  81. subplot(2,2,4);imshow(f2,[]);title('Target Frame - Half Pixel EBMA')
  82. figure;imshow(f2-fp,[]);title('Error - Integer Pixel EBMA')
  83. %%%%%%%%%%%%%
  84. %%% PSNR  %%%
  85. %%%%%%%%%%%%%
  86. f_mse = sum(sum((f2-fp).^2))/(height*width);
  87. f_snr = (255^2)/f_mse;
  88. f_psnr = 10*log(f_snr);
  89. fprintf('nPSNR(Half Pixel EBMA) is  %f.n',f_psnr);