画像ピラミッドの構築#

pyramid_gaussian関数は、画像を受け取り、一定の縮尺係数で縮小された連続する画像を生成します。画像ピラミッドは、ノイズ除去、テクスチャ識別、スケール不変検出などのアルゴリズムを実装するために頻繁に使用されます。

import numpy as np
import matplotlib.pyplot as plt

from skimage import data
from skimage.transform import pyramid_gaussian


image = data.astronaut()
rows, cols, dim = image.shape
pyramid = tuple(pyramid_gaussian(image, downscale=2, channel_axis=-1))

可視化のための合成画像の生成#

可視化のために、ソース画像と同じ行数、cols + pyramid[1].shape[1]列の合成画像を生成します。その後、すべてのダウンサンプルされた画像を元の画像の右側に積み重ねるためのスペースが確保されます。

注記:ピラミッド内のすべてのダウンサンプルされた画像の行数の合計は、image.shape[0]が2のべき乗でない場合、元の画像サイズを超える場合があります。必要に応じて、これを考慮するために、合成画像の行数をわずかに拡張します。元の行数を超える拡張は、downscale < 2の場合にも必要になります。

# determine the total number of rows and columns for the composite
composite_rows = max(rows, sum(p.shape[0] for p in pyramid[1:]))
composite_cols = cols + pyramid[1].shape[1]
composite_image = np.zeros((composite_rows, composite_cols, 3), dtype=np.double)

# store the original to the left
composite_image[:rows, :cols, :] = pyramid[0]

# stack all downsampled images in a column to the right of the original
i_row = 0
for p in pyramid[1:]:
    n_rows, n_cols = p.shape[:2]
    composite_image[i_row : i_row + n_rows, cols : cols + n_cols] = p
    i_row += n_rows

fig, ax = plt.subplots()
ax.imshow(composite_image)
plt.show()
plot pyramid

スクリプトの総実行時間:(0分0.471秒)

Sphinx-Galleryによって生成されたギャラリー