Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PIP2020-10-note2.ipynb
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "PIP2020-10-note2.ipynb",
"provenance": [],
"collapsed_sections": [],
"toc_visible": true,
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/takatakamanbou/aa7e98d2df882424251735c0873b597b/pip2020-10-note2.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "NI3mY1FGZWgV",
"colab_type": "text"
},
"source": [
"# 2020年度パターン情報処理第10回講義資料その2\n",
"\n",
"この科目のウェブサイトへ https://www-tlab.math.ryukoku.ac.jp/wiki/?PIP/2020\n",
"\n",
"![hoge](https://www-tlab.math.ryukoku.ac.jp/~takataka/course/PIP/PIP-logo-96x96.png)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ffaGVK2o5VWe",
"colab_type": "text"
},
"source": [
"## はじめに"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0IpujRN_Wuib",
"colab_type": "text"
},
"source": [
"### これは何?\n",
"\n",
"これは,Google Colaboratory(以下 Google Colab) という,Google が提供しているサービスを利用して作成した資料です.Notebook と呼びます.クラウド上に仮想的に構築された Linux マシン上で Python のプログラムを実行することができます."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "QRNjWashW4pr",
"colab_type": "text"
},
"source": [
"### Notebook の動かし方\n",
"\n",
"この Notebook では,上の方のセルで作った変数や関数を後のセルで使うことがあります.そのため,上の方のセルを実行せずに下の方を実行すると,エラーになることがあります.上から順に実行していきましょう.\n",
"\n",
"また,メニューの「ランタイム」から,「すべてのセルを実行」したりすることも可能です."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "1jfSXIY6VNxP",
"colab_type": "text"
},
"source": [
"## 準備\n",
"\n",
"以下のセルは,プログラムを実行するための準備を行うためのものです.このセルを実行してから,先へ進みましょう."
]
},
{
"cell_type": "code",
"metadata": {
"id": "IFYQk7ONZENz",
"colab_type": "code",
"colab": {}
},
"source": [
"# 科学技術計算のライブラリ NumPy のモジュールを np という名前で使えるようにする\n",
"import numpy as np\n",
"# 科学技術計算のライブラリ SciPy の中の WAVE ファイルを扱うモジュール を wavfile という名前で使えるようにする\n",
"import scipy.io.wavfile as wavfile\n",
"# グラフを描くためのライブラリ matplotlib の pyplot を plt という名前でインポート\n",
"import matplotlib.pyplot as plt\n",
"# コンピュータビジョン・画像処理のためのライブラリ OpenCV のモジュール cv2 をインポート\n",
"import cv2\n",
"# Google Colab 上へ/からのファイルのアップロード/ダウンロードのためのほげ\n",
"from google.colab import files\n",
"\n",
"# matplotlib が描くグラフの文字サイズをデフォルト 12pt から 18pt へ\n",
"plt.rcParams[\"font.size\"] = 18"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "bUpNOXqZZnPU",
"colab_type": "text"
},
"source": [
"## ★ 気温データの時間領域でのフィルタリング\n",
"\n",
"\n",
"講義資料にある気温データを時間領域でフィルタリングしてみよう."
]
},
{
"cell_type": "code",
"metadata": {
"id": "ZIl-OpriwQ9g",
"colab_type": "code",
"colab": {}
},
"source": [
"# 気温のデータを入手する\n",
"! wget -nc https://www-tlab.math.ryukoku.ac.jp/~takataka/course/PIP/temp2004.txt\n",
"! ls -l\n",
"# 入手したファイルを読み込んで temp2004 という変数に入れておく\n",
"temp2004 = np.loadtxt('temp2004.txt')\n",
"print(temp2004)\n",
"print(temp2004.shape) # 366次元ベクトル"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "nDoKX-ohivJq"
},
"source": [
"### 気温変化のグラフを描いてみる"
]
},
{
"cell_type": "code",
"metadata": {
"colab_type": "code",
"id": "fBXeJjH2ivJq",
"colab": {}
},
"source": [
"# t と temp2004 でグラフを描く\n",
"t = np.arange(0, 366)\n",
"fig, ax = plt.subplots(facecolor=\"white\", figsize=(8, 6))\n",
"ax.plot(t, temp2004, \"-\", label = \"temp2004\")\n",
"ax.legend()\n",
"ax.set_xlim([0, 365]) # 横軸の範囲\n",
"ax.set_ylim([-5, 35]) # 縦軸の範囲\n",
"plt.show()"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "XbhO7Gq6ivJs"
},
"source": [
"### 平滑化してみる"
]
},
{
"cell_type": "code",
"metadata": {
"colab_type": "code",
"id": "9b6XMT0qivJt",
"colab": {}
},
"source": [
"for L in [3, 10, 20]:\n",
" w = np.ones(L) / L # 長さ L の平滑化フィルタ\n",
" print('# L = ', L, w)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "CY3dY0OZNcgC",
"colab_type": "code",
"colab": {},
"cellView": "both"
},
"source": [
"#@title 平滑化フィルタの長さ L をいろいろ変えてフィルタリングした結果を観察しよう { run: \"auto\" }\n",
"L = 1 #@param {type:\"slider\", min:1, max:100, step:1}\n",
"w = np.ones(L) / L\n",
"\n",
"# フィルタリング(畳み込み,コンボリューション)\n",
"filtered = np.convolve(temp2004, w, mode = 'same')\n",
"\n",
"# t と temp2004, filtered のグラフを描く\n",
"t = np.arange(0, 366)\n",
"fig, ax = plt.subplots(facecolor=\"white\", figsize=(8, 6))\n",
"ax.plot(t, temp2004, \"-\", label = \"temp2004\")\n",
"ax.plot(t, filtered, \"-\", label = f\"filtered (L={L})\", color = 'red')\n",
"ax.legend()\n",
"ax.set_xlim([0, 365]) # 横軸の範囲\n",
"ax.set_ylim([-5, 35]) # 縦軸の範囲\n",
"plt.show()"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "0RdB2_UzPMIe",
"colab_type": "text"
},
"source": [
"上記のフィルタリングでは,講義資料に書いてある例とは違い,L を大きくしても端の値も計算してくれるような方法になってます."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "KAVgY2oYksTq",
"colab_type": "text"
},
"source": [
"## ★ 気温データの周波数領域でのフィルタリング\n",
"\n",
"\n",
"今度は周波数領域でフィルタリングしてみよう."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "x5VnStbiP8Jm",
"colab_type": "text"
},
"source": [
"### FFTして振幅スペクトルを描いてみる"
]
},
{
"cell_type": "code",
"metadata": {
"id": "_A98IpLvk4X_",
"colab_type": "code",
"colab": {}
},
"source": [
"# FFT する\n",
"N = temp2004.shape[0]\n",
"C = np.fft.fft(temp2004) / N"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "l7Q1eHjNlPNS",
"colab_type": "code",
"colab": {}
},
"source": [
"# 振幅スペクトルを描く\n",
"spec = np.abs(C)\n",
"k = np.arange(0, N)\n",
"fig, ax = plt.subplots(facecolor=\"white\", figsize=(8, 6))\n",
"ax.plot(k[:N//2], spec[:N//2], \"-\", label = \"|C|\")\n",
"ax.legend()\n",
"ax.set_xlim([0, N//2]) # 横軸の範囲\n",
"ax.set_ylim([0, 0.5]) # 縦軸の範囲\n",
"plt.show()"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "xm1lZ_armt3L",
"colab_type": "text"
},
"source": [
"### フィルタリングする"
]
},
{
"cell_type": "code",
"metadata": {
"id": "hKmNREmYmpC2",
"colab_type": "code",
"colab": {}
},
"source": [
"# フィルタリングする\n",
"k_thresh = 30\n",
"CFUGA = np.zeros_like(C)\n",
"CFUGA[k_thresh:N-k_thresh+1] = C[k_thresh:N-k_thresh+1]\n",
"CHOGE = np.zeros_like(C)\n",
"CHOGE[:k_thresh] = C[:k_thresh]\n",
"CHOGE[N-k_thresh+1:] = C[N-k_thresh+1:]"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "XRcQjHY5nLEH",
"colab_type": "code",
"colab": {}
},
"source": [
"# 振幅スペクトルを描く\n",
"specHOGE = np.abs(CHOGE)\n",
"specFUGA = np.abs(CFUGA)\n",
"k = np.arange(0, N)\n",
"fig, ax = plt.subplots(2, facecolor=\"white\", figsize=(8, 12))\n",
"ax[0].plot(k[:N//2], specHOGE[:N//2], \"-\", label = \"amplitude spectrum HOGE\")\n",
"ax[0].legend()\n",
"ax[0].set_xlim([0, N//2]) # 横軸の範囲\n",
"ax[0].set_ylim([0, 0.5]) # 縦軸の範囲\n",
"ax[1].plot(k[:N//2], specFUGA[:N//2], \"-\", label = \"amplitude spectrum FUGA\")\n",
"ax[1].legend()\n",
"ax[1].set_xlim([0, N//2]) # 横軸の範囲\n",
"ax[1].set_ylim([0, 0.5]) # 縦軸の範囲\n",
"plt.show()"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "0tvdPLJp0P65",
"colab_type": "text"
},
"source": [
"上記の振幅スペクトルと以下の波形のグラフを考察する問題が moodle 上にあります.上記の HOGE とか FUGA とかいう謎の名前は,その問題のためのものです.どちらかが元の信号にローパスフィルタリングした結果で,どちらかがハイパスフィルタリングした結果.どっちがどっちだろう?"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "s_hLMaQFp5vW",
"colab_type": "text"
},
"source": [
"### 逆変換して元の波形と比較してみる"
]
},
{
"cell_type": "code",
"metadata": {
"id": "F3jMhw5lnlv0",
"colab_type": "code",
"colab": {}
},
"source": [
"# 逆変換する\n",
"filtered2 = (np.fft.ifft(CFUGA) * N).real\n",
"filtered3 = (np.fft.ifft(CHOGE) * N).real"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "EBxTIatnohmY",
"colab_type": "code",
"colab": {}
},
"source": [
"# t と temp2004, filtered2, filtered3 のグラフを描く\n",
"t = np.arange(0, 366)\n",
"fig, ax = plt.subplots(facecolor=\"white\", figsize=(8, 6))\n",
"ax.plot(t, temp2004, \"-\", label = \"temp2004\")\n",
"ax.plot(t, filtered2, \"-\", label = \"filtered2\", color = 'green')\n",
"ax.plot(t, filtered3, \"-\", label = \"filtered3\", color = 'red')\n",
"ax.legend()\n",
"ax.set_xlim([0, 365]) # 横軸の範囲\n",
"ax.set_ylim([-5, 35]) # 縦軸の範囲\n",
"plt.show()"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "IUodWFY21Fdp",
"colab_type": "text"
},
"source": [
"先の振幅スペクトルと上記の波形を対応付けて考察する問題が moodle 上にあります.filtered2, filtered3 は一方が HOGE に,他方が FUGA に対応してます.どっちがどっちだろう?"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "E18iye_YvkrU",
"colab_type": "text"
},
"source": [
"## ★ 音響信号のフィルタリング"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "CbVZtlqvv2C0"
},
"source": [
"### データの入手"
]
},
{
"cell_type": "code",
"metadata": {
"id": "bvtsGG5uqXMY",
"colab_type": "code",
"colab": {}
},
"source": [
"# 音響データのファイルを入手する\n",
"! wget -nc https://www-tlab.math.ryukoku.ac.jp/~takataka/course/PIP/hw09.npz\n",
"! ls -l\n",
"# ファイルの中身を取り出す\n",
"hw09 = np.load('hw09.npz')\n",
"C_org, C_noisy, C_lowpass = hw09['C_org'], hw09['C_noisy'], hw09['C_lowpass'] # 3つの音響データをフーリエ変換した値\n",
"# これらの音響データの標本化周波数\n",
"framerate = 44100"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "_3cSDHT5v2C5"
},
"source": [
"### 入手したデータの振幅スペクトルを描いてみる"
]
},
{
"cell_type": "code",
"metadata": {
"colab_type": "code",
"id": "Wt_hofiYv2C6",
"colab": {}
},
"source": [
"# 振幅スペクトルを求める\n",
"amp = np.abs(C_noisy)\n",
"# 振幅スペクトルをグラフに描く\n",
"N = amp.shape[0]\n",
"freq = np.arange(N) * framerate / N # 横軸の値.単位は Hz\n",
"fig, ax = plt.subplots(facecolor=\"white\", figsize=(8, 6))\n",
"ax.plot(freq, amp, \"-\", label = \"|C_noisy|\")\n",
"ax.legend()\n",
"ax.set_xlim([0, 8000]) # 横軸の範囲\n",
"ax.set_ylim([0, 1100]) # 縦軸の範囲\n",
"plt.show()"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "VxEjQYnNceJk",
"colab_type": "text"
},
"source": [
"### ローパスフィルタリングしてみる"
]
},
{
"cell_type": "code",
"metadata": {
"id": "TWsNseXZcc4t",
"colab_type": "code",
"colab": {}
},
"source": [
"# ローパスフィルタリングする関数\n",
"#\n",
"def lowpass(C, framerate, cutoff):\n",
"\n",
" Ct = np.copy(C)\n",
" if cutoff >= framerate/2:\n",
" return Ct # 遮断周波数が標本化周波数の半分以上なら C のコピーを返すだけ \n",
"\n",
" N = C.shape[0]\n",
" kmax = cutoff * N // framerate\n",
" Ct[kmax:N-kmax] = 0.0\n",
" \n",
" return Ct"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "nE7xOak_cjt7",
"colab_type": "code",
"colab": {}
},
"source": [
"# ローパスフィルタリング\n",
"cutoff = 4000 # 遮断周波数 4000 Hz\n",
"#cutoff = 3000 # 遮断周波数 3000 Hz\n",
"C_filtered = lowpass(C_noisy, framerate, cutoff)\n",
"\n",
"# 振幅スペクトルを求める\n",
"amp = np.abs(C_filtered)\n",
"\n",
"# 振幅スペクトルをグラフに描く\n",
"N = amp.shape[0]\n",
"freq = np.arange(N) * framerate / N # 横軸の値.単位は Hz\n",
"fig, ax = plt.subplots(facecolor=\"white\", figsize=(8, 6))\n",
"ax.plot(freq, amp, \"-\", label = \"|C_filtered|\")\n",
"ax.legend()\n",
"ax.set_xlim([0, 8000]) # 横軸の範囲\n",
"ax.set_ylim([0, 1100]) # 縦軸の範囲\n",
"plt.show()"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "HZXzVimTv2DB"
},
"source": [
"### 音に戻して聞いてみよう\n",
"\n",
"元データ C_noisy とそれにローパスフィルタリングを適用した結果 C_filtered をそれぞれ逆変換して,WAVE形式で保存"
]
},
{
"cell_type": "code",
"metadata": {
"colab_type": "code",
"id": "FuEDCISKv2DB",
"colab": {}
},
"source": [
"# C_noisy と C_filtered を逆変換して元の信号を求める\n",
"f_noisy = np.fft.ifft(C_noisy).real * N\n",
"f_filtered = np.fft.ifft(C_filtered).real * N\n",
"# WAVE 形式のファイルとして保存\n",
"wavfile.write('noisy.wav', framerate, f_noisy.astype(np.int16))\n",
"wavfile.write(f'filtered_cutoff{cutoff}.wav', framerate, f_filtered.astype(np.int16))\n",
"! ls -l"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "I4mtscmOEAJz",
"colab_type": "code",
"colab": {}
},
"source": [
"# ファイルをダウンロード\n",
"files.download('noisy.wav')\n",
"files.download(f'filtered_cutoff{cutoff}.wav')"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "3lVymv9Ffdg9",
"colab_type": "text"
},
"source": [
"**ここまで来たら,次のことをやりなさい**\n",
"\n",
"↑のセルに含まれる lowpass 関数の呼び出しの直前の行(下記のようにかかれているところ)のコメントを付け外しして cutoff を 4000 から 3000 に変更してそれ以降のセルを実行し直そう.noisy.wav と filtered_cutoff3000.wav を聴き比べよう.\n",
"\n",
"\n",
"```\n",
"# ローパスフィルタリング\n",
"cutoff = 4000 # 遮断周波数 4000 Hz\n",
"#cutoff = 3000 # 遮断周波数 3000 Hz\n",
"C_filtered = lowpass(C_noisy, framerate, cutoff)\n",
"```\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "si108G8636qi",
"colab_type": "text"
},
"source": [
"## ★ 画像の空間領域でのフィルタリング"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "gdZrAtE54CCW"
},
"source": [
"### 画像の準備\n"
]
},
{
"cell_type": "code",
"metadata": {
"colab_type": "code",
"id": "T4eMKID578kx",
"colab": {}
},
"source": [
"from IPython.display import display, Image\n",
"\n",
"### 画像表示用の関数\n",
"#\n",
"def displayImage(img):\n",
" \n",
" if type(img) is np.ndarray: # 渡されたのが np.ndarray だった(OpenCVの画像はこの形式)\n",
" rv, buf = cv2.imencode('.png', img) # PNG形式のバイト列に変換\n",
" if rv:\n",
" display(Image(data = buf.tobytes())) # 変換できたらバイト列を渡して表示\n",
" return\n",
" elif type(img) is str: # 渡されたのが文字列だった\n",
" display(Image(filename = img))\n",
" return\n",
" \n",
" print('displayImage: error')"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab_type": "code",
"id": "hUsptHij4CCY",
"colab": {}
},
"source": [
"# 画像を入手\n",
"! wget -nc https://www-tlab.math.ryukoku.ac.jp/~takataka/course/PIP/filtering-input.png\n",
"! wget -nc https://www-tlab.math.ryukoku.ac.jp/~takataka/course/PIP/filtering-output.png\n",
"! ls -l\n",
"# 入手した画像を読み込む\n",
"filteringIn = cv2.imread('filtering-input.png', cv2.IMREAD_GRAYSCALE)\n",
"filteringOut = cv2.imread('filtering-output.png', cv2.IMREAD_GRAYSCALE)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "sK2V3D20ONIY",
"colab_type": "code",
"colab": {}
},
"source": [
"# 画像を表示\n",
"fig = plt.figure(figsize = (5, 5))\n",
"ax1 = fig.add_subplot(121)\n",
"ax1.axis('off')\n",
"ax1.imshow(filteringIn, cmap = 'gray', vmin = 0, vmax = 255)\n",
"ax2 = fig.add_subplot(122)\n",
"ax2.axis('off')\n",
"ax2.imshow(filteringOut, cmap = 'gray', vmin = 0, vmax = 255)\n",
"plt.show()"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "9ZkL0txuWjiN",
"colab_type": "text"
},
"source": [
"**以下の問に答えなさい(答えをメモしておこう)**\n",
"\n",
"上記左のグレイスケール画像に対して,以下に示された4つのフィルタのいずれかを用いてフィルタリングを行うと,上記右の画像が得られた.ただし,この画像では,フィルタが画像の範囲からはみ出す外周部についてはグレーに対応した画素値が代入してある.また,フィルタリング結果が負の値もとり得る場合は,0 がグレーに対応するようにして,正の値を白,負の値を黒で表示してある.用いたフィルタとして正しいものを選びなさい.\n",
"\n",
"$$\n",
"\\begin{array}{cccc}\n",
"\\begin{array}{|r|r|r|r|}\n",
"\\hline\n",
"\\frac{1}{16} & \\frac{1}{16} & \\frac{1}{16} & \\frac{1}{16} \\\\\n",
"\\hline\n",
"\\frac{1}{16} & \\frac{1}{16} & \\frac{1}{16} & \\frac{1}{16} \\\\\n",
"\\hline\n",
"\\frac{1}{16} & \\frac{1}{16} & \\frac{1}{16} & \\frac{1}{16} \\\\\n",
"\\hline\n",
"\\frac{1}{16} & \\frac{1}{16} & \\frac{1}{16} & \\frac{1}{16} \\\\\n",
"\\hline\n",
"\\end{array}\n",
"&\n",
"\\begin{array}{|r|r|r|r|}\n",
"\\hline\n",
"+1 & +1 & +1 & +1 \\\\\n",
"\\hline\n",
"+1 & +1 & +1 & +1 \\\\\n",
"\\hline\n",
"-1 & -1 & -1 & -1 \\\\\n",
"\\hline\n",
"-1 & -1 & -1 & -1 \\\\\n",
"\\hline\n",
"\\end{array}\n",
"&\n",
"\\begin{array}{|r|r|r|r|}\n",
"\\hline\n",
"-1 & -1 & +1 & +1 \\\\\n",
"\\hline\n",
"-1 & -1 & +1 & +1 \\\\\n",
"\\hline\n",
"-1 & -1 & +1 & +1 \\\\\n",
"\\hline\n",
"-1 & -1 & +1 & +1 \\\\\n",
"\\hline\n",
"\\end{array}\n",
"&\n",
"\\begin{array}{|r|r|r|r|}\n",
"\\hline\n",
"-1 & -1 & -1 & -1 \\\\\n",
"\\hline\n",
"-1 & -1 & -1 & -1 \\\\\n",
"\\hline\n",
"+1 & +1 & +1 & +1 \\\\\n",
"\\hline\n",
"+1 & +1 & +1 & +1 \\\\\n",
"\\hline\n",
"\\end{array}\n",
"\\\\\n",
"A & B & C & D\n",
"\\end{array}\n",
"$$"
]
},
{
"cell_type": "code",
"metadata": {
"id": "Mcn5bWtUYUvD",
"colab_type": "code",
"colab": {}
},
"source": [
"# 画像を入手\n",
"! wget -nc https://www-tlab.math.ryukoku.ac.jp/~takataka/course/PIP/lena.jpg\n",
"! ls -l\n",
"# 入手した画像を読み込む\n",
"lena = cv2.imread('lena.jpg', cv2.IMREAD_GRAYSCALE) # グレイスケール画像として読み込む\n",
"# 画像を表示\n",
"displayImage(lena)\n",
"print(lena.shape)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "GA4G3H-Obw9C",
"colab_type": "code",
"colab": {}
},
"source": [
"# 平滑化フィルタの値を表示\n",
"for S in [2, 3, 4, 5]:\n",
" filter = np.ones((S, S), dtype = float) / (S*S)\n",
" print('# {0}x{0} の平滑化フィルタ'.format(S))\n",
" print(filter)\n",
" print()"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "8LWqJNR1Ax1A",
"colab_type": "code",
"colab": {},
"cellView": "both"
},
"source": [
"#@title 平滑化フィルタの大きさ S を変えるとフィルタリングした結果がどうなるか観察しよう { run: \"auto\" }\n",
"S = 1 #@param {type:\"slider\", min:1, max:64, step:1}\n",
"\n",
"# 平滑化フィルタの作成\n",
"filter = np.ones((S, S), dtype = float) / (S*S)\n",
"# フィルタリング\n",
"img = cv2.filter2D(lena, -1, filter)\n",
"# 結果の表示\n",
"displayImage(img)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "IxugCN5y574v",
"colab_type": "code",
"colab": {}
},
"source": [
""
],
"execution_count": 0,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment