Scikit-Image Tutorial:データ型と画像値・画像の入出力

その買うを、もっとハッピーに。|ハピタス

このサイトにあるskimage-tutorialsの画像はnumpyアレイの続きをやる。今回のチュートリアルは、データ型と画像値、イメージI/O(input/output)、画像の入出力となっている。

スポンサーリンク

データ型と画像値

In literature, one finds different conventions for representing image values:
資料においては、画像値を表す異なる表現法が存在する。

  • 0 – 255 where 0 is black, 255 is white
  • 0が黒で、255が白
  • 0 – 1 where 0 is black, 1 is white
  • 0が黒で、1が白

scikit-image supports both conventions–the choice is determined by the data-type of the array.
scikit-imageは、両表現法をサポートし、アレイのデータ型によってどっちの表現法が使われるかが決まる。

E.g., here, I generate two valid images:
例えば、ここでは、2つの有効な画像を生成する。

cd git/skimage-tutorials/lectures
/home/workspace/git/skimage-tutorials/lectures
import numpy as np
import matplotlib.pyplot as plt

linear0 = np.linspace(0, 1, 2500).reshape((50, 50))
linear1 = np.linspace(0, 255, 2500).reshape((50, 50)).astype(np.uint8)

print("Linear0:", linear0.dtype, linear0.min(), linear0.max())
print("Linear1:", linear1.dtype, linear1.min(), linear1.max())

fig, (ax0, ax1) = plt.subplots(1, 2, figsize=(15, 15))
ax0.imshow(linear0, cmap='gray')
ax1.imshow(linear1, cmap='gray');
Linear0: float64 0.0 1.0
Linear1: uint8 0 255

The library is designed in such a way that any data-type is allowed as input, as long as the range is correct (0-1 for floating point images, 0-255 for unsigned bytes, 0-65535 for unsigned 16-bit integers).
sk-imageライブラリは、範囲が正しい(浮動小数点画像は0-1、符号無しバイトは0-255、符号無し16ビット整数は0-65535)限り、全データ型が入力できるようになっている。

You can convert images between different representations by using img_as_float, img_as_ubyte, etc.:
img_as_floatimg_as_ubyte等を使うことで、異なる表現間で画像を変換できる。

from skimage import img_as_float, img_as_ubyte
from skimage import data

image = data.chelsea()

image_ubyte = img_as_ubyte(image)
image_float = img_as_float(image)

print("type, min, max:", image_ubyte.dtype, image_ubyte.min(), image_ubyte.max())
print("type, min, max:", image_float.dtype, image_float.min(), image_float.max())
print()
print("231/255 =", 231/255.)
type, min, max: uint8 0 231
type, min, max: float64 0.0 0.9058823529411765

231/255 = 0.9058823529411765

Your code would then typically look like this:
コードはだいたいこんな感じになる。

def my_function(any_image):
   float_image = img_as_float(any_image)
#Proceed,-knowing-image-is-in-[0,-1]

We recommend using the floating point representation, given that
scikit-image mostly uses that format internally.
scikit-imageが、そのフォーマットを主に内部で使用していることを考慮して、浮動小数点表示を使うことを推奨している。

スポンサーリンク

Image I/O

Mostly, we won’t be using input images from the scikit-image example data sets. Those images are typically stored in JPEG or PNG format. Since scikit-image operates on NumPy arrays, any image reader library that provides arrays will do. Options include imageio, matplotlib, pillow, etc.
ほとんどの場合、scikit-image見本データ・セットから入力画像を使うことはしない。それらの画像は、通常、JPEGかPNG形式で保存されている。scikit-imageは、numpyアレイで動作しているので、アレイを提供している画像読み込みライブラリは何でも使え、オプションには、imageio、matplotlib、pillow等が含まれる。

scikit-image conveniently wraps many of these in the io submodule, and will use whichever of the libraries mentioned above are installed:
scikit-imageは、これら画像の多くをioサブモジュールに扱いやすくラップし、前記のライブラリでインストールされているものを使用する。

from skimage import io

plt.rcParams['figure.figsize'] = 20, 8
plt.rcParams["font.size"] = "17"

image = io.imread('../images/balloon.jpg')

print(type(image))
print(image.dtype)
print(image.shape)
print(image.min(), image.max())

plt.imshow(image);
<class 'numpy.ndarray'>
uint8
(300, 225, 3)
0 255

We also have the ability to load multiple images, or multi-layer TIFF images:
また、複数画像や多層TIFF画像をロードすることも可能だ。

ic = io.ImageCollection('../images/*.png:../images/*.jpg')

print('Type:', type(ic))

ic.files
Type: <class 'skimage.io.collection.ImageCollection'>
['../images/Bells-Beach.jpg',
 '../images/balloon.jpg',
 '../images/bubbles.jpg',
 '../images/chapel_floor.png',
 '../images/chromosomes.jpg',
 '../images/clock_motion.png',
 '../images/color-wheel.jpg',
 '../images/credit_card.jpg',
 '../images/cycle_spin.png',
 '../images/fingers.png',
 '../images/forest.jpg',
 '../images/greenscreen.jpg',
 '../images/hand-coin.jpg',
 '../images/microarray.jpg',
 '../images/mm.jpg',
 '../images/mona_lisa.jpg',
 '../images/mpl_hist.png',
 '../images/round_pill.jpg',
 '../images/snakes.png',
 '../images/spice_1.jpg',
 '../images/spices.jpg',
 '../images/stefan.jpg',
 '../images/superprom_prev.jpg',
 '../images/zebrafish-spinal-cord-color.png',
 '../images/zebrafish-spinal-cord.png']
import os

f, axes = plt.subplots(nrows=7, ncols=len(ic) // 7 + 1, figsize=(20, 20))

# subplots returns the figure and an array of axes
# we use `axes.ravel()` to turn these into a list
axes = axes.ravel()

for ax in axes:
    ax.axis('off')

for i, image in enumerate(ic):
    axes[i].imshow(image, cmap='gray')
    axes[i].set_title(os.path.basename(ic.files[i]))
    #plt.tight_layout()
スポンサーリンク

Aside(余談): enumerate

enumerate gives us each element in a container, along with its position.
enumerateは、コンテナ内の各要素とその位置を提供する。

animals = ['cat', 'dog', 'leopard']
for i, animal in enumerate(animals):
    print('The animal in position {} is {}'.format(i, animal))
The animal in position 0 is cat
The animal in position 1 is dog
The animal in position 2 is leopard
スポンサーリンク
スポンサーリンク