motionEstDS.m
上传用户:cxsjwj
上传日期:2022-08-09
资源大小:34k
文件大小:19k
源码类别:

matlab例程

开发平台:

Matlab

  1. % Computes motion vectors using Diamond Search method
  2. %
  3. % Based on the paper by Shan Zhu, and Kai-Kuang Ma
  4. % IEEE Trans. on Image Processing
  5. % Volume 9, Number 2, February 2000 :  Pages 287:290
  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. %   DScomputations: The average number of points searched for a macroblock
  16. %
  17. % Written by Aroh Barjatya
  18. function motionVect = motionEstDS(imgP, imgI, mbSize, p)
  19. [row col] = size(imgI);
  20. vectors = zeros(2,row*col/mbSize^2);
  21. costs = ones(1, 9) * 65537;
  22. % we now take effectively log to the base 2 of p
  23. % this will give us the number of steps required
  24. L = floor(log10(p+1)/log10(2));   
  25. % The index points for Large Diamond search pattern
  26. LDSP(1,:) = [ 0 -2];
  27. LDSP(2,:) = [-1 -1]; 
  28. LDSP(3,:) = [ 1 -1];
  29. LDSP(4,:) = [-2  0];
  30. LDSP(5,:) = [ 0  0];
  31. LDSP(6,:) = [ 2  0];
  32. LDSP(7,:) = [-1  1];
  33. LDSP(8,:) = [ 1  1];
  34. LDSP(9,:) = [ 0  2];
  35. % The index points for Small Diamond search pattern
  36. SDSP(1,:) = [ 0 -1];
  37. SDSP(2,:) = [-1  0];
  38. SDSP(3,:) = [ 0  0];
  39. SDSP(4,:) = [ 1  0];
  40. SDSP(5,:) = [ 0  1];
  41. % we start off from the top left of the image
  42. % we will walk in steps of mbSize
  43. % for every marcoblock that we look at we will look for
  44. % a close match p pixels on the left, right, top and bottom of it
  45. % computations = 0;
  46. mbCount = 1;
  47. for i = 1 : mbSize : row-mbSize+1
  48.     for j = 1 : mbSize : col-mbSize+1
  49.         
  50.         % the Diamond search starts
  51.         % we are scanning in raster order
  52.         
  53.         x = j;
  54.         y = i;
  55.         
  56.         costs(5) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
  57.                                     imgI(i:i+mbSize-1,j:j+mbSize-1),mbSize);
  58. %        computations = computations + 1;
  59.         
  60.         % This is the first search so we evaluate all the 9 points in LDSP
  61.         for k = 1:9
  62.             refBlkVer = y + LDSP(k,2);   % row/Vert co-ordinate for ref block
  63.             refBlkHor = x + LDSP(k,1);   % col/Horizontal co-ordinate
  64.             if ( refBlkVer < 1 | refBlkVer+mbSize-1 > row ...
  65.                  | refBlkHor < 1 | refBlkHor+mbSize-1 > col)
  66.                 continue;
  67.             end
  68.             if (k == 5)
  69.                 continue
  70.             end
  71.             costs(k) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
  72.                   imgI(refBlkVer:refBlkVer+mbSize-1, refBlkHor:refBlkHor+mbSize-1), mbSize);
  73.             %computations = computations + 1;
  74.         end
  75.         
  76.         [cost, point] = min(costs);
  77.         
  78.         
  79.         % The SDSPFlag is set to 1 when the minimum
  80.         % is at the center of the diamond           
  81.         
  82.         if (point == 5)
  83.             SDSPFlag = 1;
  84.         else
  85.             SDSPFlag = 0;
  86.             if ( abs(LDSP(point,1)) == abs(LDSP(point,2)) )
  87.                 cornerFlag = 0;
  88.             else
  89.                 cornerFlag = 1; % the x and y co-ordinates not equal on corners
  90.             end
  91.             xLast = x;
  92.             yLast = y;
  93.             x = x + LDSP(point, 1);
  94.             y = y + LDSP(point, 2);
  95.             costs = ones(1,9) * 65537;
  96.             costs(5) = cost;
  97.         end
  98.         
  99.            
  100.         while (SDSPFlag == 0)
  101.             if (cornerFlag == 1)
  102.                 for k = 1:9
  103.                     refBlkVer = y + LDSP(k,2);   % row/Vert co-ordinate for ref block
  104.                     refBlkHor = x + LDSP(k,1);   % col/Horizontal co-ordinate
  105.                     if ( refBlkVer < 1 | refBlkVer+mbSize-1 > row ...
  106.                         | refBlkHor < 1 | refBlkHor+mbSize-1 > col)
  107.                         continue;
  108.                     end
  109.                     if (k == 5)
  110.                         continue
  111.                     end
  112.             
  113.                     if ( refBlkHor >= xLast - 1  & refBlkHor <= xLast + 1 ...
  114.                             & refBlkVer >= yLast - 1  & refBlkVer <= yLast + 1 )
  115.                         continue;
  116.                     elseif (refBlkHor < j-p | refBlkHor > j+p | refBlkVer < i-p ...
  117.                             | refBlkVer > i+p)
  118.                         continue;
  119.                     else
  120.                         costs(k) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
  121.                                        imgI(refBlkVer:refBlkVer+mbSize-1, ...
  122.                                           refBlkHor:refBlkHor+mbSize-1), mbSize);
  123.                         %computations = computations + 1;
  124.                     end
  125.                 end
  126.                 
  127.             else
  128.                 switch point
  129.                     case 2
  130.                         refBlkVer = y + LDSP(1,2);   % row/Vert co-ordinate for ref block
  131.                         refBlkHor = x + LDSP(1,1);   % col/Horizontal co-ordinate
  132.                         if ( refBlkVer < 1 | refBlkVer+mbSize-1 > row ...
  133.                             | refBlkHor < 1 | refBlkHor+mbSize-1 > col)
  134.                            % do nothing, outside image boundary
  135.                         elseif (refBlkHor < j-p | refBlkHor > j+p | refBlkVer < i-p ...
  136.                             | refBlkVer > i+p)
  137.                             % do nothing, outside search window
  138.                         else 
  139.                            costs(1) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
  140.                                        imgI(refBlkVer:refBlkVer+mbSize-1, ...
  141.                                           refBlkHor:refBlkHor+mbSize-1), mbSize);
  142.                          %  computations = computations + 1;
  143.                         end
  144.                                    
  145.                         refBlkVer = y + LDSP(2,2);   % row/Vert co-ordinate for ref block
  146.                         refBlkHor = x + LDSP(2,1);   % col/Horizontal co-ordinate
  147.                         if ( refBlkVer < 1 | refBlkVer+mbSize-1 > row ...
  148.                             | refBlkHor < 1 | refBlkHor+mbSize-1 > col)
  149.                            % do nothing, outside image boundary
  150.                         elseif (refBlkHor < j-p | refBlkHor > j+p | refBlkVer < i-p ...
  151.                             | refBlkVer > i+p)
  152.                             % do nothing, outside search window
  153.                         else
  154.                          
  155.                            costs(2) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
  156.                                        imgI(refBlkVer:refBlkVer+mbSize-1, ...
  157.                                           refBlkHor:refBlkHor+mbSize-1), mbSize);
  158.                           % computations = computations + 1;
  159.                         end
  160.                         
  161.                         refBlkVer = y + LDSP(4,2);   % row/Vert co-ordinate for ref block
  162.                         refBlkHor = x + LDSP(4,1);   % col/Horizontal co-ordinate
  163.                         if ( refBlkVer < 1 | refBlkVer+mbSize-1 > row ...
  164.                             | refBlkHor < 1 | refBlkHor+mbSize-1 > col)
  165.                            % do nothing, outside image boundary
  166.                         elseif (refBlkHor < j-p | refBlkHor > j+p | refBlkVer < i-p ...
  167.                             | refBlkVer > i+p)
  168.                             % do nothing, outside search window
  169.                         else
  170.                          
  171.                            costs(4) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
  172.                                        imgI(refBlkVer:refBlkVer+mbSize-1, ...
  173.                                           refBlkHor:refBlkHor+mbSize-1), mbSize);
  174.                            %computations = computations + 1;
  175.                         end
  176.                      
  177.                     case 3
  178.                         refBlkVer = y + LDSP(1,2);   % row/Vert co-ordinate for ref block
  179.                         refBlkHor = x + LDSP(1,1);   % col/Horizontal co-ordinate
  180.                         if ( refBlkVer < 1 | refBlkVer+mbSize-1 > row ...
  181.                             | refBlkHor < 1 | refBlkHor+mbSize-1 > col)
  182.                            % do nothing, outside image boundary
  183.                         elseif (refBlkHor < j-p | refBlkHor > j+p | refBlkVer < i-p ...
  184.                             | refBlkVer > i+p)
  185.                             % do nothing, outside search window
  186.                         else
  187.                          
  188.                            costs(1) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
  189.                                        imgI(refBlkVer:refBlkVer+mbSize-1, ...
  190.                                           refBlkHor:refBlkHor+mbSize-1), mbSize);
  191.                            %computations = computations + 1;
  192.                         end
  193.                                    
  194.                         refBlkVer = y + LDSP(3,2);   % row/Vert co-ordinate for ref block
  195.                         refBlkHor = x + LDSP(3,1);   % col/Horizontal co-ordinate
  196.                         if ( refBlkVer < 1 | refBlkVer+mbSize-1 > row ...
  197.                             | refBlkHor < 1 | refBlkHor+mbSize-1 > col)
  198.                            % do nothing, outside image boundary
  199.                         elseif (refBlkHor < j-p | refBlkHor > j+p | refBlkVer < i-p ...
  200.                             | refBlkVer > i+p)
  201.                             % do nothing, outside search window
  202.                         else
  203.                             
  204.                            costs(3) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
  205.                                        imgI(refBlkVer:refBlkVer+mbSize-1, ...
  206.                                           refBlkHor:refBlkHor+mbSize-1), mbSize);
  207.                            %computations = computations + 1;
  208.                         end
  209.                         
  210.                         refBlkVer = y + LDSP(6,2);   % row/Vert co-ordinate for ref block
  211.                         refBlkHor = x + LDSP(6,1);   % col/Horizontal co-ordinate
  212.                         if ( refBlkVer < 1 | refBlkVer+mbSize-1 > row ...
  213.                             | refBlkHor < 1 | refBlkHor+mbSize-1 > col)
  214.                            % do nothing, outside image boundary
  215.                         elseif (refBlkHor < j-p | refBlkHor > j+p | refBlkVer < i-p ...
  216.                             | refBlkVer > i+p)
  217.                             % do nothing, outside search window
  218.                         else
  219.                              
  220.                            costs(6) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
  221.                                        imgI(refBlkVer:refBlkVer+mbSize-1, ...
  222.                                           refBlkHor:refBlkHor+mbSize-1), mbSize);
  223.                            %computations = computations + 1;
  224.                         end
  225.                         
  226.                         
  227.                     case 7
  228.                         refBlkVer = y + LDSP(4,2);   % row/Vert co-ordinate for ref block
  229.                         refBlkHor = x + LDSP(4,1);   % col/Horizontal co-ordinate
  230.                         if ( refBlkVer < 1 | refBlkVer+mbSize-1 > row ...
  231.                             | refBlkHor < 1 | refBlkHor+mbSize-1 > col)
  232.                            % do nothing, outside image boundary
  233.                         elseif (refBlkHor < j-p | refBlkHor > j+p | refBlkVer < i-p ...
  234.                             | refBlkVer > i+p)
  235.                             % do nothing, outside search window
  236.                             
  237.                         else 
  238.                            costs(4) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
  239.                                        imgI(refBlkVer:refBlkVer+mbSize-1, ...
  240.                                           refBlkHor:refBlkHor+mbSize-1), mbSize);
  241.                            %computations = computations + 1;
  242.                         end
  243.                                    
  244.                         refBlkVer = y + LDSP(7,2);   % row/Vert co-ordinate for ref block
  245.                         refBlkHor = x + LDSP(7,1);   % col/Horizontal co-ordinate
  246.                         if ( refBlkVer < 1 | refBlkVer+mbSize-1 > row ...
  247.                             | refBlkHor < 1 | refBlkHor+mbSize-1 > col)
  248.                            % do nothing, outside image boundary
  249.                         elseif (refBlkHor < j-p | refBlkHor > j+p | refBlkVer < i-p ...
  250.                             | refBlkVer > i+p)
  251.                             % do nothing, outside search window
  252.                             
  253.                         else 
  254.                            costs(7) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
  255.                                        imgI(refBlkVer:refBlkVer+mbSize-1, ...
  256.                                           refBlkHor:refBlkHor+mbSize-1), mbSize);
  257.                            %computations = computations + 1;
  258.                         end
  259.                         
  260.                         refBlkVer = y + LDSP(9,2);   % row/Vert co-ordinate for ref block
  261.                         refBlkHor = x + LDSP(9,1);   % col/Horizontal co-ordinate
  262.                         if ( refBlkVer < 1 | refBlkVer+mbSize-1 > row ...
  263.                             | refBlkHor < 1 | refBlkHor+mbSize-1 > col)
  264.                            % do nothing, outside image boundary
  265.                         elseif (refBlkHor < j-p | refBlkHor > j+p | refBlkVer < i-p ...
  266.                             | refBlkVer > i+p)
  267.                             % do nothing, outside search window
  268.                             
  269.                         else 
  270.                            costs(9) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
  271.                                        imgI(refBlkVer:refBlkVer+mbSize-1, ...
  272.                                           refBlkHor:refBlkHor+mbSize-1), mbSize);
  273.                            %computations = computations + 1;
  274.                         end
  275.                         
  276.                     
  277.                     case 8
  278.                         refBlkVer = y + LDSP(6,2);   % row/Vert co-ordinate for ref block
  279.                         refBlkHor = x + LDSP(6,1);   % col/Horizontal co-ordinate
  280.                         if ( refBlkVer < 1 | refBlkVer+mbSize-1 > row ...
  281.                             | refBlkHor < 1 | refBlkHor+mbSize-1 > col)
  282.                            % do nothing, outside image boundary
  283.                         elseif (refBlkHor < j-p | refBlkHor > j+p | refBlkVer < i-p ...
  284.                             | refBlkVer > i+p)
  285.                             % do nothing, outside search window
  286.                             
  287.                         else 
  288.                            costs(6) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
  289.                                        imgI(refBlkVer:refBlkVer+mbSize-1, ...
  290.                                           refBlkHor:refBlkHor+mbSize-1), mbSize);
  291.                            %computations = computations + 1;
  292.                         end
  293.                                    
  294.                         refBlkVer = y + LDSP(8,2);   % row/Vert co-ordinate for ref block
  295.                         refBlkHor = x + LDSP(8,1);   % col/Horizontal co-ordinate
  296.                         if ( refBlkVer < 1 | refBlkVer+mbSize-1 > row ...
  297.                             | refBlkHor < 1 | refBlkHor+mbSize-1 > col)
  298.                            % do nothing, outside image boundary
  299.                         elseif (refBlkHor < j-p | refBlkHor > j+p | refBlkVer < i-p ...
  300.                             | refBlkVer > i+p)
  301.                             % do nothing, outside search window
  302.                             
  303.                         else 
  304.                            costs(8) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
  305.                                        imgI(refBlkVer:refBlkVer+mbSize-1, ...
  306.                                           refBlkHor:refBlkHor+mbSize-1), mbSize);
  307.                            %computations = computations + 1;
  308.                         end
  309.                         
  310.                         refBlkVer = y + LDSP(9,2);   % row/Vert co-ordinate for ref block
  311.                         refBlkHor = x + LDSP(9,1);   % col/Horizontal co-ordinate
  312.                         if ( refBlkVer < 1 | refBlkVer+mbSize-1 > row ...
  313.                             | refBlkHor < 1 | refBlkHor+mbSize-1 > col)
  314.                            % do nothing, outside image boundary
  315.                         elseif (refBlkHor < j-p | refBlkHor > j+p | refBlkVer < i-p ...
  316.                             | refBlkVer > i+p)
  317.                             % do nothing, outside search window
  318.                             
  319.                         else 
  320.                            costs(9) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
  321.                                        imgI(refBlkVer:refBlkVer+mbSize-1, ...
  322.                                           refBlkHor:refBlkHor+mbSize-1), mbSize);
  323.                            %computations = computations + 1;
  324.                         end
  325.                     otherwise
  326.                 end
  327.             end
  328.             
  329.             [cost, point] = min(costs);
  330.            
  331.             if (point == 5)
  332.                 SDSPFlag = 1;
  333.             else
  334.                 SDSPFlag = 0;
  335.                 if ( abs(LDSP(point,1)) == abs(LDSP(point,2)) )
  336.                     cornerFlag = 0;
  337.                 else
  338.                     cornerFlag = 1;
  339.                 end
  340.                 xLast = x;
  341.                 yLast = y;
  342.                 x = x + LDSP(point, 1);
  343.                 y = y + LDSP(point, 2);
  344.                 costs = ones(1,9) * 65537;
  345.                 costs(5) = cost;
  346.             end
  347.             
  348.         end  % while loop ends here
  349.         
  350.         % we now enter the SDSP calculation domain
  351.         costs = ones(1,5) * 65537;
  352.         costs(3) = cost;
  353.         
  354.         for k = 1:5
  355.             refBlkVer = y + SDSP(k,2);   % row/Vert co-ordinate for ref block
  356.             refBlkHor = x + SDSP(k,1);   % col/Horizontal co-ordinate
  357.             if ( refBlkVer < 1 | refBlkVer+mbSize-1 > row ...
  358.                   | refBlkHor < 1 | refBlkHor+mbSize-1 > col)
  359.                 continue; % do nothing, outside image boundary
  360.             elseif (refBlkHor < j-p | refBlkHor > j+p | refBlkVer < i-p ...
  361.                             | refBlkVer > i+p)
  362.                 continue;   % do nothing, outside search window
  363.             end
  364.             if (k == 3)
  365.                 continue
  366.             end
  367.             
  368.             costs(k) = costFuncMAD(imgP(i:i+mbSize-1,j:j+mbSize-1), ...
  369.                               imgI(refBlkVer:refBlkVer+mbSize-1, ...
  370.                                   refBlkHor:refBlkHor+mbSize-1), mbSize);
  371.             %computations = computations + 1;
  372.                    
  373.         end
  374.          
  375.         [cost, point] = min(costs);
  376.         
  377.         x = x + SDSP(point, 1);
  378.         y = y + SDSP(point, 2);
  379.         
  380.         vectors(1,mbCount) = y - i;    % row co-ordinate for the vector
  381.         vectors(2,mbCount) = x - j;    % col co-ordinate for the vector            
  382.         mbCount = mbCount + 1;
  383.         costs = ones(1,9) * 65537;
  384.         
  385.     end
  386. end
  387.     
  388. motionVect = vectors;
  389. %DScomputations = computations/(mbCount - 1);
  390.     
  391.     
  392.     
  393.