Added unit test to check correct output of HOG on Lena grayscale image, and added feature_vector parameter to disable the .ravel() call on the result.

This commit is contained in:
Korijn van Golen
2015-12-04 10:42:12 +01:00
parent 2adab1560a
commit c095daa234
3 changed files with 24 additions and 4 deletions
Binary file not shown.
+10 -3
View File
@@ -5,7 +5,8 @@ from . import _hoghistogram
def hog(image, orientations=9, pixels_per_cell=(8, 8),
cells_per_block=(3, 3), visualise=False, normalise=False):
cells_per_block=(3, 3), visualise=False, normalise=False,
feature_vector=True):
"""Extract Histogram of Oriented Gradients (HOG) for a given image.
Compute a Histogram of Oriented Gradients (HOG) by
@@ -31,6 +32,9 @@ def hog(image, orientations=9, pixels_per_cell=(8, 8),
normalise : bool, optional
Apply power law compression to normalise the image before
processing.
feature_vector : bool, optional
Return the data as a feature vector by calling .ravel() on the result
just before returning.
Returns
-------
@@ -169,8 +173,11 @@ def hog(image, orientations=9, pixels_per_cell=(8, 8),
overlapping grid of blocks covering the detection window into a combined
feature vector for use in the window classifier.
"""
if feature_vector:
normalised_blocks = normalised_blocks.ravel()
if visualise:
return normalised_blocks.ravel(), hog_image
return normalised_blocks, hog_image
else:
return normalised_blocks.ravel()
return normalised_blocks
+14 -1
View File
@@ -1,5 +1,7 @@
import os
import numpy as np
from scipy import ndimage as ndi
import skimage as si
from skimage import data
from skimage import feature
from skimage import img_as_float
@@ -9,7 +11,7 @@ from numpy.testing import (assert_raises,
)
def test_histogram_of_oriented_gradients():
def test_histogram_of_oriented_gradients_output_size():
img = img_as_float(data.astronaut()[:256, :].mean(axis=2))
fd = feature.hog(img, orientations=9, pixels_per_cell=(8, 8),
@@ -18,6 +20,17 @@ def test_histogram_of_oriented_gradients():
assert len(fd) == 9 * (256 // 8) * (512 // 8)
def test_histogram_of_oriented_gradients_output_correctness():
img = np.load(os.path.join(si.data_dir, 'lena_GRAY_U8.npy'))
correct_output = np.load(os.path.join(si.data_dir, 'lena_GRAY_U8_hog.npy'))
output = feature.hog(img, orientations=9, pixels_per_cell=(8, 8),
cells_per_block=(3, 3), feature_vector=True,
normalise=False, visualise=False)
assert_almost_equal(output, correct_output)
def test_hog_image_size_cell_size_mismatch():
image = data.camera()[:150, :200]
fd = feature.hog(image, orientations=9, pixels_per_cell=(8, 8),