Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % BI_dilate.m
- % Binary Dilation operation
- % Written by Barak Itkin, http://barak-itkin.blogspot.com
- %%%%%%%%%%%%%%%%%%%% Image commands "cheat sheet" %%%%%%%%%%%%%%%%%%%%%%%%%
- % im = imread (path); % read an image file (try 'cameraman.tif'
- % % and 'peppers.png')
- % info = imfinfo (path); % Get information about the image (use the
- % % same path!). Try printing this ;)
- %
- % if info.ColorType == 'truecolor' % Check if the image is RGB type
- % img = rgb2gray(im); % If so, convert to gray 0-255
- % else % Otherwise (if gray), do nothing
- % img = im; % Nothing :)
- % end % End of if statement
- %
- % imgd = img >= 128; % Preform thresholding with a limit of 128
- % % (127 or less => 0, 128 or more => 1)
- % imshow (imgd); % Display some image in rgb/gray/0-1/0-255
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %%% BI_dilate (img, se) %%%
- % img = a black-white image (binary 2D array)
- % se = a binary array, where 1 is the area to match
- % dimg (return) = the dilated image
- % Example useage - imshow (BI_dilate (img, ones(3)))
- function dimg = BI_dilate (img, se)
- % Get the image width and height
- [h,w] = size (img);
- % Get the structure element width and height
- [hse,wse] = size (se);
- % The index of the middle of the structure elements
- % For example, in a 3x5 matrix we'll have (2,3). Don't forget that matlab
- % indexes begin in 1, and not in zero!
- xm = 1 + floor(wse / 2);
- ym = 1 + floor(hse / 2);
- % Let's create a new image, from the source image, and pad it's edges
- % This will save us index troubles for referencing indexes outside of the
- % image on the edges.
- % So, initialize the image with 0's (in dilation, we match 1', so
- % initializing with 0's means it won't change the effect)
- bigimg = zeros (size(img)+(size(se)-1));
- % Now, copy the original image to the right place in the big image
- bigimg(ym:(ym+h-1),xm:(xm+w-1)) = img;
- % Create the result image
- dimg = zeros(size(img));
- % Pass over all pixels in the source image
- for i=1:h
- for j = 1:w
- % Calculate the intersection of the se with the source image
- % - We intersect with bigimg which is our source image with padding
- % on the edges to solve index problems on the edges
- % - We use the .* operator which means element-by-element
- % multiplication (and not regular matrix multiplication)
- % - We multiply element-by-element by the SE, so that areas which
- % had 1 in the SE will not change, and areas that had 0 will be
- % ignored
- intersect = bigimg(i:(i+hse-1),j:(j+wse-1)) .* se;
- % Now, we check if the intersection wasn't zero, and we put that in
- % the result image
- dimg(i,j) = sum (intersect(:) == 1);
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement