37
40

More than 5 years have passed since last update.

[Python]scikit-imageによる画像処理

Last updated at Posted at 2016-02-09

画像ファイルの読み込み

In [5]: from skimage import io

In [6]: I = io.imread('lena512color.tiff')

In [7]: print I.shape
(512, 512, 3)

画像ファイルの書き込み

In [12]: io.imsave('output.bmp', I)

複数の画像ファイルの書き込み

tiffフォーマットでは下記のように複数の画像ファイルを一つのファイルとして保存することができる。

In [13]: I1 = io.imread('Lenna.bmp')

In [14]: I2 = io.imread('Mandrill.bmp')

In [15]: I3 = io.imread('Parrots.bmp')

In [16]: io.imsave('output.tiff', [I1, I2, I3])

In [17]: I4 = io.imread('output.tif')

In [18]: I4.shape
Out[18]: (3, 256, 256, 3)

In [19]: io.imshow(I4[0])

In [20]: io.imshow(I4[1])

In [21]: io.imshow(I4[2])

skimage_03.png
skimage_04.png
skimage_05.png

画像の表示

In [8]: io.imshow(I)

skimage_01.png

色変換

RGB → Gray

In [9]: from skimage.color import rgb2gray

In [10]: G = rgb2gray(I)

In [11]: io.imshow(G)

skimage_02.png

その他の色変換はここを参照

型と値の変更

型の変換と値の変換を同時に行う。
値の変換はimg_as_floatの場合、255~0の値を1.0~0.0の値に変換する。
ただ型の変換をしたい場合は、astypeを使う。

In [50]: from skimage import img_as_float, img_as_int, img_as_ubyte, img_as_uint

In [51]: I_float = img_as_float(I)

In [52]: print I_float.dtype, I_float.max(), I_float.min()
float64 1.0 0.0117647058824

In [53]: I_int   = img_as_int(I)

In [54]: print I_int.dtype, I_int.max(), I_int.min()
int16 32767 385

In [55]: I_ubyte = img_as_ubyte(I)

In [56]: print I_ubyte.dtype, I_ubyte.max(), I_ubyte.min()
uint8 255 3

In [57]: I_uint  = img_as_uint(I)

In [58]: print I_uint.dtype, I_uint.max(), I_uint.min()
uint16 65535 771

画像のリサイズ

scikit-imageにもtransform.resizeがあるが、変換方式が指定できないので、scipyのimresizeを使用する。

In [79]: from skimage import io

In [80]: import numpy as np

In [81]: from scipy.misc import imresize

In [82]: I = io.imread('Lenna.bmp')

In [83]: IN = imresize(I,(I.shape[0]*2, I.shape[1]*2), interp='nearest')

In [84]: IB = imresize(I,(I.shape[0]*2, I.shape[1]*2), interp='bilinear')

In [85]: IC = imresize(I,(I.shape[0]*2, I.shape[1]*2), interp='bicubic')

In [86]: io.imshow(np.hstack((IN[200:264,200:264], IB[200:264,200:264], IC[200:264,200:264])))

skimage_06.png

37
40
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
37
40