matlab如何截取图像的中间部分_【求助】matlab如何只读取图像的一部分? - 信息科学 - 小木虫 - 学术 科研 互动社区...

这篇博客介绍了如何使用MATLAB的rawread函数来读取和显示PGM或RAW格式的图像,并特别强调了如何截取图像的中间部分。通过解析文件头信息,确定图像尺寸,创建灰度映射表,最后读取并显示图像数据。
摘要由CSDN通过智能技术生成

你参考下rawread函数。

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 during 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')

% 要求是pgm格式或者raw格式

disp(sprintf('\nopens %s file\n',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 : %s\n',head); % 默认打印到窗口

else

fprintf(1,'\n Bad Magic Number : %s\n',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 : %s\n',creator);

else

fprintf('\n No creator signature\n');

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

% pgm格式开头包括维数

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,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值