Advertisement
Guest User

Untitled

a guest
Feb 27th, 2012
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 3.21 KB | None | 0 0
  1. function emap = canny1(img, tlow, thigh, fsize, sigma)
  2.     % INITIALIZING EMAP:
  3.     emap = zeros(size(img));
  4.     % CONVERTING TO GRAY SCALE:
  5.     size(size(img))
  6.     %img = rgb2gray(img);
  7.     % SMOOTHING FILTER | REDUCE NOISE:
  8.     filter = fspecial('gaussian', fsize, sigma);
  9.     img = im2double(img);
  10.     img = imfilter(img, filter);
  11.     % FINDING IMAGE DERIVATIVES:
  12.     % dx = img - [img(2:end, :); img(1,:)];
  13.     % dy = img - [img(:,2:end) img(:,1)];
  14.     [dx, dy] = gradient(img);
  15.     % GRADIENT MAGNITUDE:
  16.     magnitude = sqrt(dx.^2 + dy.^2);
  17.     % GRADIENT DIRECTION:
  18.     direction = atan2(dy, dx) * (180.0 / pi);
  19.     % ROUNDING DIRECTION:
  20.     negatives = direction < 0;                  % Getting the negative value locations.
  21.     direction = direction + negatives * 180;    % Correcting them.
  22.     % SORTING DIRECTIONS USING HISTOGRAM COUNT:
  23.     roundAngTo = [-inf, 45, 90, 135, inf];
  24.     % W-E,         NE-SW,         N-S,           NW-SE
  25.     % [0-45 -> 1], [45-90 -> 2] , [90-135 -> 3], [135-180 -> 4]
  26.     [notNeeded, newDirection] = histc(direction, roundAngTo);
  27.     nonSupress = magnitude;
  28.     % NON-MAXIMUM SUPPRESSION:
  29.     for m = 2:(size(img, 1) - 1)
  30.         for n = 2:(size(img, 2) - 1)
  31.             switch newDirection(m,n)
  32.                 case 1
  33.                 % INTERPOLATE [W-E]:
  34.                 if((magnitude(m,n) > magnitude(m - 1, n)) || (magnitude(m,n) > magnitude(m + 1, n)))
  35.                     nonSupress(m,n) = 0;
  36.                     if(magnitude(m,n) > thigh)
  37.                         emap(m,n) = 1;
  38.                     elseif(magnitude(m,n) > tlow)
  39.                         % CHECK PERPENDICULAR:
  40.                         if((magnitude(m, n - 1) > thigh) || (magnitude(m, n + 1) > thigh))
  41.                             emap(m,n) = 1;
  42.                         end
  43.                     end
  44.                 end
  45.                 case 2
  46.                 % INTERPOLATE [NE-SW]:
  47.                 if((magnitude(m,n) > magnitude(m + 1, n - 1)) || (magnitude(m,n) > magnitude(m - 1, n + 1)))
  48.                     nonSupress(m,n) = 0;
  49.                     if(magnitude(m,n) > thigh)
  50.                         emap(m,n) = 1;
  51.                     elseif(magnitude(m,n) > tlow)
  52.                         % CHECK PERPENDICULAR:
  53.                         if((magnitude(m - 1, n - 1) > thigh) || (magnitude(m + 1, n + 1) > thigh))
  54.                             emap(m,n) = 1;
  55.                         end
  56.                     end
  57.                 end
  58.                 case 3
  59.                 % INTERPOLATE [N-S]:
  60.                 if((magnitude(m,n) > magnitude(m, n - 1)) || (magnitude(m,n) > magnitude(m, n + 1)))
  61.                     nonSupress(m,n) = 0;
  62.                     if(magnitude(m,n) > thigh)
  63.                         emap(m,n) = 1;
  64.                     elseif(magnitude(m,n) > tlow)
  65.                         % CHECK PERPENDICULAR:
  66.                         if((magnitude(m + 1, n) > thigh) || (magnitude(m - 1, n) > thigh))
  67.                             emap(m,n) = 1;
  68.                         end
  69.                     end
  70.                 end
  71.                 case 4
  72.                 % INTERPOLATE [NW-SE]:
  73.                 if((magnitude(m,n) > magnitude(m - 1, n - 1)) || (magnitude(m,n) > magnitude(m + 1, n + 1)))
  74.                     nonSupress(m,n) = 0;
  75.                     if(magnitude(m,n) > thigh)
  76.                         emap(m,n) = 1;
  77.                     elseif(magnitude(m,n) > tlow)
  78.                         % CHECK PERPENDICULAR:
  79.                         if((magnitude(m - 1, n + 1) > thigh) || (magnitude(m + 1, n - 1) > thigh))
  80.                             emap(m,n) = 1;
  81.                         end
  82.                     end
  83.                 end
  84.             end
  85.         end
  86.     end
  87.    
  88.     % CONVERTING TO BINARY MASK:
  89.     nonSupress = nonSupress > 0;
  90.     % SHOW RESULT:
  91.     subplot(2,3,1); imshow(img), title('Smoothed Image')
  92.     subplot(2,3,2), imshow(magnitude), title('Magnitude');
  93.     subplot(2,3,3), imshow(dx), title('dx');
  94.     subplot(2,3,4), imshow(dy), title('dy');
  95.     subplot(2,3,5), imshow(nonSupress), title('Supression');
  96.     subplot(2,3,6), imshow(emap), title('EMAP');
  97.     %figure; imagesc(nonSupress);
  98.     %figure; imshow(emap,[]);
  99. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement