function eroded_image = erode2( source_image, neighborhood ); % ERODE2 erode a greyscale image. % eroded_image = erode2( source_image, neighborhood ); % erodes the image to make white objects smaller, black cracks larger, % (The MatLab function "erode.m" % does the same thing, but only accepts binary black-and-white image). % Assumes source_image is a array 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-02-15:DAV: modified to avoid black frame creepage. % 1998-03-26:DAV: updated to grayscale version. % 1998-02-04:DAV: David Cary wrote binary version % see medfilt2() for median filtering. if( isa(source_image, 'uint8') ), the_max = double( max(source_image(:)) ); % I thought this would be faster than converting back and forth to doubles, % but (according to 'profile') it is slightly slower. % elements = nnz(neighborhood); % % bitcmp(x, 8) = bitxor(x, 0xFF) = 0xFF - x. % eroded_image = bitcmp( ordfilt2(bitcmp(source_image, 8), elements, neighborhood), 8 ); % eroded_image = bitxor( ordfilt2(bitxor(source_image, 255), elements, neighborhood), 255 ); % It makes no difference whether "neighborhood" % is double or uint8. % neighborhood = uint8(neighborhood); eroded_image = uint8(ordfilt2(double(source_image) - the_max, 1, neighborhood) + the_max); else, the_max = max(source_image(:)); eroded_image = ordfilt2(source_image - the_max, 1, neighborhood) + the_max; end % end erode2.m