使用 numpy 在 Python 中读取 16 位 PGM 图像的有效且清晰的方法是什么?
由于 PIL 错误,我无法使用 PIL 加载 16 位 PGM 图像。我可以使用以下代码阅读标题:
dt = np.dtype([('type', 'a2'),
('space_0', 'a1', ),
('x', 'a3', ),
('space_1', 'a1', ),
('y', 'a3', ),
('space_2', 'a1', ),
('maxval', 'a5')])
header = np.fromfile( 'img.pgm', dtype=dt )
print header
这将打印正确的数据:('P5', ' ', '640', ' ', '480', ' ', '65535')
但我感觉这不是最好的方法。除此之外,我很难弄清楚如何通过 16 位以size(header)
.
编辑:添加图像
读取和显示图像的 MATLAB 代码是:
I = imread('foo.pgm');
imagesc(I);
看起来像这样: