From f3024fc4cda567058dea1f72c5e7f0e9ecb95fe5 Mon Sep 17 00:00:00 2001 From: Brian Holt Date: Tue, 24 Jul 2012 10:52:55 +0100 Subject: [PATCH] simplified hog code + extra unit test --- skimage/feature/hog.py | 11 +++++------ skimage/feature/tests/test_hog.py | 21 ++++++++++++--------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/skimage/feature/hog.py b/skimage/feature/hog.py index 4a24b0a2..5206034e 100644 --- a/skimage/feature/hog.py +++ b/skimage/feature/hog.py @@ -107,6 +107,7 @@ def hog(image, orientations=9, pixels_per_cell=(8, 8), # compute orientations integral images orientation_histogram = np.zeros((n_cellsy, n_cellsx, orientations)) + subsample = np.index_exp[cy / 2:cy * n_cellsy:cy, cx / 2:cx * n_cellsx:cx] for i in range(orientations): #create new integral image for this orientation # isolate orientations in this range @@ -119,19 +120,17 @@ def hog(image, orientations=9, pixels_per_cell=(8, 8), cond2 = temp_ori > 0 temp_mag = np.where(cond2, magnitude, 0) - orientation_histogram[:, :, i] = uniform_filter(temp_mag, - size=(cy, cx))[cy / 2::cy, cx / 2::cx] + temp_filt = uniform_filter(temp_mag, size=(cy, cx)) + orientation_histogram[:, :, i] = temp_filt[subsample] # now for each cell, compute the histogram - #orientation_histogram = np.zeros((n_cellsx, n_cellsy, orientations)) - radius = min(cx, cy) // 2 - 1 hog_image = None - if visualise: - hog_image = np.zeros((sy, sx), dtype=float) if visualise: from skimage import draw + radius = min(cx, cy) // 2 - 1 + hog_image = np.zeros((sy, sx), dtype=float) for x in range(n_cellsx): for y in range(n_cellsy): for o in range(orientations): diff --git a/skimage/feature/tests/test_hog.py b/skimage/feature/tests/test_hog.py index 18c13c85..90e08105 100644 --- a/skimage/feature/tests/test_hog.py +++ b/skimage/feature/tests/test_hog.py @@ -1,18 +1,21 @@ -import numpy as np -import scipy - -from skimage.feature import hog - +from skimage import data +from skimage import feature +from skimage import img_as_float def test_histogram_of_oriented_gradients(): - # Replace with skimage.data.lena() after merge - img = scipy.misc.lena()[:256, :].astype(np.int8) + img = img_as_float(data.lena()[:256, :].mean(axis=2)) - fd = hog(img, orientations=9, pixels_per_cell=(8, 8), - cells_per_block=(1, 1)) + fd = feature.hog(img, orientations=9, pixels_per_cell=(8, 8), + cells_per_block=(1, 1)) assert len(fd) == 9 * (256 // 8) * (512 // 8) +def test_hog_image_size_cell_size_mismatch(): + image = data.camera()[:150, :200] + fd = feature.hog(image, orientations=9, pixels_per_cell=(8, 8), + cells_per_block=(1, 1)) + assert len(fd) == 9 * (150 // 8) * (200 // 8) + if __name__ == '__main__': from numpy.testing import run_module_suite run_module_suite()