Advertisement
Guest User

Matlab code

a guest
Aug 21st, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.54 KB | None | 0 0
  1. % Computes motion vectors using Three Step Search method
  2. %
  3. % Input
  4. %   imgP : The image for which we want to find motion vectors
  5. %   imgI : The reference image
  6. %   mbSize : Size of the macroblock
  7. %   p : Search parameter  (read literature to find what this means)
  8. %
  9. % Ouput
  10. %   motionVect : the motion vectors for each integral macroblock in imgP
  11. %   TSScomputations: The average number of points searched for a macroblock
  12. %
  13. % Written by Aroh Barjatya
  14.  
  15.  
  16. function [motionVect, TSScomputations] = motionEstTSS(imgP, imgI, mbSize, p)
  17.  
  18. [row col] = size(imgI);
  19.  
  20. vectors = zeros(2,row*col/mbSize^2);
  21. costs = ones(3, 3) * 65537;
  22.  
  23. computations = 0;
  24.  
  25. % we now take effectively log to the base 2 of p
  26. % this will give us the number of steps required
  27.  
  28. L = floor(log10(p+1)/log10(2));  
  29. stepMax = 2^(L-1);
  30.  
  31. % we start off from the top left of the image
  32. % we will walk in steps of mbSize
  33. % for every marcoblock that we look at we will look for
  34. % a close match p pixels on the left, right, top and bottom of it
  35.  
  36. mbCount = 1;
  37. for i = 1 : mbSize : row-mbSize+1
  38.     for j = 1 : mbSize : col-mbSize+1
  39.        
  40.         % the three step search starts
  41.         % we will evaluate 9 elements at every step
  42.         % read the literature to find out what the pattern is
  43.         % my variables have been named aptly to reflect their significance
  44.  
  45.         x = j;
  46.         y = i;
  47.        
  48.         % In order to avoid calculating the center point of the search
  49.         % again and again we always store the value for it from teh
  50.         % previous run. For the first iteration we store this value outside
  51.         % the for loop, but for subsequent iterations we store the cost at
  52.         % the point where we are going to shift our root.
  53.        
  54.         costs(2,2) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
  55.                                     imgI(i:i+mbSize-1,j:j+mbSize-1),mbSize);
  56.        
  57.         computations = computations + 1;
  58.         stepSize = stepMax;              
  59.  
  60.         while(stepSize >= 1)  
  61.  
  62.             % m is row(vertical) index
  63.             % n is col(horizontal) index
  64.             % this means we are scanning in raster order
  65.             for m = -stepSize : stepSize : stepSize        
  66.                 for n = -stepSize : stepSize : stepSize
  67.                     refBlkVer = y + m;   % row/Vert co-ordinate for ref block
  68.                     refBlkHor = x + n;   % col/Horizontal co-ordinate
  69.                     if ( refBlkVer < 1 || refBlkVer+mbSize-1 > row ...
  70.                         || refBlkHor < 1 || refBlkHor+mbSize-1 > col)
  71.                         continue;
  72.                     end
  73.  
  74.  
  75.                     costRow = m/stepSize + 2;
  76.                     costCol = n/stepSize + 2;
  77.                     if (costRow == 2 && costCol == 2)
  78.                         continue
  79.                     end
  80.                     costs(costRow, costCol ) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
  81.                         imgI(refBlkVer:refBlkVer+mbSize-1, refBlkHor:refBlkHor+mbSize-1), mbSize);
  82.                    
  83.                     computations = computations + 1;
  84.                 end
  85.             end
  86.        
  87.             % Now we find the vector where the cost is minimum
  88.             % and store it ... this is what will be passed back.
  89.        
  90.             [dx, dy, min] = minCost(costs);      % finds which macroblock in imgI gave us min Cost
  91.            
  92.            
  93.             % shift the root for search window to new minima point
  94.  
  95.             x = x + (dx-2)*stepSize;
  96.             y = y + (dy-2)*stepSize;
  97.            
  98.             % Arohs thought: At this point we can check and see if the
  99.             % shifted co-ordinates are exactly the same as the root
  100.             % co-ordinates of the last step, then we check them against a
  101.             % preset threshold, and ifthe cost is less then that, than we
  102.             % can exit from teh loop right here. This way we can save more
  103.             % computations. However, as this is not implemented in the
  104.             % paper I am modeling, I am not incorporating this test.
  105.             % May be later...as my own addition to the algorithm
  106.            
  107.             stepSize = stepSize / 2;
  108.             costs(2,2) = costs(dy,dx);
  109.            
  110.         end
  111.         vectors(1,mbCount) = y - i;    % row co-ordinate for the vector
  112.         vectors(2,mbCount) = x - j;    % col co-ordinate for the vector            
  113.         mbCount = mbCount + 1;
  114.         costs = ones(3,3) * 65537;
  115.     end
  116. end
  117.  
  118. motionVect = vectors;
  119. TSScomputations = computations/(mbCount - 1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement