はじめに
skimage.future graphのshow_ragにより、領域隣接グラフを表示する例について説明する。
コード
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
from skimage import data, segmentation | |
from skimage.future import graph | |
from matplotlib import pyplot as plt | |
from mpl_toolkits.axes_grid1 import ImageGrid | |
img = plt.imread('zou.jpg') | |
labels = segmentation.slic(img, compactness=30, n_segments=400) | |
g = graph.rag_mean_color(img, labels) | |
fig = plt.figure(figsize=(10, 5)) | |
grid = ImageGrid(fig, 111, | |
nrows_ncols=(1, 2), | |
axes_pad=0.3, | |
share_all=True, | |
cbar_location="bottom", | |
cbar_mode="each", | |
cbar_size="8%", | |
cbar_pad="2%", | |
) | |
grid[0].set_title('RAG drawn with default settings') | |
lc = graph.show_rag(labels, g, img, ax=grid[0]) | |
grid.cbar_axes[0].colorbar(lc) | |
grid[1].set_title('RAG drawn with grayscale image and viridis colormap') | |
lc2 = graph.show_rag(labels, g, img, | |
img_cmap='gray', edge_cmap='viridis', ax=grid[1]) | |
grid.cbar_axes[1].colorbar(lc2) | |
#for cax in grid.cbar_axes: | |
# cax.toggle_label(False) | |
for gr in grid: | |
gr.axis('off') | |
plt.savefig('plot_rag_draw.jpg',dpi=100) |

解説
モジュールのインポート
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
from skimage import data, segmentation
from skimage.future import graph
from matplotlib import pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
画像データの読み込み
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
img = plt.imread('zou.jpg')
コリファンタ属の象牙丸を用いる。

k平均法によるセグメント化
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
labels = segmentation.slic(img, compactness=30, n_segments=400)
segmentation.slic()により、k平均法によるセグメント化を行う。
隣接境界グラフの生成
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
g = graph.rag_mean_color(img, labels)
rag_mean_color()で画像とそのセグメント化された情報から、領域隣接グラフ(RAG)を構築する。
領域隣接グラフを表示
Imagegridの設定
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=(10, 5))
grid = ImageGrid(fig, 111,
nrows_ncols=(1, 2),
axes_pad=0.3,
share_all=True,
cbar_location="bottom",
cbar_mode="each",
cbar_size="8%",
cbar_pad="2%",
)
Imagegridを用いて画像を表示する。Imagegridについては下記で解説した。

[matplotlib] 19. mpl_toolkits.axes_grid1 の使い方(画像の表示)
axes_grid1のImageGridを用いて複数の画像を表示する方法

[matplotlib] 20. mpl_toolkits.axes_grid1 の使い方(ラベルとカラーバー)
axes_grid1を用いて複数の画像を表示する際のラベルとカラーバーの設定方法
default settingsの画像
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
grid[0].set_title('RAG drawn with default settings')
lc = graph.show_rag(labels, g, img, ax=grid[0])
grid.cbar_axes[0].colorbar(lc)
imgとRAGが同じような色調で表示される。
画像とRAGのcmapを変えた画像
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
grid[1].set_title('RAG drawn with grayscale image and viridis colormap')
lc2 = graph.show_rag(labels, g, img,
img_cmap='gray', edge_cmap='viridis', ax=grid[1])
grid.cbar_axes[1].colorbar(lc2)
グレースケール画像にviridisのRAGを表示した。
コードをダウンロード参考
Drawing Region Adjacency Graphs (RAGs) — skimage 0.25.2 documentation
skimage.segmentation — skimage 0.25.2 documentation
Page not found · GitHub Pages
Page not found · GitHub Pages
コメント