function y = mmreadgray( filename ) % MMREADGRAY Read an image from a commercial file format and stores it as a gray-scale image. % % Y = MMREADGRAY( FILENAME ) % % Input: % FILENAME - String. Name of file. Possible extensions: tif, hdf, % jpg, bmp, pcx, xwd. % % Output: % Y - Gray-scale (uint8 or uint16) or binary image (logical uint8). % % MMIMREAD reads the image in FILENAME and stores it in Y, an uint8 % gray-scale image (without colormap). If the input file is a color % image, it is converted to gray-scale. This function calls the % MATLAB function IMREAD. % % Examples % -------- % % a=mmreadgray('small_bw.tif'); % mmshow(a) % % See also MMWRITE. % begin of the default argument automatic treatment % end of the default argument automatic treatment 1 [xin map] = imread(filename); % if there is no map, then nothing to do if isempty(map) if (size(xin,3) == 1) % normal gray scale without colormap y = xin; return; elseif (size(xin,3) == 3) % true-color RGB image if all(all(((xin(:,:,1) == xin(:,:,2)) & (xin(:,:,1) == xin(:,:,2))))) y = xin(:,:,1); return; else disp('Warning: converting true-color RGB image to gray'); if isa(xin, 'uint8') y = uint8(0.2989 * double(xin(:,:,1)) + ... 0.5870 * double(xin(:,:,2)) + ... 0.1140 * double(xin(:,:,3))); return; else y = 0.2989 * xin(:,:,1) + 0.5870 * xin(:,:,2) + 0.1140 * xin(:,:,3); return; end end else disp('Error: image with unknow number of bands'); end end if isa(xin, 'uint8') xin = double(xin)+1; % Switch to one based indexing end if size(map,2) ~= 3 disp('Error: colormap not of size 3') return; end % test if colormap is gray if ~all(map(:,1) == map(:,2) & map(:,1) == map(:,3)) map = rgb2ntsc(map); % if not, convert disp('Warning: converting RGB colored indexed image to gray'); end if (size(map,1) <= 256) y = uint8(zeros(size(xin))); y(:) = uint8(map(xin,1) * 255); if (size(map,1) == 2 & min(map(:,1)) == 0 & max(map(:,1)) == 1 ) y = mmbinary(y); disp('binary image'); elseif ~any(map(3:end,:)) y = mmbinary(y); disp('binary image'); else disp('uint8 gray-scale image'); end else y = uint16(zeros(size(xin))); y(:) = uint16(map(xin,1) * 65535); disp('uint16 gray-scale image'); end return; % Copyright (c) 1998-2001 by SDC Information Systems.