%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %Reads a PGM image into a Matrix (Grayscale format) % %Written by: % Alexander Stoytchev % College of Computing % Georgia Tech % saho@cc.gatech.edu % http://www.cc.gatech.edu/~saho % % Returns: % On success a (R,C) matrix is returned. % On failure the function returns -1; % % Note: No color scaling is performed, i.e. the number of colors read % from the PGM file is ignored. % % Last modified: Nov 28, 1998 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function IMAGE_GRAY=imread_pgm(fname) fid=fopen(fname,'r'); if fid== -1 IMAGE_GRAY=-1; else magic=fgetl(fid); if ~strcmp(magic,'P5') IMAGE_GRAY=-1; return end; Comment=fgetl(fid); if (Comment(1) ~= '#') %File with no comment CR=sscanf(Comment,'%d %d'); else C_R=fgetl(fid); CR=sscanf(C_R,'%d %d'); end; Colors=fgetl(fid); Col=sscanf(Colors,'%d'); [IMAGE_GRAY, count]=fread(fid,[CR(1), CR(2)], 'char*1'); IMAGE_GRAY=uint8(IMAGE_GRAY'); % imshow(IMAGE_GRAY); fclose(fid); end;