SHARE
TWEET

[Matlab] Image Dilation

LightningIsMyName Mar 29th, 2011 381 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. % BI_dilate.m
  2. % Binary Dilation operation
  3. % Written by Barak Itkin, http://barak-itkin.blogspot.com
  4.  
  5. %%%%%%%%%%%%%%%%%%%% Image commands "cheat sheet" %%%%%%%%%%%%%%%%%%%%%%%%%
  6.  
  7. % im = imread (path);           % read an image file (try 'cameraman.tif'
  8. %                               % and 'peppers.png')
  9. % info = imfinfo (path);        % Get information about the image (use the
  10. %                               % same path!). Try printing this ;)
  11. %
  12. % if info.ColorType == 'truecolor' % Check if the image is RGB type
  13. %    img = rgb2gray(im);        % If so, convert to gray 0-255
  14. % else                          % Otherwise (if gray), do nothing
  15. %    img = im;                  % Nothing :)
  16. % end                           % End of if statement
  17. %
  18. % imgd = img >= 128;            % Preform thresholding with a limit of 128
  19. %                               % (127 or less => 0, 128 or more => 1)
  20. % imshow (imgd);                % Display some image in rgb/gray/0-1/0-255
  21.  
  22. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  23.  
  24. %%% BI_dilate (img, se) %%%
  25. % img           = a black-white image (binary 2D array)
  26. % se            = a binary array, where 1 is the area to match
  27. % dimg (return) = the dilated image
  28.  
  29. % Example useage - imshow (BI_dilate (img, ones(3)))
  30. function dimg = BI_dilate (img, se)
  31.  
  32. % Get the image width and height
  33. [h,w] = size (img);
  34.  
  35. % Get the structure element width and height
  36. [hse,wse] = size (se);
  37.  
  38. % The index of the middle of the structure elements
  39. % For example, in a 3x5 matrix we'll have (2,3). Don't forget that matlab
  40. % indexes begin in 1, and not in zero!
  41. xm = 1 + floor(wse / 2);
  42. ym = 1 + floor(hse / 2);
  43.  
  44. % Let's create a new image, from the source image, and pad it's edges
  45. % This will save us index troubles for referencing indexes outside of the
  46. % image on the edges.
  47.  
  48. % So, initialize the image with 0's (in dilation, we match 1', so
  49. % initializing with 0's means it won't change the effect)
  50. bigimg = zeros (size(img)+(size(se)-1));
  51. % Now, copy the original image to the right place in the big image
  52. bigimg(ym:(ym+h-1),xm:(xm+w-1)) = img;
  53.  
  54. % Create the result image
  55. dimg = zeros(size(img));
  56.  
  57. % Pass over all pixels in the source image
  58. for i=1:h
  59.     for j = 1:w
  60.         % Calculate the intersection of the se with the source image
  61.         % - We intersect with bigimg which is our source image with padding
  62.         %   on the edges to solve index problems on the edges
  63.         % - We use the .* operator which means element-by-element
  64.         %   multiplication (and not regular matrix multiplication)
  65.         % - We multiply element-by-element by the SE, so that areas which
  66.         %   had 1 in the SE will not change, and areas that had 0 will be
  67.         %   ignored
  68.         intersect = bigimg(i:(i+hse-1),j:(j+wse-1)) .* se;
  69.         % Now, we check if the intersection wasn't zero, and we put that in
  70.         % the result image
  71.         dimg(i,j) = sum (intersect(:) == 1);
  72.     end
  73. end
RAW Paste Data
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top