17

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ビットでx x y(この場合は640x480)の次のデータを読み取る方法を理解する方法に問題がありsize(header)ます。

編集:画像を追加

画像を読み取って表示するMATLABコードは次のとおりです。

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

そしてこのように見えます:

ここに画像の説明を入力してください

4

5 に答える 5

24
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()
于 2011-09-10T06:10:48.087 に答える
4

私は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))

于 2011-09-10T03:02:43.627 に答える
1

実際、ヘッダーの後の「文字列」はファイル内のバイナリです。私は以下でそれを解決しました(以下を見つけました:)ndarray: [2047 2047 2047 ..., 540 539 539]が、別の問題があります:ファイルが十分に長くありません。640*480の代わりに289872の数だけを数えます...

クラスを作って大げさになってすみません...

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

編集:画像をゼロで埋めるためのジョーキントンからの素晴らしいアイデア!

于 2011-09-10T03:03:32.543 に答える
1

ここから、ヘッダー情報はスペース、キャリッジリターンなどで区切ることができることを理解しています。スペースで区切られている場合(そうでない場合は通知してください)、次のことができます。

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')
于 2011-09-10T01:02:34.610 に答える
0

これを理解するのを手伝ってくれた@joe-kingtonによる答えに感謝します。解決策は次のとおりです。

既知のヘッダー長(この場合は17バイト)をハードコーディングせずに、ヘッダーから決定するために、少し余分な作業があります。PGM標準では、ヘッダーは通常改行で終わるとされていますが、空白で終わることもあります。このコードは、ヘッダーの終わりの区切り文字に改行以外の空白を使用するPGMで機能しないと思います。この場合のヘッダーサイズは、width、height、およびmaxsizeを保持する変数のサイズに加えて、「P5」の場合は2バイト、さらに4バイトの空白によって決定されます。

これが機能しない可能性がある他のケースは、幅または高さがint(非常に大きな画像)よりも大きい場合です。または、PGMが16ビットではなく8ビットの場合(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)
于 2011-09-10T05:09:36.960 に答える