The End of the World

勉強したことのメモが中心。たまに日々の雑感。

ハイパースペクトルイメージを扱うPythonモジュールspectralの紹介

はじめに

画像処理のコンペで拡張子が.hdrの画像を扱う機会があり、調べても日本語の記事見つからなかったので記録しておきます。

spectralのインストール

 conda install -c conda-forge spectral 

画像の読み込み

# 画像の読み込みとメタデータの表示
In [1]: import spectral
In [2]: img = spectral.open_image('hyper_spectral_image.HDR')
In [3]: print(img)
In [4]: img
Out[4]: 
    Data Source:   './hyper_spectral_image'
    # Rows:            601
    # Samples:        2384
    # Bands:            50
    Interleave:        BSQ
    Quantization:  16 bits
    Data format:    uint16
In [5]: img.__class__
Out[5]: spectral.io.bsqfile.BsqFile

# 画像をNumpy Arrayに変換
In [6]: arr = img.load()
In [7]: arr.shape
Out[7]: (601, 2384, 50)

一旦Numpyに変えてしまえば他の画像と同様に扱えますね。