Matplotlib による RGB チャネルの可視化

Beginner

This tutorial is from open-source community. Access the source code

はじめに

Matplotlib は、Python における人気のあるデータ可視化ライブラリです。2D および 3D の描画機能を含む、様々な描画ツールを提供します。このチュートリアルでは、Matplotlib の AxesGrid ツールキットの RGBAxes モジュールを使用して RGB チャネルを表示します。

VM のヒント

VM の起動が完了した後、左上隅をクリックしてノートブックタブに切り替え、Jupyter Notebook を使って練習しましょう。

時々、Jupyter Notebook が読み込み終了するまで数秒待つ必要がある場合があります。Jupyter Notebook の制限により、操作の検証を自動化することはできません。

学習中に問題に遭遇した場合は、Labby にお問い合わせください。セッション後にフィードバックを提供してください。すぐに問題を解決いたします。

必要なライブラリをインポートする

このステップでは、必要なライブラリをインポートします。numpymatplotlib.pyplot、およびmpl_toolkits.axes_grid1.axes_rgbです。

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.axes_rgb import RGBAxes, make_rgb_axes

RGB チャネルを取得する関数を定義する

このステップでは、画像の R、G、B チャネルを取得するための関数get_rgb()を定義します。この例では、cbookモジュールのget_sample_data()関数を使ってサンプル画像を取得します。

import matplotlib.cbook as cbook

def get_rgb():
    ## Get a sample image
    Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy")
    Z[Z < 0] = 0.
    Z = Z / Z.max()

    ## Get R, G, and B channels
    R = Z[:13, :13]
    G = Z[2:, 2:]
    B = Z[:13, 2:]

    return R, G, B

RGB キューブを作成する関数を定義する

このステップでは、前のステップで取得した R、G、B チャネルから RGB キューブを作成する関数make_cube()を定義します。この関数は、R、G、B キューブと RGB 画像を返します。

def make_cube(r, g, b):
    ## Get the shape of R
    ny, nx = r.shape

    ## Create the R, G, and B cubes
    R = np.zeros((ny, nx, 3))
    R[:, :, 0] = r
    G = np.zeros_like(R)
    G[:, :, 1] = g
    B = np.zeros_like(R)
    B[:, :, 2] = b

    ## Combine the R, G, and B cubes to create the RGB image
    RGB = R + G + B

    return R, G, B, RGB

RGBAxes プロットを作成する

このステップでは、RGBAxesクラスを使って RGBAxes プロットを作成します。RGBAxesオブジェクトのimshow_rgb()メソッドを使って RGB 画像を表示します。

def demo_rgb1():
    ## Create a figure and a RGBAxes object
    fig = plt.figure()
    ax = RGBAxes(fig, [0.1, 0.1, 0.8, 0.8], pad=0.0)

    ## Get the R, G, and B channels
    r, g, b = get_rgb()

    ## Display the RGB image using the imshow_rgb() method
    ax.imshow_rgb(r, g, b)

個別のチャネルを持つ RGBAxes プロットを作成する

このステップでは、make_rgb_axes()関数を使って個別のチャネルを持つ RGBAxes プロットを作成します。Axesオブジェクトのimshow()メソッドを使って R、G、B チャネルを表示します。

def demo_rgb2():
    ## Create a figure and an Axes object
    fig, ax = plt.subplots()

    ## Create the R, G, and B Axes objects using the make_rgb_axes() function
    ax_r, ax_g, ax_b = make_rgb_axes(ax, pad=0.02)

    ## Get the R, G, and B channels and create the RGB cube
    r, g, b = get_rgb()
    im_r, im_g, im_b, im_rgb = make_cube(r, g, b)

    ## Display the RGB image and the R, G, and B channels
    ax.imshow(im_rgb)
    ax_r.imshow(im_r)
    ax_g.imshow(im_g)
    ax_b.imshow(im_b)

    ## Set the tick parameters and spine colors for all Axes objects
    for ax in fig.axes:
        ax.tick_params(direction='in', color='w')
        ax.spines[:].set_color("w")

プロットを表示する

このステップでは、demo_rgb1()demo_rgb2()関数を呼び出してプロットを作成し、plt.show()関数を使って表示します。

demo_rgb1()
demo_rgb2()

plt.show()

まとめ

このチュートリアルでは、Matplotlib の AxesGrid ツールキットの RGBAxes モジュールを使って RGB チャネルを表示する方法を学びました。以下の手順を扱いました:

  1. 必要なライブラリをインポートする
  2. RGB チャネルを取得する関数を定義する
  3. RGB キューブを作成する関数を定義する
  4. RGBAxes プロットを作成する
  5. 個別のチャネルを持つ RGBAxes プロットを作成する
  6. プロットを表示する。