24小时热门版块排行榜    

查看: 3310  |  回复: 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 的主题更新
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 328求调剂,英语六级551,有科研经历 +3 生物工程调剂 2026-03-16 4/200 2026-03-16 20:13 by Wangjingyue
[考研] 机械专硕325,寻找调剂院校 +3 y9999 2026-03-15 5/250 2026-03-16 19:58 by y9999
[文学芳草园] 伙伴们,祝我生日快乐吧 +17 myrtle 2026-03-10 26/1300 2026-03-16 18:32 by 青橙Ln
[考研] 333求调剂 +3 文思客 2026-03-16 7/350 2026-03-16 18:21 by 文思客
[考研] 0703化学调剂,求各位老师收留 +8 秋有木北 2026-03-14 8/400 2026-03-16 15:21 by 哦哦123
[考研] 材料与化工求调剂 +3 为学666 2026-03-16 3/150 2026-03-16 15:09 by 加号+
[考研] 材料与化工专硕调剂 +3 heming3743 2026-03-16 3/150 2026-03-16 15:05 by peike
[基金申请] NSFC申报书里申请人简历中代表性论著还需要在申报书最后的附件里面再上传一遍吗 20+5 NSFC2026我来了 2026-03-10 14/700 2026-03-15 23:53 by 不负韶华的虎
[考研] 机械专硕调剂 +3 笨笨兔子 2026-03-12 3/150 2026-03-15 20:02 by 栗子粥?
[考研] 274求调剂 +4 时间点 2026-03-13 4/200 2026-03-15 15:29 by Rambo13
[考研] 26考研调剂 +3 ying123. 2026-03-10 3/150 2026-03-14 00:18 by JourneyLucky
[考研] 0703,333分求调剂 一志愿郑州大学-物理化学 +3 李魔女斗篷 2026-03-11 3/150 2026-03-13 22:24 by JourneyLucky
[考研] 工科,求调剂 +3 我887 2026-03-11 3/150 2026-03-13 21:39 by JourneyLucky
[考研] 26调剂/材料科学与工程/总分295/求收留 +9 2026调剂侠 2026-03-12 9/450 2026-03-13 20:46 by 18595523086
[考研] 310求调剂 +3 【上上签】 2026-03-11 3/150 2026-03-13 16:16 by JourneyLucky
[考研] 材料专硕350 求调剂 +4 王金科 2026-03-12 4/200 2026-03-13 16:02 by ruiyingmiao
[考研] 290求调剂 +7 ADT 2026-03-12 7/350 2026-03-13 15:17 by JourneyLucky
[考研] 268求调剂 +4 好运连绵不绝 2026-03-12 4/200 2026-03-13 10:45 by hyswxzs
[考研] 296求调剂 +3 大口吃饭 身体健 2026-03-13 3/150 2026-03-13 10:31 by 学员8dgXkO
[考研] 收调剂 +7 调剂的考研学生 2026-03-10 7/350 2026-03-10 17:57 by 麦茶汤圆
信息提示
请填处理意见