pbm/pgm/ppm图片的读写Matlab


pbm/pgm/ppm图片的读写函数和测试程序(Matlab)
资源截图
代码片段和文件信息
function [ IMG ] = imread_pxm( filename )
%  filename : the name of .ppm/.pgm/.pbm file (in ASCII/Binary mode)
%  This function read .ppm/.pgm/.pbm file and store the image data into variable IMG

    fid = fopen(filename ‘rb‘);    % open .ppm file in binary mode
    p = fread(fid 1 ‘*char‘);     % read the char ‘p‘
    pinfo = fgetl(fid);             % read the head information of the image
    info = str2num(pinfo);          % convert the info of image into number
    [ptype m n] = deal(info(1)info(2)info(3));
                                    % ptype : the type of the image (p1..p6)
                                    % n m : the display resolution
    if (ptype ~= 1)&&(ptype ~= 4)    % range : the range of pixels‘ value
        range = info(4);
    else range = 1;
    end
    
    if info(1) <= 3                 % the image is stored in ASCII mode
        if ptype == 3               % .ppm(RGB) & ASCII 
            for i = 1:n
                for j = 1:m
                    for k = 1:3
                        num = 0;
                        ch = fread(fid 1 ‘uint8‘);
                        while (~feof(fid))&&(ch ~= 32)&&(ch ~= 10)      % read the value of image in ASCII mode
                            num = num * 10 + ch - 48;
                            ch = fread(fid 1 ‘uint8‘);
                        end
                        IMG(i j k) = uint8(num);     % update the value of return var
                    end
                end
            end
        else                        % .pbm/.pgm & ASCII
            for i = 1:n
                for j = 1:m
                    num = 0;
                    ch = fread(fid 1 ‘uint8‘);
                    while (~feof(fid))&&(ch ~= 32)&&(ch ~= 10)      % read the value of image in ASCII mode
                        num = num * 10 + ch - 48;
                        ch = fread(fid 1 ‘uint8‘);
                    end
                    if ptype == 2                       % process .pgm file
                        IMG(i j) =  uint8(num);        % update the value of return var
                    elseif ptype == 1
                        IMG(ij) = logical(1 - num);    % process .pbm file
                    end
                end
            end
        end
    else                            % the image is stored in binary mode
        if ptype == 6               % .ppm(RGB) & binary
            for i = 1:n
                for j = 1:m
                    for k = 1:3
                        ch = fread(fid 1 ‘uint8‘);
                        IMG(i j k) = uint8(ch);   % update the value of return var
                    end
                end
            end
        elseif ptype == 5                        % .pgm & binary
            for i = 1:n
                for j = 1:m
                    ch = fread(fid 1 ‘uint8‘);
                    IMG(i j) =  uint8(ch);      % update the value of return var
     

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2016-03-04 23:21  程序
     文件       74948  2016-03-02 16:18  程序greens.jpg
     文件       18911  2016-03-03 10:27  程序greens.pbm
     文件      150015  2016-03-03 10:27  程序greens.pgm
     文件      450015  2016-03-02 23:54  程序greens.ppm
     文件      300010  2016-03-03 10:27  程序greens1.pbm
     文件      470765  2016-03-03 10:27  程序greens1.pgm
     文件     1392328  2016-03-02 23:53  程序greens1.ppm
     文件        4495  2016-03-04 00:04  程序imread_pxm.m
     文件        4327  2016-03-04 23:07  程序imwrite_pxm.m
     文件         244  2016-03-02 23:54  程序jpg2ppm.m
     文件      300011  2016-03-04 23:18  程序p1.pbm
     文件      150015  2016-03-04 23:18  程序p1.pgm
     文件      470766  2016-03-04 23:18  程序p2.pgm
     文件     1392329  2016-03-04 23:18  程序p3.ppm
     文件       18911  2016-03-04 23:18  程序p4.pbm
     文件      150015  2016-03-04 23:18  程序p5.pgm
     文件      450015  2016-03-04 23:18  程序p6.ppm
     文件         903  2016-03-04 23:25  程序Readme.txt
     文件        1442  2016-03-04 00:11  程序 est_imread_pxm.m
     文件        1500  2016-03-04 23:18  程序 est_imwrite_pxm.m
     文件       45668  2016-03-04 23:21  程序 mp.jpg

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

发表评论

评论列表(条)