天天看點

matlab 讀取 raw,matlab如何讀取raw資料

%網上找的,看下有沒有用function[X,map]=rawread(filename,n,m);%RAWREADReadaPortableBitmapfile,orarawfile。%RAWREAD('imagefile。raw',xsize,ysize)readsa"raw"imagefile%RAWREAD('imagefile。

pgm')readsa"pgm"(portablegraymap)image%[X,map]=RAWREAD('imagefile。raw')returnsboththeimageanda%colormap,sothat%[X,map]=rawread('imagefile。

raw',sx,sy);%or[X,map]=rawread('imagefile。pgm');%image(X)%colormap(map)%willdisplaytheresultwiththepropercolors。%%NOTE:mapisoptionalandcouldbereplacedduringthedisplayby%the"colormap('gray')"command%%SeealsoIMWRITE,IMREAD,IMAGE,COLORMAP。

dot=max(find(filename=='。'));suffix=filename(dot+1:dot+3);ifstrcmp(suffix,'pgm')|strcmp(suffix,'raw')disp(sprintf('nopens%sfilen',filename));fp=fopen(filename,'rb','b');%"Big-endian"byteorder。

if(fp<0)error(['Cannotopen'filename'。']);endifstrcmp(suffix,'pgm')%Readandcracktheheaderhead=fread(fp,2,'uchar');%pgmmagicnumber:P5if~strcmp(head,'P5'),fprintf(1,'nMagicNumber:%sn',head);elsefprintf(1,'nBadMagicNumber:%sn',head);error('cannotcontinuethisway,goodbyecruelworld');endc=fread(fp,1,'uchar');%readsthecarriagereturnseparatingP5fromthecreatorprecreator=fread(fp,1,'uchar');%lookfora'#'characterpreceedingacreatorsignatureifprecreator=='#',c=setstr(20);%anycharacterexceptcarriagereturncr=setstr(10);%definesacarriagereturnwhilec~=cr,c=fread(fp,1,'uchar');creator=[creator,c];end;fprintf(1,'ncreator:%sn',creator);elsefprintf('nNocreatorsignaturen');fseek(fp,-1,'cof');%returnonecharbeforeend;endifnargin<2,ifstrcmp(suffix,'raw')%assumeimagesizeis256x256disp('RAWfilewithoutsize:assumeimagesizeis256x256');n=256;m=256;else%forPGMfiles%readsthesizeanddepthdisp('readssizes');n=fscanf(fp,'%d',1);tn=num2str(n);disp(['xsize='tn]);m=fscanf(fp,'%d',1);tm=num2str(m);disp(['ysize='tm]);p=fscanf(fp,'%d',1);tp=num2str(p);disp(['depth='tp]);c=fread(fp,1,'uchar');%readsthelastcarriagereturnend;end%Createsagraypaletteandscaleitto[0,1]。

disp('creategraypalette');fori=1:256,map(i,[1:3])=[i/256,i/256,i/256];end;%Readtheimagedisp('Readsimagedata。。。');[X,l]=fread(fp,[n,m],'uchar');ifl~=m*n,l,error('HSIimagefileiswronglength'),end%Imageelementsarecolormapindices,sostartat1。

X=X'+1;fclose(fp);disp('end');elseerror('Imagefilenamemustendin''raw''or''pgm''。')end。

全部