Loading [MathJax]/extensions/tex2jax.js

[scikit-image] 42. HoG(Histogram of Oriented Gradients)による画像の特徴抽出(skimage.feature hog)

python

はじめに

ここでは、skimage feature hog(Histogram of Oriented Gradients)により画像の特徴量を抽出した例を示す。

コード

import matplotlib.pyplot as plt
from skimage.feature import hog
from skimage import data, exposure
from skimage.color import rgb2gray
image = plt.imread('rofofora.jpg')
fd, hog_image = hog(image, orientations=8, pixels_per_cell=(16, 16),
cells_per_block=(2,2), visualize=True, multichannel=True)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4), sharex=True, sharey=True)
ax1.axis('off')
ax1.imshow(image, cmap=plt.cm.gray)
ax1.set_title('Input image')
# Rescale histogram for better display
hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 4))
ax2.axis('off')
ax2.imshow(hog_image_rescaled, cmap=plt.cm.gray)
ax2.set_title('Histogram of Oriented Gradients')
plt.savefig("HOG_rofo.jpg",dpi=150)
plt.show()
view raw plot_HOG.py hosted with ❤ by GitHub
コードをダウンロード

解説

モジュールのインポート

import matplotlib.pyplot as plt
from skimage.feature import hog
from skimage import data, exposure
from skimage.color import rgb2gray
view raw plot_HOG.py hosted with ❤ by GitHub

画像データの読み込み

image = plt.imread('rofofora.jpg')
view raw plot_HOG.py hosted with ❤ by GitHub

HoG

fd, hog_image = hog(image, orientations=8, pixels_per_cell=(16, 16),
cells_per_block=(2,2), visualize=True, multichannel=True)
view raw plot_HOG.py hosted with ❤ by GitHub

HoGについては下記が詳しい。

http://lang.sist.chukyo-u.ac.jp/Classes/ComputerVision/KeypointDescription.html#HOG%E8%A8%98%E8%BF%B0%E5%AD%90
https://www.jstage.jst.go.jp/article/itej/64/3/64_3_322/_pdf/-char/ja

画像の表示

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4), sharex=True, sharey=True)
ax1.axis('off')
ax1.imshow(image, cmap=plt.cm.gray)
ax1.set_title('Input image')
# Rescale histogram for better display
hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 4))
ax2.axis('off')
ax2.imshow(hog_image_rescaled, cmap=plt.cm.gray)
ax2.set_title('Histogram of Oriented Gradients')
plt.savefig("HOG_rofo.jpg",dpi=150)
plt.show()
view raw plot_HOG.py hosted with ❤ by GitHub

参考

Histogram of Oriented Gradients — skimage 0.25.2 documentation
http://lang.sist.chukyo-u.ac.jp/Classes/ComputerVision/KeypointDescription.html#HOG%E8%A8%98%E8%BF%B0%E5%AD%90
https://www.jstage.jst.go.jp/article/itej/64/3/64_3_322/_pdf/-char/ja

コメント