motionEst4SS.m
上传用户:peter9f
上传日期:2014-10-05
资源大小:510k
文件大小:6k
源码类别:

多媒体

开发平台:

Matlab

  1. % Computes motion vectors using Four Step Search method
  2. %
  3. % Based on the paper by Lai-Man Po, and Wing-Chung Ma
  4. % IEEE Trans. on Circuits and Systems for Video Technology
  5. % Volume 6, Number 3, June 1996 :  Pages 313:317
  6. %
  7. % Input
  8. %   imgP : The image for which we want to find motion vectors
  9. %   imgI : The reference image
  10. %   mbSize : Size of the macroblock
  11. %   p : Search parameter  (read literature to find what this means)
  12. %
  13. % Ouput
  14. %   motionVect : the motion vectors for each integral macroblock in imgP
  15. %   SS4computations: The average number of points searched for a macroblock
  16. %
  17. % Written by Aroh Barjatya
  18. function [motionVect, SS4Computations] = motionEst4SS(imgP, imgI, mbSize, p)
  19. [row col] = size(imgI);
  20. vectors = zeros(2,row*col/mbSize^2);
  21. costs = ones(3, 3) * 65537;
  22. % we start off from the top left of the image
  23. % we will walk in steps of mbSize
  24. % for every marcoblock that we look at we will look for
  25. % a close match p pixels on the left, right, top and bottom of it
  26. computations = 0;
  27. mbCount = 1;
  28. for i = 1 : mbSize : row-mbSize+1
  29.     for j = 1 : mbSize : col-mbSize+1
  30.         
  31.         % the 4 step search starts
  32.         % we are scanning in raster order
  33.         
  34.         x = j;
  35.         y = i;
  36.         
  37.         costs(2,2) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
  38.                                     imgI(i:i+mbSize-1,j:j+mbSize-1),mbSize);
  39.         computations = computations + 1;
  40.         
  41.         % This is the calculation of the 9 points
  42.         % As this is the first stage, we evaluate all 9 points
  43.         for m = -2 : 2 : 2        
  44.             for n = -2 : 2 : 2
  45.                 refBlkVer = y + m;   % row/Vert co-ordinate for ref block
  46.                 refBlkHor = x + n;   % col/Horizontal co-ordinate
  47.                 if ( refBlkVer < 1 || refBlkVer+mbSize-1 > row ...
  48.                      || refBlkHor < 1 || refBlkHor+mbSize-1 > col)
  49.                      continue;
  50.                 end
  51.                 costRow = m/2 + 2;
  52.                 costCol = n/2 + 2;
  53.                 if (costRow == 2 && costCol == 2)
  54.                     continue
  55.                 end
  56.                 costs(costRow, costCol ) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
  57.                     imgI(refBlkVer:refBlkVer+mbSize-1, refBlkHor:refBlkHor+mbSize-1), mbSize);
  58.                 computations = computations + 1;
  59.             end
  60.         end
  61.         
  62.         % Now we find the vector where the cost is minimum
  63.         % and store it ... 
  64.         
  65.         [dx, dy, cost] = minCost(costs);      % finds which macroblock in imgI gave us min Cost
  66.             
  67.               
  68.        
  69.         % The flag_4ss is set to 1 when the minimum
  70.         % is at the center of the search area           
  71.         
  72.         if (dx == 2 && dy == 2)
  73.             flag_4ss = 1;
  74.         else
  75.             flag_4ss = 0;
  76.             xLast = x;
  77.             yLast = y;
  78.             x = x + (dx-2)*2;
  79.             y = y + (dy-2)*2;
  80.         end
  81.         costs = ones(3,3) * 65537;
  82.         costs(2,2) = cost;
  83.         
  84.         stage = 1;
  85.         while (flag_4ss == 0 && stage <=2)
  86.             for m = -2 : 2 : 2        
  87.                 for n = -2 : 2 : 2
  88.                     refBlkVer = y + m;   % row/Vert co-ordinate for ref block
  89.                     refBlkHor = x + n;   % col/Horizontal co-ordinate
  90.                     if ( refBlkVer < 1 || refBlkVer+mbSize-1 > row ...
  91.                         || refBlkHor < 1 || refBlkHor+mbSize-1 > col)
  92.                         continue;
  93.                     end
  94.                     if (refBlkHor >= xLast - 2 && refBlkHor <= xLast + 2 ...
  95.                            && refBlkVer >= yLast - 2 && refBlkVer <= yLast + 2 )
  96.                         continue;
  97.                     end
  98.                     
  99.                     costRow = m/2 + 2;
  100.                     costCol = n/2 + 2;
  101.                     if (costRow == 2 && costCol == 2)
  102.                         continue
  103.                     end
  104.                            
  105.                     costs(costRow, costCol ) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
  106.                                             imgI(refBlkVer:refBlkVer+mbSize-1, ...
  107.                                               refBlkHor:refBlkHor+mbSize-1), mbSize);
  108.                     computations = computations + 1;
  109.                                               
  110.                 end
  111.             end
  112.                 
  113.             [dx, dy, cost] = minCost(costs);
  114.             
  115.             
  116.             if (dx == 2 && dy == 2)
  117.                 flag_4ss = 1;
  118.             else
  119.                 flag_4ss = 0;
  120.                 xLast = x;
  121.                 yLast = y;
  122.                 x = x + (dx-2)*2;
  123.                 y = y + (dy-2)*2;
  124.             end
  125.             
  126.             costs = ones(3,3) * 65537;
  127.             costs(2,2) = cost;
  128.             stage = stage + 1;
  129.            
  130.             
  131.         end  % while loop ends here
  132.         
  133.         
  134.         % we now enter the final stage
  135.         
  136.         for m = -1 : 1 : 1        
  137.             for n = -1 : 1 : 1
  138.                 refBlkVer = y + m;   % row/Vert co-ordinate for ref block
  139.                 refBlkHor = x + n;   % col/Horizontal co-ordinate
  140.                 if ( refBlkVer < 1 || refBlkVer+mbSize-1 > row ...
  141.                      || refBlkHor < 1 || refBlkHor+mbSize-1 > col)
  142.                      continue;
  143.                 end
  144.                 costRow = m + 2;
  145.                 costCol = n + 2;
  146.                 if (costRow == 2 && costCol == 2)
  147.                     continue
  148.                 end
  149.                 costs(costRow, costCol ) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
  150.                 imgI(refBlkVer:refBlkVer+mbSize-1, refBlkHor:refBlkHor+mbSize-1), mbSize);
  151.                 computations = computations + 1;
  152.             end
  153.         end
  154.         
  155.         % Now we find the vector where the cost is minimum
  156.         % and store it ... 
  157.         
  158.         [dx, dy, cost] = minCost(costs);
  159.         
  160.         x = x + dx - 2;
  161.         y = y + dy - 2;
  162.         
  163.         vectors(1,mbCount) = y - i;    % row co-ordinate for the vector
  164.         vectors(2,mbCount) = x - j;    % col co-ordinate for the vector            
  165.         mbCount = mbCount + 1;
  166.         costs = ones(3,3) * 65537;
  167.         
  168.     end
  169. end
  170.     
  171. motionVect = vectors;
  172. SS4Computations = computations/(mbCount - 1);
  173.     
  174.     
  175.     
  176.