How I can read image with PPM format without imread and design PPM reader ?

88 ビュー (過去 30 日間)
elma ozge
elma ozge 2021 年 11 月 25 日
コメント済み: Walter Roberson 2021 年 11 月 26 日
I want to read test.ppm image without imread and design PPM reader and open it with fopen and extract data from it after that show it with imshow.
Can you help me?
  7 件のコメント
elma ozge
elma ozge 2021 年 11 月 26 日
I can't understand you.
Walter Roberson
Walter Roberson 2021 年 11 月 26 日
What is the reason that you cannot use imread() ?

サインインしてコメントする。

採用された回答

DGM
DGM 2021 年 11 月 26 日
編集済み: DGM 2021 年 11 月 26 日
Again, the documentation
As an example for this one image, consider the following.
% open the file and read the whole thing
fid = fopen('Barbara.pgm', 'r');
B = fread(fid,inf,'uint8=>char')';
fclose(fid);
% find header delimiters
ws = find(isspace(B),4,'first')
% get file parts (assuming no comments)
magicnum = B(1:ws(1)-1) % this is expected to be P5
% if P2, P3, or P6, do whatever is needed
imgeometry = [str2double(B(ws(1)+1:ws(2)-1)) str2double(B(ws(2)+1:ws(3)-1))]
maxgval = str2double(B(ws(3)+1:ws(4)-1)) % expected to be 255
imgdata = permute(reshape(double(B(ws(4)+1:end)),imgeometry),[2 1 3]);
% normalize the image for convenience
imgdata = imgdata/maxgval;
imshow(imgdata)
Note that this code is not robust and does not accomodate the existence of inline comments in the file. You'll have to deal with that.
This code also assumes that the max gray value stays below 256. If maxgval>255, then you'll have to do some reshaping and simple math because each pixel will be represented by byte pairs instead of single bytes.
If your project needs to read both PGM and PPM files of both types, then you'll have to conditionally handle those cases based on the magic number.
If you want an integer-class numeric array as output instead of a normalized floating-point array, you'll have to conditionally look at the max gray value and cast the image data accordingly.
  4 件のコメント
DGM
DGM 2021 年 11 月 26 日
編集済み: DGM 2021 年 11 月 26 日
Like I said; that example is only made to handle the specific subset of options presented by that particular image. It assumes that the image is a P5 (regular PGM) image. For other types of images in the same family, you'll have to write some conditional statement to check the magic number and deal with the data accordingly. For a P6 PPM, each pixel is represented by a sequence of either three or six bytes (as opposed to only one or two bytes in a P5 PGM file).
When the file is read, the result stored in B is a single long character vector. The following lines simply index into the vector B and extract the relevant subvectors.
% extract image width and height, convert from char to numbers
imgeometry = [str2double(B(ws(1)+1:ws(2)-1)) str2double(B(ws(2)+1:ws(3)-1))]
% extract maximum pixel value, convert from char to number
maxgval = str2double(B(ws(3)+1:ws(4)-1)) % expected to be 255
% extract image data as a vector, reshape into a 2D array according to the specified geometry, transpose
imgdata = permute(reshape(double(B(ws(4)+1:end)),imgeometry),[2 1 3]);
For a P6 PPM with maxgval<256, everything is similar. The only difference is in the array reshaping.
% open the file and read the whole thing
fid = fopen('Brain.ppm', 'r');
B = fread(fid,inf,'uint8=>char')';
fclose(fid);
% find header delimiters
ws = find(isspace(B),4,'first')
% get file parts (assuming no comments)
magicnum = B(1:ws(1)-1) % this is expected to be P6
% if P2, P3, or P5, do whatever is needed
imgeometry = [str2double(B(ws(1)+1:ws(2)-1)) str2double(B(ws(2)+1:ws(3)-1))]
maxgval = str2double(B(ws(3)+1:ws(4)-1)) % assumed to be <256
imgdata = reshape(double(B(ws(4)+1:end)),3,[]); % reshape into 3x(M*N) list of color tuples
imgdata = reshape(permute(imgdata,[2 3 1]),[imgeometry 3]); % reshape into MxNx3 array
imgdata = permute(imgdata,[2 1 3]); % transpose
% normalize the image for convenience
imgdata = imgdata/maxgval;
imshow(imgdata)
Again, this is still not robust and doesn't cover everything.
elma ozge
elma ozge 2021 年 11 月 26 日
Thanks @DGM.

サインインしてコメントする。

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2021 年 11 月 25 日
fopen the file and read it as a series of uint8. fclose(). Now use loadlibrary() to make a call to the libnetpbm library to decode the uint8 into an image.
By the way, is it a P3 or P6?
  2 件のコメント
elma ozge
elma ozge 2021 年 11 月 26 日
I don't know . How i can understand that?
I attached my file. How i can use loadlibrary() with this file?
Walter Roberson
Walter Roberson 2021 年 11 月 26 日
PPM files do not all have exactly the same layout. The very first item inside a PPM file gives information about which of the formats the rest of the file is in. Two of the most common formats are known by the codes P3 and P6.
If you need to be able to handle all possible PPM files, then you have to do notably more work than if you only have to handle one particular kind of PPM file.

サインインしてコメントする。


yanqi liu
yanqi liu 2021 年 11 月 26 日
yes,sir,may be upload the ppm file to do some analysis
  1 件のコメント
elma ozge
elma ozge 2021 年 11 月 26 日
編集済み: elma ozge 2021 年 11 月 26 日
I attached my ppm file.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by