From a52cb01a49038870d7e0f91a9ddbcf0b027da153 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20M=C3=A1rquez=20Neila?= Date: Sun, 17 Aug 2014 22:09:03 +0200 Subject: [PATCH 1/4] Fix bugs in HOG --- skimage/feature/_hog.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/skimage/feature/_hog.py b/skimage/feature/_hog.py index 67922b92..da9f737e 100644 --- a/skimage/feature/_hog.py +++ b/skimage/feature/_hog.py @@ -1,6 +1,6 @@ import numpy as np from scipy import sqrt, pi, arctan2, cos, sin -from scipy.ndimage import uniform_filter +from scipy.ndimage import uniform_filter, convolve1d def hog(image, orientations=9, pixels_per_cell=(8, 8), @@ -80,10 +80,8 @@ def hog(image, orientations=9, pixels_per_cell=(8, 8), # to avoid problems with subtracting unsigned numbers in np.diff() image = image.astype('float') - gx = np.zeros(image.shape) - gy = np.zeros(image.shape) - gx[:, :-1] = np.diff(image, n=1, axis=1) - gy[:-1, :] = np.diff(image, n=1, axis=0) + gx = convolve1d(image, [-1,0,1], axis=1, mode='nearest') + gy = convolve1d(image, [-1,0,1], axis=0, mode='nearest') """ The third stage aims to produce an encoding that is sensitive to @@ -144,9 +142,9 @@ def hog(image, orientations=9, pixels_per_cell=(8, 8), dx = radius * cos(float(o) / orientations * np.pi) dy = radius * sin(float(o) / orientations * np.pi) rr, cc = draw.line(int(centre[0] - dx), - int(centre[1] - dy), + int(centre[1] + dy), int(centre[0] + dx), - int(centre[1] + dy)) + int(centre[1] - dy)) hog_image[rr, cc] += orientation_histogram[y, x, o] """ From 23bc3ed077b8594331047aa975b5b37db2a366de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20M=C3=A1rquez=20Neila?= Date: Mon, 18 Aug 2014 01:34:54 +0200 Subject: [PATCH 2/4] Replaced convolve1d by ad-hoc gradient computation --- skimage/feature/_hog.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/skimage/feature/_hog.py b/skimage/feature/_hog.py index da9f737e..f9608c9d 100644 --- a/skimage/feature/_hog.py +++ b/skimage/feature/_hog.py @@ -1,6 +1,6 @@ import numpy as np from scipy import sqrt, pi, arctan2, cos, sin -from scipy.ndimage import uniform_filter, convolve1d +from scipy.ndimage import uniform_filter def hog(image, orientations=9, pixels_per_cell=(8, 8), @@ -79,9 +79,15 @@ def hog(image, orientations=9, pixels_per_cell=(8, 8), # convert uint image to float # to avoid problems with subtracting unsigned numbers in np.diff() image = image.astype('float') - - gx = convolve1d(image, [-1,0,1], axis=1, mode='nearest') - gy = convolve1d(image, [-1,0,1], axis=0, mode='nearest') + + gx = np.empty_like(image) + gx[:, 0] = 0 + gx[:, -1] = 0 + gx[:, 1:-1] = image[:, 2:] - image[:, :-2] + gy = np.empty_like(image) + gy[0, :] = 0 + gy[-1, :] = 0 + gy[1:-1, :] = image[2:, :] - image[:-2, :] """ The third stage aims to produce an encoding that is sensitive to From 808e53c7ae53ad80e1c8f8ed77b34f1b38b569df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20M=C3=A1rquez=20Neila?= Date: Mon, 18 Aug 2014 02:12:27 +0200 Subject: [PATCH 3/4] Code valid for all image dtypes --- skimage/feature/_hog.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/skimage/feature/_hog.py b/skimage/feature/_hog.py index f9608c9d..2c6dbf0a 100644 --- a/skimage/feature/_hog.py +++ b/skimage/feature/_hog.py @@ -80,11 +80,11 @@ def hog(image, orientations=9, pixels_per_cell=(8, 8), # to avoid problems with subtracting unsigned numbers in np.diff() image = image.astype('float') - gx = np.empty_like(image) + gx = np.empty(image.shape) gx[:, 0] = 0 gx[:, -1] = 0 gx[:, 1:-1] = image[:, 2:] - image[:, :-2] - gy = np.empty_like(image) + gy = np.empty(image.shape) gy[0, :] = 0 gy[-1, :] = 0 gy[1:-1, :] = image[2:, :] - image[:-2, :] From 64e20d817344040ec7861c269d090cb2b3bd9b2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20M=C3=A1rquez=20Neila?= Date: Mon, 18 Aug 2014 02:47:29 +0200 Subject: [PATCH 4/4] Minor fix --- skimage/feature/_hog.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/skimage/feature/_hog.py b/skimage/feature/_hog.py index 2c6dbf0a..72411308 100644 --- a/skimage/feature/_hog.py +++ b/skimage/feature/_hog.py @@ -80,11 +80,11 @@ def hog(image, orientations=9, pixels_per_cell=(8, 8), # to avoid problems with subtracting unsigned numbers in np.diff() image = image.astype('float') - gx = np.empty(image.shape) + gx = np.empty(image.shape, dtype=np.double) gx[:, 0] = 0 gx[:, -1] = 0 gx[:, 1:-1] = image[:, 2:] - image[:, :-2] - gy = np.empty(image.shape) + gy = np.empty(image.shape, dtype=np.double) gy[0, :] = 0 gy[-1, :] = 0 gy[1:-1, :] = image[2:, :] - image[:-2, :]