Tuesday, June 17, 2014

Matlab simulation code for Harris Corner Point Detection

  1. % Harris detector
  2. % The code calculates
  3. % the Harris Feature Points(FP) 
  4. % When u execute the code, the test image file opened
  5. % and u have to select by the mouse the region where u
  6. % want to find the Harris points, 
  7. % then the code will print out and display the feature
  8. % points in the selected region.
  9. % You can select the number of FPs by changing the variables 
  10. % max_N & min_N

  11. load Imag;

  12. I =double(frame);

  13. %****************************
  14. imshow(frame);

  15. k = waitforbuttonpress;
  16. point1 = get(gca,'CurrentPoint');  %button down detected
  17. rectregion = rbbox;  %%%return figure units
  18. point2 = get(gca,'CurrentPoint');%%%%button up detected
  19. point1 = point1(1,1:2); %%% extract col/row min and maxs
  20. point2 = point2(1,1:2);
  21. lowerleft = min(point1, point2);
  22. upperright = max(point1, point2); 
  23. ymin = round(lowerleft(1)); 

  24. %%% Length and height of the bounded rectangle region
  25. ymax = round(upperright(1));
  26. xmin = round(lowerleft(2));
  27. xmax = round(upperright(2));
  28. %***********************************

  29. Aj=6;
  30. cmin=xmin-Aj; cmax=xmax+Aj; rmin=ymin-Aj; rmax=ymax+Aj;
  31. min_N=12;max_N=16;

  32. %%%%%%%%%%%%%%Interest Points%%%%%%%%%%%%%%%%%%
  33. sigma=2; Thrshold=20; r=6; disp=1;
  34. dx = [-1 0 1; -1 0 1; -1 0 1]; % The Mask 
  35.     dy = dx';
  36.    
  37.     %%%%%% Derivatives of Ix and Iy
  38.     Ix = conv2(I(cmin:cmax,rmin:rmax), dx, 'same');   
  39.     Iy = conv2(I(cmin:cmax,rmin:rmax), dy, 'same');
  40.     
  41.      %%%%%% Gaussien Filter
  42.     g = fspecial('gaussian',max(1,fix(6*sigma)), sigma);
  43.     
  44.     %%%%% Convulation of Ix2, Iy2,Ixy using Gaussian window
  45.     Ix2 = conv2(Ix.^2, g, 'same');  
  46.     Iy2 = conv2(Iy.^2, g, 'same');
  47.     Ixy = conv2(Ix.*Iy, g,'same');
  48.    
  49.     %%%%%%%%%%%%%% Compute the cornerness values
  50.     k = 0.04;
  51.     R11 = (Ix2.*Iy2 - Ixy.^2) - k*(Ix2 + Iy2).^2;
  52.     R11=(1000/max(max(R11)))*R11;
  53.     R=R11;
  54.     ma=max(max(R));
  55.     sze = 2*r+1; 
  56.     MX = ordfilt2(R,sze^2,ones(sze));
  57.     R11 = (R==MX)&(R>Thrshold); 
  58.     count=sum(sum(R11(5:size(R11,1)-5,5:size(R11,2)-5)));
  59.     
  60.     %%%%% Find local maxima above some threshold to detect interest points 
  61.     loop=0;
  62.     while (((count<min_N)|(count>max_N))&(loop<30))
  63.         if count>max_N
  64.             Thrshold=Thrshold*1.5;
  65.         elseif count < min_N
  66.             Thrshold=Thrshold*0.5;
  67.         end
  68.         
  69.         R11 = (R==MX)&(R>Thrshold); 
  70.         count=sum(sum(R11(5:size(R11,1)-5,5:size(R11,2)-5)));
  71.         loop=loop+1;
  72.     end
  73.     
  74.     
  75. R=R*0;
  76.     R(5:size(R11,1)-5,5:size(R11,2)-5)=R11(5:size(R11,1)-5,5:size(R11,2)-5);
  77. [r1,c1] = find(R);
  78.     PIP=[r1+cmin,c1+rmin]
  79.    

  80.    %%%%%%%%%%%%%%%%%%%% Display
  81.    
  82.    Size_PI=size(PIP,1);
  83.    for r=1: Size_PI
  84.    I(PIP(r,1)-2:PIP(r,1)+2,PIP(r,2)-2)=255;
  85.    I(PIP(r,1)-2:PIP(r,1)+2,PIP(r,2)+2)=255;
  86.    I(PIP(r,1)-2,PIP(r,2)-2:PIP(r,2)+2)=255;
  87.    I(PIP(r,1)+2,PIP(r,2)-2:PIP(r,2)+2)=255;
  88.    
  89.    end
  90.    
  91.    imshow(uint8(I))

No comments:

Post a Comment