キカガク(KIKAGAKU)で「畳み込みニューラルネットワークの基礎」を学ぶ

Photo by Owen Beard on Unsplash

実習環境

Colaboratory を使わない場合、 OpenCV をインストールしないといけないが、ここに書かれている cv2 だとインストールできなかった。

pip install opencv_python でインストールした。 

なお、PIL はすでに pip install pillow でインストール済み。

学習内容

画像処理の実装

  1. OpenCV
  2. Pillow
  3. グレースケール変換

画像処理の基礎

  1. 画像とは
  2. 画像の特徴抽出
  3. 画像を切り取る
  4. ヒストグラムを取る
  5. CNN 以前の画像分類のモデル
  6. フィルタとは
  7. エッジ検出

畳み込みニューラルネットワーク

  1. 畳み込み( Convolution )
  2. プーリング( Pooling )
  3. 全結合層( Fully Connected Layer )

出力結果

OpenCV では画像が BGR の順に格納されており、Matplotlib では画像が RGB の順で格納されているため青味がかった色になっている。


OpenCV に格納された画像を cvtColor() で RGB の順に変換した結果。


グレースケール変換した結果。Matplotlib が RGB の 3 チャンネルの画像の入力を標準としているため、白黒になっていない。


plt.imshow(img_gray, cmap='gray') で表示した結果。


横方向のエッジ検出を行った結果。


縦方向のエッジ検出を行った結果。


出力ログ

<class 'numpy.ndarray'>
(512, 512, 3)
uint8
AxesImage(80,52.8;496x369.6)
AxesImage(80,52.8;496x369.6)
<class 'PIL.PngImagePlugin.PngImageFile'>
<class 'numpy.ndarray'>
(512, 512, 3)
uint8
(512, 512) (512, 512, 3)



ソースコード

import numpy as np
import matplotlib.pyplot as plt
# OpenCV のインポート
import cv2
from PIL import Image
# 画像の読み込み
img = cv2.imread('sample.png')
# 変数の型を確認
print(type(img))
# 形の確認
print(img.shape)
# データ型の確認
print(img.dtype)
print(plt.imshow(img))
plt.show()
# BGR -> RGB
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
print(plt.imshow(img_rgb))
plt.show()
# 画像の読み込み
img = Image.open('sample.png')
# 型の確認
print(type(img))
# ndarray に変換
img = np.array(img)
# 型の確認
print(type(img))
# 形の確認
print(img.shape)
# データ型の確認
print(img.dtype)
# グレースケール変換
img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
# 画像の表示
plt.imshow(img_gray)
plt.show()
print(img_gray.shape, img.shape)
# 画像を表示
plt.imshow(img_gray, cmap='gray')
plt.show()
# エッジ検出のフィルタの定義(横方向)
kernel = np.array([
[-1, 0, 1],
[-1, 0, 1],
[-1, 0, 1]
])
# 畳み込み演算
img_conv = cv2.filter2D(img_gray, -1, kernel)
# 結果を可視化
plt.imshow(img_conv, cmap='gray')
plt.show()
# エッジ検出のフィルタの定義(縦方向)
kernel = np.array([
[-1, -1, -1],
[0, 0, 0],
[1, 1, 1]
])
# 畳み込み演算
img_conv = cv2.filter2D(img_gray, -1, kernel)
# 結果を可視化
plt.imshow(img_conv, cmap='gray')
plt.show()


Posted in  on 5/31/2020 by rteak |