はじめに
skimage.morphologyのstarで、大きさの異なる星形の構造化要素を作成して表示する。
コード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from skimage.morphology import star
# Generate 2D structuring star elements.
struc_2d = {
"star(1)": star(1),
"star(2)": star(2),
"star(3)": star(3),
"star(4)": star(4),
"star(5)": star(5),
"star(6)": star(6)}
# Visualize the elements.
fig = plt.figure(figsize=(12,6))
idx = 1
for title, struc in struc_2d.items():
ax = fig.add_subplot(2, 3, idx)
ax.imshow(struc, cmap="terrain", vmin=-1, vmax=3,zorder=2)
for i in range(struc.shape[0]):
for j in range(struc.shape[1]):
ax.text(j, i, struc[i, j], ha="center", va="center", color="k",zorder=3)
ax.xaxis.set_major_locator(ticker.MultipleLocator(1))
ax.yaxis.set_major_locator(ticker.MultipleLocator(1))
ax.grid()
ax.set(xlim=(-1,struc.shape[0]),ylim=(-1,struc.shape[0]))
ax.set_title(title)
ax.set_axisbelow(True)
idx += 1
fig.tight_layout()
plt.savefig('star.png',dpi=100)
plt.show()
解説
モジュールのインポートなど
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from skimage.morphology import star
バージョン
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#version
import skimage
print(skimage.__version__)
#0.16.2
import matplotlib
print(matplotlib.__version__)
#3.2.0
print(np.__version__)
#1.18.1
構造化要素の生成
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struc_2d = {
"star(1)": star(1),
"star(2)": star(2),
"star(3)": star(3),
"star(4)": star(4),
"star(5)": star(5),
"star(6)": star(6)}
star(5)でサイズ5の正方形とその正方形が45度回転したものが重なった形状となる。
構造化要素の表示
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fig = plt.figure(figsize=(12,6))
idx = 1
for title, struc in struc_2d.items():
ax = fig.add_subplot(2, 3, idx)
ax.imshow(struc, cmap="terrain", vmin=-1, vmax=3,zorder=2)
for i in range(struc.shape[0]):
for j in range(struc.shape[1]):
ax.text(j, i, struc[i, j], ha="center", va="center", color="k",zorder=3)
ax.xaxis.set_major_locator(ticker.MultipleLocator(1))
ax.yaxis.set_major_locator(ticker.MultipleLocator(1))
ax.grid()
ax.set(xlim=(-1,struc.shape[0]),ylim=(-1,struc.shape[0]))
ax.set_title(title)
ax.set_axisbelow(True)
idx += 1
fig.tight_layout()
plt.savefig('star.png',dpi=100)
plt.show()
plt.figure(figsize=(12,6))でfigを作成し、fig.add_subplot(2, 3, idx)で図を順次生成していく。ax.imshow(struc)で構造化要素を表示する。
2重のfor文とax.textにより各ピクセルの中央に各ピクセルの値を表示する。
ax.xaxis.set_major_locator(ticker.MultipleLocator(1))とすることで1ごとに目盛りを表示する。
この状態でax.grid()をすると、1ごとにグリッドが表示されることになる。
ax.set(xlim=(-1,struc.shape[0]),ylim=(-1,struc.shape[0]))で各構造化要素の大きさに応じた軸範囲設定を行う。
ax.set_axisbelow(True)によりグリッドを再下面に表示する。
idx += 1によりidxに1を足して次の図の作成を行う。
参考
Generate footprints (structuring elements) — skimage 0.26.0rc0.dev0 documentation
skimage.morphology — skimage 0.26.0rc0.dev0 documentation
コメント