24小时热门版块排行榜    

北京石油化工学院2026年研究生招生接收调剂公告
查看: 3354  |  回复: 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 的主题更新
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 327求调剂 +5 汲亦昊 2026-03-29 5/250 2026-03-29 12:38 by 无际的草原
[考研] 考研调剂 +4 小蜡新笔 2026-03-29 4/200 2026-03-29 12:03 by 无际的草原
[考研] 318一志愿吉林大学生物与医药 求调剂 +4 笃行致远. 2026-03-28 4/200 2026-03-29 11:55 by longlotian
[考研] 348求调剂 +5 小懒虫不懒了 2026-03-28 5/250 2026-03-29 10:34 by 唐沐儿
[考研] 279求调剂 +4 蝶舞轻绕 2026-03-29 4/200 2026-03-29 09:45 by laoshidan
[考研] 305求调剂 +8 RuiFairyrui 2026-03-28 8/400 2026-03-29 08:22 by fmesaito
[考研] 085600 材料与化工 329分求调剂 +11 Mr. Z 2026-03-25 11/550 2026-03-29 08:14 by 无际的草原
[考研] 309求调剂 +6 谁不是少年 2026-03-29 6/300 2026-03-29 08:11 by fmesaito
[考研] 求调剂 +3 QiMing7 2026-03-25 4/200 2026-03-28 14:30 by QiMing7
[考研] 一志愿中南大学化学0703总分337求调剂 +5 niko- 2026-03-27 5/250 2026-03-28 14:25 by 唐沐儿
[考研] 【求调剂】085601材料工程专硕 | 总分272 | +6 脚滑的守法公民 2026-03-27 6/300 2026-03-28 11:02 by gjlllb
[考研] 085405 考的11408求各位老师带走 +3 Qiu学ing 2026-03-28 3/150 2026-03-28 09:19 by 乐呵呵的追梦人
[考研] 330一志愿中国海洋大学 化学工程 085602 有读博意愿 求调剂 +3 wywy.. 2026-03-27 4/200 2026-03-28 03:32 by fmesaito
[考研] 351求调剂 +4 麦克阿磊 2026-03-24 4/200 2026-03-27 00:32 by wxiongid
[考研] 调剂求收留 +7 果然有我 2026-03-26 7/350 2026-03-27 00:26 by wxiongid
[考研] 调剂 +4 柚柚yoyo 2026-03-26 4/200 2026-03-26 20:43 by fmesaito
[考研] 环境专硕324分求调剂推荐 +5 轩小宁—— 2026-03-26 5/250 2026-03-26 12:05 by i_cooler
[考研] 290分调剂求助 +3 吉祥止止陈 2026-03-25 3/150 2026-03-25 19:58 by barlinike
[考研] 网络空间安全0839招调剂 +4 w320357296 2026-03-25 6/300 2026-03-25 17:59 by 255671
[考研] 086003食品工程求调剂 +6 淼淼111 2026-03-24 6/300 2026-03-25 10:29 by 3Strings
信息提示
请填处理意见