因为公司使用的人脸识别算法用到了PGM格式的图片作为输入源,查资料备用:
PBM图片格式
可移植像素图格式 (PPM), 可移植灰度图格式 (PGM)和 可移植位图格式 (PBM)是便于 跨平台 的 图像格式 。有时候也被统称为 PNM格式文件格式描述
这三种格式在颜色的表示上有差异。PBM是单色,PGM是灰度图,PPM使用RGB颜色。
每个文件的开头两个字节(ASCII码)作为文件描述子,指出具体格式和编码形式。具体见下表。
文件描述子 | 类型 | 编码 |
---|---|---|
P1 |
位图 | ASCII |
P2 |
灰度图 | ASCII |
P3 |
像素图 | ASCII |
P4 |
位图 | 二进制 |
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 repr