繁体   English   中英

Numpy和16位PGM

[英]Numpy and 16-bit PGM

用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')但是我感觉这并不是最好的方法。 除此之外,我很难确定如何用size(header)的偏移量将x乘以y(在这种情况下为640x480)读入x的以下16位数据。

编辑:图像已添加

用于读取和显示图像的MATLAB代码是:

I = imread('foo.pgm'); 
imagesc(I);

看起来像这样:

在此处输入图片说明

import re
import numpy

def read_pgm(filename, byteorder='>'):
    """Return image data from a raw PGM file as numpy array.

    Format specification: http://netpbm.sourceforge.net/doc/pgm.html

    """
    with open(filename, 'rb') as f:
        buffer = f.read()
    try:
        header, width, height, maxval = re.search(
            b"(^P5\s(?:\s*#.*[\r\n])*"
            b"(\d+)\s(?:\s*#.*[\r\n])*"
            b"(\d+)\s(?:\s*#.*[\r\n])*"
            b"(\d+)\s(?:\s*#.*[\r\n]\s)*)", buffer).groups()
    except AttributeError:
        raise ValueError("Not a raw PGM file: '%s'" % filename)
    return numpy.frombuffer(buffer,
                            dtype='u1' if int(maxval) < 256 else byteorder+'u2',
                            count=int(width)*int(height),
                            offset=len(header)
                            ).reshape((int(height), int(width)))


if __name__ == "__main__":
    from matplotlib import pyplot
    image = read_pgm("foo.pgm", byteorder='<')
    pyplot.imshow(image, pyplot.cm.gray)
    pyplot.show()

我对PGM格式不是很熟悉,但是通常来说,您只需要使用numpy.fromfile fromfile将从您传递给它的文件指针所处的任何位置开始,因此您可以简单地查找(或读取)到标头的末尾,然后使用fromfile读取其余部分。

您需要使用infile.readline()而不是next(infile)

import numpy as np

with open('foo.pgm', 'r') as infile:
    header = infile.readline()
    width, height, maxval = [int(item) for item in header.split()[1:]]
    image = np.fromfile(infile, dtype=np.uint16).reshape((height, width))

附带说明,您在注释中指向的“ foo.pgm”文件似乎在标题中指定了错误的行数。

如果您要读取很多可能存在该问题的文件,则可以使用零填充数组或截断数组,如下所示。

 
 
 
 
  
  
  import numpy as np with open('foo.pgm', 'r') as infile: header = next(infile) width, height, maxval = [int(item) for item in header.split()[1:]] image = np.fromfile(infile, dtype=np.uint16) if image.size < width * height: pad = np.zeros(width * height - image.size, dtype=np.uint16) image = np.hstack([image, pad]) if image.size > width * height: image = image[:width * height] image = image.reshape((height, width))
 
 
  

这里我知道标题信息可以用空格,回车符或其他分隔。 如果您之间用空格隔开(否则请通知我),您可以执行以下操作:

with open('img.pgm') as f:
    lines = f.readlines()
    data = np.array([line.split() for line in lines[1:]], dtype=np.int16).T

您的数据现在是int16格式的数组!

假设您仍然对标题信息感兴趣,可以执行以下操作:

class Header(object):
    def __init__(self, type, width, height, maxval):
        self.type = type
        self.width = int(width)
        self.height = int(height)
        self.maxval = int(maxval)

h = Header(*lines[0].split()[:4])

这样就可以对照读取的行检查图像数据:

assert (h.width, h.height) == data.shape    
assert h.maxval >= data.max()

编辑 :图像数据为二进制 ,文件必须以“ rb”打开,并在标题信息之后读取:

import numpy as np

def as_array(filepath):
    f = open(filepath, 'r')
    w, h = size = tuple(int(v) for v in next(f).split()[1:3])
    data_size = w * h * 2

    f.seek(0, 2)
    filesize = f.tell()
    f.close()
    i_header_end = filesize - (data_size)

    f = open(filepath, 'rb')
    f.seek(i_header_end)
    buffer = f.read()
    f.close()

    # convert binary data to an array of the right shape
    data = np.frombuffer(buffer, dtype=np.uint16).reshape((w, h))

    return data

a = as_array('foo.pgm')

实际上,标头后的“字符串”是文件中的二进制文件。 我在下面解决了这一问题(找到了以下内容: ndarray: [2047 2047 2047 ..., 540 539 539] ),但是还有另一个问题:文件不够长; 仅计算289872个数字,而不是640 * 480 ...

我为自己的上课感到自豪,对此我感到非常抱歉。

import numpy as np
import Image

class PGM(object):
    def __init__(self, filepath):

        with open(filepath) as f:

            # suppose all header info in first line:
            info = f.readline().split()
            self.type = info[0]
            self.width, self.height, self.maxval = [int(v) for v in info[1:]]
            size = self.width * self.height

            lines = f.readlines()
            dt = [np.int8, np.int16][self.maxval > 255]
            try:
                # this will work if lines are integers separated by e.g. spaces
                self.data = np.array([l.split() for l in lines], dtype=dt).T
            except ValueError:
                # data is binary
                data = np.fromstring(lines[0], dtype=dt)
                if data.size < size:
                    # this is the case for the 'db.tt/phaR587 (foo.pgm)'
                    #raise ValueError('data binary string probably uncomplete')
                    data = np.hstack((data, np.zeros(size-data.size)))
                self.data = data[:size].reshape((self.width, self.height))

            assert (self.width, self.height) == self.data.shape
            assert self.maxval >= self.data.max()

        self._img = None

    def get_img(self):
        if self._img is None:
            # only executed once
            size = (self.width, self.height)
            mode = 'L'
            data = self.data
            self.img = Image.frombuffer(mode, size, data)

        return self.img

    Image = property(get_img)

mypgm = PGM('foo.pgm')

mypgm.Image

编辑:乔·肯顿(Joe Kington)的好主意,用零填充图像!

感谢@ joe-kington的回答,帮助您解决了这一问题。 解决方案如下。

没有硬编码已知的标头长度(在这种情况下为17个字节),而是根据标头确定它,需要做一些额外的工作。 PGM标准表示,标头通常以换行符结尾,但可以以任何空格结尾。 我认为这段代码将在使用非换行空白作为报头末尾分隔符的PGM上中断。 在这种情况下,标头大小取决于保存宽度,高度和最大大小的变量的大小,再加上两个字节的“ P5”,再加上4个字节的空白。

其他可能中断的情况是宽度或高度大于int(非常大的图像)。 或者,如果PGM是8位而不是16位(可以从maxval以及可能的宽度,高度和文件大小确定)。

#!/usr/bin/python
import numpy as np
import matplotlib.pyplot as plt

file='foo.pgm'
infile = open(file,'r')
header = next(infile)
width, height, maxval = [int(item) for item in header.split()[1:]]
infile.seek(len(header))
image = np.fromfile(infile, dtype=np.uint16).reshape((height, width))
print width, height, maxval
plt.figimage(image)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM