function dilated_image = dilate2( source_image, neighborhood ); % DILATE2 dilate a greyscale image. % dilated_image = dilate2( source_image, neighborhood ); % dilates the image to make white objects larger, black cracks smaller. % (The MatLab function "dilate.m" % does the same thing, but only accepts binary black-and-white image). % Assumes source_image is a array of 0 = black, max(source_image)=white pixels; % Assumes neighborhood is a array of % 1 = neighbor pixels, 0 = non-neighbor pixels. % See also ERODE2, OPEN2, CLOSE2, DILATE. % Change log: % 1999-05-06:DAV: merged Scott Acton's bug fix. % 1998-03-26:DAV: updated to grayscale version. % 1998-02-04:DAV: David Cary wrote binary version elements = nnz(neighborhood); % Rotate halfway around % to make "dilate2" consistent with MatLab's "dilate" command. % This obviously makes no difference with % symmetrical structuring elements such as circle(11), % but if I forget to rotate, then % "open2" and "close2" don't work right % for non-symmetrical neighborhoods, % or for even-sized neighborhoods such as circle(10), source_rotate = rot90(source_image,2); dilated_rotate = ordfilt2(source_rotate, elements, neighborhood); dilated_image = rot90(dilated_rotate,2); % the following version gives correct results, but according to profile % it is exactly the same speed as the above. %if(1), %[y,x] = size(neighborhood); %rotated_neighborhood = rot90(neighborhood,2); %if( 0 == mod( x, 2 ) ), % % even widths cause unwanted shift of edges, so make it odd: % rotated_neighborhood(y,x+1) = 0; %end %if( 0 == mod( x, 2 ) ), % % even heights cause unwanted shift of edges, so make it odd: % rotated_neighborhood(y+1,x) = 0; %end %dilated_image = ordfilt2(source_image, elements, rotated_neighborhood); %end; % end dilate2.m