Scikit-Image Tutorial:画像はnumpyアレイ

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

このサイトにあるskimage-tutorialsをやってみようと思う。画像処理はpython programmingの基本でもあるので、基礎固めの一環として、画像周辺の知識を深めておいた方がいいようだ。

スポンサーリンク

tutorialの準備

先ず、チュートリアルのgitをcloneする。

!git clone --depth=1 https://github.com/scikit-image/skimage-tutorials
Cloning into 'skimage-tutorials'...
remote: Enumerating objects: 305, done.
remote: Counting objects: 100% (305/305), done.
remote: Compressing objects: 100% (279/279), done.
remote: Total 305 (delta 33), reused 233 (delta 11), pack-reused 0
Receiving objects: 100% (305/305), 70.78 MiB | 16.41 MiB/s, done.
Resolving deltas: 100% (33/33), done.

チュートリアルディレクトリに移動する。

cd skimage-tutorials
/home/workspace/git/skimage-tutorials
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
import numpy as np
from matplotlib import pyplot as plt

plt.rcParams['figure.figsize'] = 16, 8
plt.rcParams["font.size"] = "17"
スポンサーリンク

画像はnumpyアレイ

Images are represented in scikit-image using standard numpy arrays. This allows maximum inter-operability with other libraries in the scientific Python ecosystem, such as matplotlib and scipy.
scikit-imageでは、画像はnumpyアレイを使って表されている。この事が、マットプロットリブやサイパイ(サイピー)などの科学的なPythonエコシステムにおいて、他のライブラリとの最大限の相互運用を可能にしてくれている。

Let’s see how to build a grayscale image as a 2D array:
2次元アレイとしてグレイスケール画像を構築する方法を見ていく。

random_image = np.random.random([500, 500])

plt.imshow(random_image, cmap='gray')
plt.colorbar();

The same holds for “real-world” images:
同じことは実在の画像にもあてはまる。

from skimage import data

coins = data.coins()

print('Type:', type(coins))
print('dtype:', coins.dtype)
print('shape:', coins.shape)

plt.imshow(coins, cmap='gray');
Type: <class 'numpy.ndarray'>
dtype: uint8
shape: (303, 384)

A color image is a 3D array, where the last dimension has size 3 and represents the red, green, and blue channels:
カラー画像は、最後の次元がサイズ3を持つ、赤、緑、青チャンネルを表す三次元アレイ。

cat = data.chelsea()
print("Shape:", cat.shape)
print("Values min/max:", cat.min(), cat.max())

plt.imshow(cat);
Shape: (300, 451, 3)
Values min/max: 0 231

These are just NumPy arrays. E.g., we can make a red square by using standard array slicing and manipulation:

cat[10:110, 10:110, :] = [255, 0, 0]  # [red, green, blue]
plt.imshow(cat);

Images can also include transparent regions by adding a 4th dimension, called an alpha layer.
画像は、アルファレイヤーと呼ばれる第4の次元を加えることで透明領域を含むことも可能だ。

他の形とそれらの意味

Image type Coordinates
2D grayscale (row, column)
2D multichannel (row, column, channel)
3D grayscale (or volumetric) (plane, row, column)
3D multichannel (plane, row, column, channel)
スポンサーリンク

matplotlibで画像表示

from skimage import data

img0 = data.chelsea()
img1 = data.rocket()
import matplotlib.pyplot as plt

f, (ax0, ax1) = plt.subplots(1, 2, figsize=(20, 10))

ax0.imshow(img0)
ax0.set_title('Cat', fontsize=18)
ax0.axis('off')

ax1.imshow(img1)
ax1.set_title('Rocket', fontsize=18)
ax1.set_xlabel(r'Launching position $\alpha=320$')

ax1.vlines([202, 300], 0, img1.shape[0], colors='magenta', linewidth=3, label='Side tower position')
ax1.plot([168, 190, 200], [400, 200, 300], color='white', linestyle='--', label='Side angle')

ax1.legend();

描画についての詳細は以下を参照のこと。

Matplotlib documentation and pyplot API.

スポンサーリンク
スポンサーリンク