• 大小: 2.6MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-11-07
  • 语言: Matlab
  • 标签: 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  程序\test_imread_pxm.m
     文件        1500  2016-03-04 23:18  程序\test_imwrite_pxm.m
     文件       45668  2016-03-04 23:21  程序\tmp.jpg

评论

共有 条评论