24小时热门版块排行榜    

北京石油化工学院2026年研究生招生接收调剂公告
查看: 3357  |  回复: 11

afgh2587849

木虫 (小有名气)

[交流] 【求助】matlab如何只读取图像的一部分? 已有7人参与

用matlab处理图像的时候遇到了一个问题,图像太大而且数目比较多,但感兴趣的只是图像中的一部分,现在想做的就是比如说对1024*1024的图像只读取200*200的这一小部分,不是先把整幅图像读进来然后再取矩阵的一部分,而是一开始就只读这一小部分图像,请问该如何操作?
回复此楼
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
回帖支持 ( 显示支持度最高的前 50 名 )

afgh2587849

木虫 (小有名气)

引用回帖:
Originally posted by jessican at 2010-11-17 05:00:21:
直接用fread就可以

麻烦您能给个script的例子吗,我也试过fread,没成功  pgm(P5)格式的图片
5楼2010-11-18 04:43:15
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

string121

金虫 (小有名气)


小木虫(金币+0.5):给个红包,谢谢回帖交流
你参考下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
6楼2010-11-18 18:22:50
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
普通回帖

string121

金虫 (小有名气)

★ ★
小木虫(金币+0.5):给个红包,谢谢回帖交流
mze04532(金币+1):热心应助,鼓励~ 2010-11-15 20:58:36
将图像当文件读就可以了,fread。
.bmp等没压缩的图像很简单。.jpg等压缩格式图像就比较麻烦了。
2楼2010-11-15 20:26:25
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

afgh2587849

木虫 (小有名气)

引用回帖:
Originally posted by string121 at 2010-11-15 20:26:25:
将图像当文件读就可以了,fread。
.bmp等没压缩的图像很简单。.jpg等压缩格式图像就比较麻烦了。

谢谢回复,是.pgm格式的,P5格式,请问这个具体该如何去读?
3楼2010-11-16 03:40:24
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

jessican

金虫 (正式写手)

直接用fread就可以
4楼2010-11-17 05:00:21
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

image_pro

新虫 (初入文坛)

呵呵,我只想问问,先把整个读近来,再取一部分有什么不行的?好奇而已。
7楼2010-12-21 16:25:00
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

afgh2587849

木虫 (小有名气)

引用回帖:
Originally posted by image_pro at 2010-12-21 16:25:00:
呵呵,我只想问问,先把整个读近来,再取一部分有什么不行的?好奇而已。

当整幅图像是4000*4000,而你感兴趣的部分只是全图的1/30左右,再把图像的数目乘以300, 原因就很明白了
8楼2010-12-23 23:31:29
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

cctt126

金虫 (正式写手)


小木虫(金币+0.5):给个红包,谢谢回帖交流
引用回帖:
Originally posted by afgh2587849 at 2010-12-23 23:31:29:


当整幅图像是4000*4000,而你感兴趣的部分只是全图的1/30左右,再把图像的数目乘以300, 原因就很明白了

读取一副4000*4000图片,再选择其中1/30的内容到新的矩阵变量中,再删除4000*4000的图片;再读取第二幅4000*4000图片......这样内存应该够用吧.
数学,英语,编程,没一样让我省心。。。
9楼2010-12-26 23:14:16
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

afgh2587849

木虫 (小有名气)

引用回帖:
Originally posted by cctt126 at 2010-12-26 23:14:16:

读取一副4000*4000图片,再选择其中1/30的内容到新的矩阵变量中,再删除4000*4000的图片;再读取第二幅4000*4000图片......这样内存应该够用吧.

我现在的做法跟你的思路差不多,先把需要的内容读到新的矩阵中,然后保存图像,继续这样读、保存....   相当于把原来的图片库按自己的需要重新剪切为小的图片库,以备以后反复使用需要,这样也能够解决问题。
但既然开始的时候想到了直接读取图像一部分,就希望知道有没有办法能做到这一点,但现在还没有什么头绪。
10楼2010-12-27 01:29:20
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 afgh2587849 的主题更新
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 266分,求材料相关专业调剂 +4 哇呼哼呼哼 2026-03-30 5/250 2026-03-30 18:09 by 哇呼哼呼哼
[考研] 一志愿北京化工大学材料与化工(085600)296求调剂 +23 稻妻小编 2026-03-26 23/1150 2026-03-30 17:49 by oooqiao
[考研] 334分 一志愿武理 材料求调剂 +16 李李不服输 2026-03-26 16/800 2026-03-30 17:47 by wangjy2002
[考研] 262求调剂 +4 ZZ..000 2026-03-30 4/200 2026-03-30 15:57 by wangjy2002
[考研] 本科211生物医学工程085409求调剂339分 +3 里子木yy 2026-03-29 3/150 2026-03-30 13:29 by gyzj2026
[考研] 材料与化工272求调剂 +21 阿斯蒂芬2004 2026-03-28 21/1050 2026-03-30 10:52 by 晴空210210
[考研] 296求调剂 +10 彼岸t 2026-03-29 10/500 2026-03-30 10:50 by 探123
[考研] 一志愿郑州大学,080500学硕,总分317分求调剂 +8 举个栗子oi 2026-03-24 9/450 2026-03-29 13:08 by peike
[考研] 调剂考研 +3 王杰一 2026-03-29 3/150 2026-03-29 08:09 by fmesaito
[考研] 生物学学硕,一志愿湖南大学,初试成绩338 +6 YYYYYNNNNN 2026-03-26 7/350 2026-03-28 20:52 by 唐沐儿
[考研] 070300求调剂306分 +4 26要上岸 2026-03-27 4/200 2026-03-28 13:06 by 唐沐儿
[考研] 352分 化工与材料 +5 海纳百川Ly 2026-03-27 5/250 2026-03-28 03:39 by fmesaito
[考研] 330一志愿中国海洋大学 化学工程 085602 有读博意愿 求调剂 +3 wywy.. 2026-03-27 4/200 2026-03-28 03:32 by fmesaito
[考研] 化学调剂 +4 爱吃番茄的旭 2026-03-24 5/250 2026-03-27 17:50 by kiokin
[考研] 0703化学338求调剂! +6 Zuhui0306 2026-03-26 7/350 2026-03-27 10:35 by shangxh
[考研] 351求调剂 +4 麦克阿磊 2026-03-24 4/200 2026-03-27 00:32 by wxiongid
[考研] 336材料求调剂 +7 陈滢莹 2026-03-26 9/450 2026-03-27 00:20 by wxiongid
[考研] 085601求调剂总分293英一数二 +4 钢铁大炮 2026-03-24 4/200 2026-03-26 16:28 by dick_runner
[考研] 各位老师您好:本人初试372分 +5 jj涌77 2026-03-25 6/300 2026-03-25 14:15 by mapenggao
[考研] 一志愿北化315 求调剂 +3 akrrain 2026-03-24 3/150 2026-03-24 19:35 by 了了了了。。
信息提示
请填处理意见