find_features.m
上传用户:trade789
上传日期:2018-05-10
资源大小:603k
文件大小:6k
源码类别:

2D图形编程

开发平台:

Matlab

  1. %/////////////////////////////////////////////////////////////////////////////////////////////
  2. %
  3. % find_features - scale space feature detector based upon difference of gaussian filters.
  4. %                 selects features based upon their maximum response in scale space
  5. %
  6. % Usage:  maxima = find_features(pyr, img, thresh, radius, radius2, min_sep, edgeratio, disp_flag, img_flag)
  7. %
  8. % Parameters:  
  9. %            pyr :      cell array of filtered image pyramid (built with build_pyramid)
  10. %            img :      original image (only used for visualization)
  11. %            thresh :   threshold value for maxima search (minimum filter response considered)
  12. %            radius :   radius for maxima comparison within current scale
  13. %            radius2:   radius for maxima comparison between neighboring scales
  14. %            disp_flag: 1- display each scale level on separate figure.  0 - display nothing
  15. %            img_flag: 1 - display filter responses. 0 - display original images.
  16. %
  17. % Returns:
  18. %
  19. %            maxima  - cell array of nX2 matrices of row,column coordinates of selected points on each scale level
  20. %
  21. % Author: 
  22. % Scott Ettinger
  23. % scott.m.ettinger@intel.com
  24. %
  25. % May 2002
  26. %/////////////////////////////////////////////////////////////////////////////////////////////
  27. function maxima = find_features(pyr, img, scl, thresh, radius, radius2, disp_flag, img_flag)
  28.   %                pts = find_features(pyr,img,scl,thresh,radius,radius2,disp_flag,1);
  29. levels = size(pyr);
  30. levels = levels(2);
  31. mcolor = [ 0 1 0;   %color array for display of features at different scales
  32.            0 1 0;
  33.            1 0 0;
  34.            .2 .5 0;
  35.            0 0 1;
  36.            1 0 1;
  37.            0 1 1;
  38.            1 .5 0
  39.            .5 1 0
  40.           0 1 .5
  41.            .5 1 .5];
  42. [himg,wimg] = size(img);                                            %get size of images
  43. [h,w] = size(pyr{2});
  44.     
  45. for i=2:levels-1
  46.     [h,w] = size(pyr{i});
  47.     [h2,w2] = size(pyr{i+1});
  48.     
  49.     %find maxima
  50.     mx = find_extrema(pyr{i},thresh,radius);                        %find maxima at current scale level
  51.     mx2 = round((mx-1)/scl) + 1;                                    %find coords in level above        
  52.     mx_above = neighbor_max(pyr{i},pyr{i+1},mx,mx2,radius2);        %do neighbor comparison in scale space above
  53.     if i>1
  54.         mx2 = round((mx-1)*scl) + 1;                                %find coords in level below     
  55.         mx_below = neighbor_max(pyr{i},pyr{i-1},mx,mx2,radius2);    %do comparison in scale below
  56.     
  57.         maxima{i} = plist(mx, mx_below & mx_above);                 %get coord list for retained maxima and minima
  58.     else
  59.         maxima{i} = plist(mx, mx_above);
  60.     end
  61.        
  62.     %find minima
  63.     %if i==11,
  64.     %    keyboard;
  65.     %end;
  66.     
  67.     mx = find_extrema(-pyr{i},thresh,radius);                       %find minima at current scale level
  68.     mx2 = round((mx-1)/scl) + 1;                                    %find coords in level above        
  69.     mx_above = neighbor_max(-pyr{i},-pyr{i+1},mx,mx2,radius2);      %do neighbor comparison in scale space above
  70.     if i>1
  71.         mx2 = round((mx-1)*scl) + 1;                                %find coords in level below     
  72.         mx_below = neighbor_max(-pyr{i},-pyr{i-1},mx,mx2,radius2);  %do comparison in scale below
  73.     
  74.         mxtemp = plist(mx, mx_below & mx_above);                    %get coord list for retained maxima and minima
  75.     else
  76.         mxtemp = plist(mx, mx_above);
  77.     end
  78.   
  79.     maxima{i} = [maxima{i}; mxtemp];                                %combine maxima and minima into list for return
  80.     
  81.     
  82.     %display results if desired
  83.     if disp_flag > 0                                                
  84.         figure
  85.         if img_flag == 0
  86.             tmp=resample_bilinear(img,himg/h);
  87.             imagesc(tmp);
  88.             colormap gray;
  89.             show_plist(maxima{i},mcolor(mod(i-1,7)+1,:),'+');
  90.         else
  91.             imagesc(pyr{i});
  92.             colormap gray;
  93.             show_plist(maxima{i},mcolor(mod(i-1,7)+1,:),'+');
  94.         end
  95.     end    
  96. end
  97.     
  98. %//////////////////////////////////////////////////////////////////////////////////////////////
  99. %
  100. %   Compare a vector of pixels with its neighbors in another scale 
  101. %
  102. %//////////////////////////////////////////////////////////////////////////////////////////////
  103. function v = neighbor_max(img1,img2,i,i2,radius)                    % i and i2 are column vectors of r,c coords
  104.     
  105.     if (size(i2,1))==0 | size(img2,1)<11 | size(img2,2)<11
  106.         v=zeros(length(i),1);
  107.     else
  108.         
  109.         [h,w] = size(img1);
  110.         [h2,w2] = size(img2);
  111.     
  112.         [y,x]=meshgrid(-20:20,-20:20);                              %create set of offsets within radius 
  113.         z = (x.^2+y.^2)<=radius^2;
  114.         [y,x]=find(z);
  115.         x=x-21; y=y-21;
  116.     
  117.         radius=ceil(radius);
  118.     
  119.         bound = ones(size(i2,1),2)*[h2-radius 0;0 w2-radius];        %create boundary listing
  120.         i2 = i2 - ((i2 > bound).*(i2-bound+1));                      %test bounds to make all points within image
  121.         i2 = i2 + ((i2 < radius+1).*(radius-i2+1));
  122.         
  123.         
  124.         i2 = vec(i2,h2);                                             %create indices from x,y coords
  125.         i = vec(i,h);
  126.     
  127.         p = img1(i);
  128.         res = ones(length(i),1);
  129.     
  130.         for j=1:length(x)                                            %check against all points within radius
  131.             itest = i2 + x(j) + h2*y(j);
  132.             p2 = img2(itest);
  133.             res = res & (p>=p2);
  134.         end
  135.         
  136.         v = res;                                                     %store results in binary vector
  137.     end
  138.     
  139. %//////////////////////////////////////////////////////////////////////////////////////////////
  140. function v = vec(points,h)
  141.     y = points(:,1);
  142.     x = points(:,2);
  143.     
  144.     v = y + (x-1)*h;  %create index vectors
  145.     
  146. %//////////////////////////////////////////////////////////////////////////////////////////////
  147. function p = plist(points, flags)
  148.     p = points(find(flags),:);
  149.    
  150.