PGM图片格式

来源:互联网 发布:小猪微电商系统源码 编辑:程序博客网 时间:2024/05/17 02:46

因为公司使用的人脸识别算法用到了PGM格式的图片作为输入源,查资料备用:

PBM图片格式

可移植像素图格式(PPM),可移植灰度图格式(PGM)和可移植位图格式(PBM)是便于跨平台图像格式。有时候也被统称为PNM格式

文件格式描述

这三种格式在颜色的表示上有差异。PBM是单色,PGM是灰度图,PPM使用RGB颜色。

每个文件的开头两个字节(ASCII码)作为文件描述子,指出具体格式和编码形式。具体见下表。

文件描述子类型编码P1位图ASCIIP2灰度图ASCIIP3像素图ASCIIP4位图二进制P5灰度图二进制P6像素图二进制

基于ASCII的格式使人可读,并且能够很容易的移植到其他格式。但是二进制格式更有效,不仅因为他节约空间,而且因为他更容易被解析(因为很少有空格)

当使用二进制格式的时候,PBM每像素使用一个比特空间,PGM每个像素使用8个比特空间,PPM每像素使用24比特空间(8比特红色、8比特绿色、8比特蓝色)。

Example:

下面是一个简单的例子

P1
# This is an example bitmap of the letter "J"
6 10
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 1 0
1 0 0 0 1 0
0 1 1 1 0 0
0 0 0 0 0 0
0 0 0 0 0 0

P1表示文件格式。#符号表示一个注释。接下来两个数是宽度和高度。接下来的矩阵是每个像素的值。(在这里单色格式,只有0和1)

P2
6 6
255
0 0 0 150 0 0
0 0 0 150 0 0
0 0 0 150 0 0
0 150 0 150 0 0
0 150 150 150 0 0
0 0 0 0 0 0


P3
4 4
15
0 0 0 0 0 0 0 0 0 15 0 15
0 0 0 0 15 7 0 0 0 0 0 0
0 0 0 0 0 0 0 15 7 0 0 0
15 0 15 0 0 0 0 0 0 0 0 0

16位扩展

P2
6 6
65535
0 0 0 30000 0 0
0 0 0 30000 0 0
0 0 0 30000 0 0
0 0 0 30000 0 0
0 30000 30000 30000 0 0
0 0 0 0 0 0

Plain PGM

There is actually another version of the PGM format that is fairly rare: "plain" PGM format. The format above, which generally considered the normal one, is known as the "raw" PGM format. See pbm for some commentary on how plain and raw formats relate to one another and how to use them.

The difference in the plain format is:

  • There is exactly one image in a file.
  • The magic number is P2 instead of P5.
  • Each pixel in the raster is represented as an ASCII decimal number (of arbitrary size).
  • Each pixel in the raster has white space before and after it. There must be at least one character of white space between any two pixels, but there is no maximum.
  • No line should be longer than 70 characters.

Here is an example of a small image in the plain PGM format.

P2# feep.pgm24 7150  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  00  3  3  3  3  0  0  7  7  7  7  0  0 11 11 11 11  0  0 15 15 15 15  00  3  0  0  0  0  0  7  0  0  0  0  0 11  0  0  0  0  0 15  0  0 15  00  3  3  3  0  0  0  7  7  7  0  0  0 11 11 11  0  0  0 15 15 15 15  00  3  0  0  0  0  0  7  0  0  0  0  0 11  0  0  0  0  0 15  0  0  0  00  3  0  0  0  0  0  7  7  7  7  0  0 11 11 11 11  0  0 15  0  0  0  00  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0

There is a newline character at the end of each of these lines.

Programs that read this format should be as lenient as possible, accepting anything that looks remotely like a PGM.

文件头部分

文件头包括的信息依次是:

1.PGM文件的格式类型(是P2还是P5);

2.图像的宽度;

3.图像的高度;

4.图像灰度值可能的最大值;

文件头的这四部分信息都是以ASCII码形式存储的,所以可以直接在将P2或P5格式的PGM文件在记事本中打开看到文件头的信息.

在P2或P5类型的PGM文件头的4个信息之间用分割符分开,PGM的合法分隔符包括:空格,TAB,回车符,换行符.PGM文件头的信息应该由合法分割符号分开,如上面两幅图所展.文件头的第4个信息,图像灰度值可能的最大值标明了文件数据部分可能出现的像素灰度值的最大值.上面两幅图都是指定的255,所以在数据区的像素数据取值范围在0到255.

数据部分

数据部分记录图像每个像素的灰度值,按照图像从上到下,从左到右的顺序依次存储每个像素的灰度值.对于像素灰度值的表示P2格式和P5格式有所不同.

P5格式

P5格式的文件,每个像素用可以用二进制表示.比如有一幅P5格式图像,灰度值可能的最大值为255,它的第一行第一列像素值为100,那么该图像每个像素使用一个字节表示,第一行第一列为数值为100的二进制一个字节表示.如果这副图灰度值可能的最大值是65535,那么它的第一行第一列为数值为100的二进制两个字节表示(因为表示到65535需要两个字节).每个像素数据之间没有间隔的连续存储,图像一行信息结束后从下一行第一列继续,两行图像数据之间也没有间隔的连续存储,直到将图像的所有信息表示完.因为是以二进制表示,所以数据部分在记事本中打开后看到的将会是乱码.

P2格式

P2格式的文件,每个像素使用字符串来表示,比如一副P2格式图像,灰度值可能的最大值为255,它的第一行第一列像素值为100,那么该图像图像每个像素使用3个ASCII字符表示,第一行第一列数据为ASII表示的"100".不同于P5格式,每个像素数据之间需要用一个空格符分开存储,在图像的每一行数据结束时需要换行.还有一点需要注意,P2格式文件数据部分当数据超过70个字节的时候,会自动换行.

The following data shows a P2 PGM file for lena256.pgm. The datum 158, for example, is three ASCII characters for the respective digits 1, 5 and 8, followed by a space.

1P2
2#P2 PGM Example
3256 256 255
4158 165 158 158 158 158 155 158 155 161 155 150 155 155 ……… <end-of-file>
5</end-of-file>

P5 PGM image file is shown below; where each pixel is stored as a byte that is a binary number from 0 to 255 (text editors show 0 through 127 as ASCII characters and 128 through 255 at whatever code the editor uses for those values).

1P5
2#P5 PGM Example
3256 256 255
4ž¥žžžž›ž›¡›–››ž—–››–¡¡®®«§«Ÿ——†u`^Zffllell`lllllllgllxnunuu{}•| †„}†$•.......<end-of-file>
5</end-of-file>

We see that the P5 type of PGM is packed (without delimiters) so there is a single byte for each pixel. This is also called the raw data format. The size of this file is 65,576 bytes. The P2 type of PGM uses a byte for each numerical symbol (digit) and therefore requires three bytes for each number greater than 99 and also uses a space character after each pixel value. Thus the file is nearly four times as large. This P2 file is 245,724 bytes. However, humans can read the P2 type of PGM file, whereas they can not read the ASCII characters of the packed bytes, which can appear different in different editors (characters 128 to 255).


代码待调试后放上
0 0
原创粉丝点击