はじめに
ここでは、skimage feature hog(Histogram of Oriented Gradients)により画像の特徴量を抽出した例を示す。
コード
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
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() |

解説
モジュールのインポート
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
import matplotlib.pyplot as plt
from skimage.feature import hog
from skimage import data, exposure
from skimage.color import rgb2gray
画像データの読み込み
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
image = plt.imread('rofofora.jpg')
HoG
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
fd, hog_image = hog(image, orientations=8, pixels_per_cell=(16, 16),
cells_per_block=(2,2), visualize=True, multichannel=True)
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
画像の表示
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, (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()
参考
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
コメント