はじめに
ここでは、skimage.utilのpadによる画像の周囲を補間する方法について説明する。
コード
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
%matplotlib inline | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from skimage.util import pad | |
img = np.zeros((10, 10)) | |
img[:5, :5] += 1 | |
img[:3, :3] += 1 | |
img[5, 5] = 1 | |
modes = ['constant', 'edge', 'wrap', 'reflect', 'symmetric','linear_ramp','maximum','mean','median'] | |
fig, axes = plt.subplots(3, 3,figsize=(8,8)) | |
ax = axes.flatten() | |
for n, mode in enumerate(modes): | |
img_padded = pad(img, pad_width=img.shape[0], mode=mode) | |
ax[n].imshow(img_padded, cmap=plt.cm.gray, interpolation='nearest') | |
ax[n].plot([9.5, 9.5, 19.5, 19.5, 9.5], | |
[9.5, 19.5, 19.5, 9.5, 9.5], 'c--', linewidth=0.5) | |
ax[n].set_title(mode) | |
for a in ax: | |
#a.set_axis_off() | |
a.set_aspect('equal') | |
plt.tight_layout() | |
plt.savefig('edgemode.jpg',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
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from skimage.util import pad
画像データの作成
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 = np.zeros((10, 10))
img[:5, :5] += 1
img[:3, :3] += 1
img[5, 5] = 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
fig, axes = plt.subplots(3, 3,figsize=(8,8))
ax = axes.flatten()
画像の補間
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
modes = ['constant', 'edge', 'wrap', 'reflect', 'symmetric','linear_ramp','maximum','mean','median']
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
for n, mode in enumerate(modes):
img_padded = pad(img, pad_width=img.shape[0], mode=mode)
ax[n].imshow(img_padded, cmap=plt.cm.gray, interpolation='nearest')
ax[n].plot([9.5, 9.5, 19.5, 19.5, 9.5],
[9.5, 19.5, 19.5, 9.5, 9.5], 'c--', linewidth=0.5)
ax[n].set_title(mode)
modesのリストに示した方法で補間する。
pad()により、画像の補間ができる。pad_widthをimg[0].shape(10)としているので、補間された画像のサイズは(30,30)となる。
ax[n].plot([9.5, ・・・, 9.5], ‘c–‘, linewidth=0.5)で、元の画像の部分にシアン色の線を表示する。
補間のモード
‘constant’
定数値で補間する。
‘edge’
エッジの値で補間する。
‘wrap’
画像と同様の画像で補間する。
‘reflect’
各軸で反射した画像になるように補間される。
‘symmetric’
各軸に対して対称的になるように補間される。
‘linear_ramp’
end_valueとエッジの値の間での線形補間となる。
‘maximum’
各軸に対して、ベクトルのすべて、または、一部の最大値で補間される。
‘mean’
各軸に対して、ベクトルのすべて、または、一部の平均値で補間される。
‘median’
各軸に対して、ベクトルのすべて、または、一部の中央値で補間される。
図のアスペクト比を揃える設定
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
for a in ax:
#a.set_axis_off()
a.set_aspect('equal')
参考
Interpolation: Edge Modes — skimage 0.25.2 documentation
コメント