開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Python 3.7 (プログラミング言語)
-
パッケージ
- NumPy
- SciPy
- Matplotlib
- scikit-image
- PIL(Python Image Library, Pillow)
エレガントなSciPy (Juan Nunez-Iglesias (著)、Stéfan van der Walt(著)、Harriet Dashnow(著)、山崎 邦子(翻訳)、山崎 康宏(翻訳)、オライリージャパン)の3章(ndimageを使った画像領域のネットワーク)、3.4(汎用フィルタ: 近傍データの任意の関数)、3.4.2(演習: ソーベル勾配の大きさ)の解答を求めてみる。
コード
Python 3
#!/usr/bin/env python3
import numpy as np
from scipy import ndimage
import matplotlib.pyplot as plt
from PIL import Image
import skimage
horizontal_sobel = np.array([[1, 2, 1],
[0, 0, 0],
[-1, -2, -1]])
vertical_sobel = horizontal_sobel.T
def sobel_filter(element):
coins_horizontal = element.dot(horizontal_sobel.reshape(9))
coins_vertical = element.dot(vertical_sobel.reshape(9))
return np.sqrt(coins_horizontal ** 2 + coins_vertical ** 2)
try:
coins_url = ('https://raw.githubusercontent.com/scikit-image/scikit-image/'
'master/skimage/data/coins.png')
coins = skimage.io.imread(coins_url)
fig, axes = plt.subplots(nrows=2, ncols=1)
axes[0].imshow(coins)
coins_sobel = ndimage.generic_filter(coins, sobel_filter, size=3)
axes[1].imshow(coins_sobel)
fig.savefig('coins_sobel.png')
except Exception as err:
print(type(err), err)
入出力結果(cmd(コマンドプロンプト)、Terminal、Jupyter(IPython))
$ python3 sample3.py $
0 コメント:
コメントを投稿