matlab如何读取raw数据
发布网友
发布时间:2022-04-23 06:22
我来回答
共2个回答
热心网友
时间:2023-10-05 02:32
1.RAW结构是纯像素数据文件,里面只有每个像素的值,没有文件头、调色板等数据,所以要想正确显示一个RAW文件图像,必须人工指定它的长、宽和像素深度。
2.每个像素根据格式不同占有不同的字节,如8位256色每个像素占一个字节;24位真彩色每个像素占三个字节。
3.要自己写,注意:
(1)函数要有此RAW文件的长、宽和像素深度三个参数,从而得到BMP文件头,存入strBMP[]的前面;
(2)函数里把读进来的RAW文件数据strRaw[]里的数据进行行反转放入strBMP[]中文件头之后,即把第length-1-i行(从第0行开始记,i从0开始)的数据放到第i行,而每行里的数据不变。这样做是因为BMP文件里的像素数据是从最后一行即length-1开始的。
(3)使用显示BMP文件的函数来显示此strBMP[]里的图像文件。
热心网友
时间:2023-10-05 02:33
% 网上找的,看下有没有用
function [X,map] = rawread(filename,n,m);
% RAWREAD Read a Portable Bitmap file, or a raw file.
% RAWREAD('imagefile.raw', xsize, ysize) reads a "raw" image file
% RAWREAD('imagefile.pgm') reads a "pgm" (portable gray map) image
% [X,map] = RAWREAD('imagefile.raw') returns both the image and a
% color map, so that
% [X,map] = rawread('imagefile.raw',sx,sy);
% or [X,map] = rawread('imagefile.pgm');
% image(X)
% colormap(map)
% will display the result with the proper colors.
%
% NOTE : map is optional and could be replaced ring the display by
% the "colormap('gray')" command
%
% See also IMWRITE, IMREAD, IMAGE, COLORMAP.
dot = max(find(filename == '.'));
suffix = filename(dot+1:dot+3);
if strcmp(suffix,'pgm') | strcmp(suffix,'raw')
disp(sprintf('nopens %s filen',filename));
fp = fopen(filename,'rb','b'); % "Big-endian" byte order.
if (fp<0)
error(['Cannot open ' filename '.']);
end
if strcmp(suffix,'pgm')
% Read and crack the header
head = fread(fp,2,'uchar'); % pgm magic number : P5
if ~strcmp(head,'P5'),
fprintf(1,'n Magic Number : %sn',head);
else
fprintf(1,'n Bad Magic Number : %sn',head);
error('cannot continue this way, good bye cruel world');
end
c = fread(fp,1,'uchar'); %reads the carriage return separating P5 from the creator
precreator = fread(fp,1,'uchar'); % look for a '#' character preceeding a creator signature
if precreator == '#',
c = setstr(20); % any character except carriage return
cr = setstr(10); % defines a carriage return
while c ~= cr,
c = fread(fp,1,'uchar');
creator = [creator,c];
end;
fprintf(1,'n creator : %sn',creator);
else
fprintf('n No creator signaturen');
fseek(fp,-1,'cof'); % return one char before
end;
end
if nargin <2,
if strcmp(suffix,'raw')
% assume image size is 256x256
disp('RAW file without size : assume image size is 256x256');
n = 256;
m = 256;
else % for PGM files
% reads the size and depth
disp(' reads sizes');
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'); %reads the last carriage return
end;
end
% Creates a gray palette and scale it to [0,1].
disp(' create gray palette');
for i=1:256,
map(i,[1:3])=[i/256,i/256,i/256];
end;
% Read the image
disp(' Reads image data ...');
[X,l] = fread(fp,[n,m],'uchar');
if l ~= m*n, l, error('HSI image file is wrong length'), end
% Image elements are colormap indices, so start at 1.
X = X'+1;
fclose(fp);
disp('end');
else
error('Image file name must end in ''raw'' or ''pgm''.')
end