From 502377e9cec9788657369c1dd4eb37edd6cc33d3 Mon Sep 17 00:00:00 2001 From: Ivo Flipse Date: Mon, 15 Jun 2015 22:49:20 +0200 Subject: [PATCH 1/6] Update _hoghistogram.pyx Made the function names PEP8 compatible and added another level of indentation. I also tried renaming some things where I considered columns useful, but for example cells per row isn't entirely accurate. Any suggestions? --- skimage/feature/_hoghistogram.pyx | 38 ++++++++++++++++--------------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/skimage/feature/_hoghistogram.pyx b/skimage/feature/_hoghistogram.pyx index d6d9732d..4d3e2dfe 100644 --- a/skimage/feature/_hoghistogram.pyx +++ b/skimage/feature/_hoghistogram.pyx @@ -5,11 +5,13 @@ import numpy as np cimport numpy as cnp -cdef float CellHog(cnp.float64_t[:, :] magnitude, - cnp.float64_t[:, :] orientation, - float ori1, float ori2, - int cx, int cy, int xi, int yi, int sx, int sy): - """CellHog +cdef float cell_hog(cnp.float64_t[:, :] magnitude, + cnp.float64_t[:, :] orientation, + float ori1, float ori2, + int cx, int cy, + int xi, int yi, + int sx, int sy): + """Calculation of the cell's HOG value Parameters ---------- @@ -26,13 +28,13 @@ cdef float CellHog(cnp.float64_t[:, :] magnitude, cy : int Pixels per cell (y). xi : int - Block index (x). + Block column index. yi : int - Block index (y). + Block row index. sx : int - Image size (x). + Number of columns. sy : int - Image size (y). + Number of rows. Returns ------- @@ -55,13 +57,13 @@ cdef float CellHog(cnp.float64_t[:, :] magnitude, return total -def HogHistograms(cnp.float64_t[:, :] gx, - cnp.float64_t[:, :] gy, - int cx, int cy, - int sx, int sy, - int n_cellsx, int n_cellsy, - int visualise, int orientations, - cnp.float64_t[:, :, :] orientation_histogram): +def hog_histograms(cnp.float64_t[:, :] gx, + cnp.float64_t[:, :] gy, + int cx, int cy, + int sx, int sy, + int n_cellsx, int n_cellsy, + int visualise, int orientations, + cnp.float64_t[:, :, :] orientation_histogram): """Extract Histogram of Oriented Gradients (HOG) for a given image. Parameters @@ -75,9 +77,9 @@ def HogHistograms(cnp.float64_t[:, :] gx, cy : int Pixels per cell (y). sx : int - Image size (x). + Number of columns. sy : int - Image size (y). + Number of rows. n_cellsx : int Number of cells (x). n_cellsy : int From c37fc9ec200b767c1dfffb95a9f465239b00ab3d Mon Sep 17 00:00:00 2001 From: Ivo Flipse Date: Mon, 15 Jun 2015 22:51:30 +0200 Subject: [PATCH 2/6] Update _hog.py Made hog_histograms snake case --- skimage/feature/_hog.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skimage/feature/_hog.py b/skimage/feature/_hog.py index 51d0d060..40175066 100644 --- a/skimage/feature/_hog.py +++ b/skimage/feature/_hog.py @@ -116,7 +116,7 @@ def hog(image, orientations=9, pixels_per_cell=(8, 8), # compute orientations integral images orientation_histogram = np.zeros((n_cellsy, n_cellsx, orientations)) - _hoghistogram.HogHistograms(gx, gy, cx, cy, sx, sy, n_cellsx, n_cellsy, + _hoghistogram.hog_histograms(gx, gy, cx, cy, sx, sy, n_cellsx, n_cellsy, visualise, orientations, orientation_histogram) # now for each cell, compute the histogram From 8ab6e63e75852c36772c6f4793277b30528ac3c7 Mon Sep 17 00:00:00 2001 From: Korijn van Golen Date: Tue, 16 Jun 2015 09:36:02 +0200 Subject: [PATCH 3/6] Removed unused import, applied pep8 formatting and corrected a method call that should have been refactored. --- skimage/feature/_hog.py | 6 +++--- skimage/feature/_hoghistogram.pyx | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/skimage/feature/_hog.py b/skimage/feature/_hog.py index 40175066..98ffb32d 100644 --- a/skimage/feature/_hog.py +++ b/skimage/feature/_hog.py @@ -1,8 +1,8 @@ import numpy as np -from scipy.ndimage import uniform_filter from .._shared.utils import assert_nD from . import _hoghistogram + def hog(image, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(3, 3), visualise=False, normalise=False): """Extract Histogram of Oriented Gradients (HOG) for a given image. @@ -116,8 +116,8 @@ def hog(image, orientations=9, pixels_per_cell=(8, 8), # compute orientations integral images orientation_histogram = np.zeros((n_cellsy, n_cellsx, orientations)) - _hoghistogram.hog_histograms(gx, gy, cx, cy, sx, sy, n_cellsx, n_cellsy, - visualise, orientations, orientation_histogram) + _hoghistogram.hog_histograms(gx, gy, cx, cy, sx, sy, n_cellsx, n_cellsy, + visualise, orientations, orientation_histogram) # now for each cell, compute the histogram hog_image = None diff --git a/skimage/feature/_hoghistogram.pyx b/skimage/feature/_hoghistogram.pyx index 4d3e2dfe..76ad2add 100644 --- a/skimage/feature/_hoghistogram.pyx +++ b/skimage/feature/_hoghistogram.pyx @@ -117,7 +117,7 @@ def hog_histograms(cnp.float64_t[:, :] gx, x = cx / 2 while x < cx2: - orientation_histogram[yi, xi, i] = CellHog(magnitude, + orientation_histogram[yi, xi, i] = cell_hog(magnitude, orientation, ori1, ori2, cx, cy, x, y, sx, sy) xi += 1 x += cx From 78cf5a0f9a2af8c5cbf41711a92c85a77cbab582 Mon Sep 17 00:00:00 2001 From: Korijn van Golen Date: Tue, 16 Jun 2015 09:40:04 +0200 Subject: [PATCH 4/6] Removed unused code. --- skimage/feature/_hog.py | 5 +---- skimage/feature/_hoghistogram.pyx | 4 +--- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/skimage/feature/_hog.py b/skimage/feature/_hog.py index 98ffb32d..877117bb 100644 --- a/skimage/feature/_hog.py +++ b/skimage/feature/_hog.py @@ -103,9 +103,6 @@ def hog(image, orientations=9, pixels_per_cell=(8, 8), cell are used to vote into the orientation histogram. """ - magnitude = np.hypot(gx, gy) - orientation = np.arctan2(gy, gx) * (180 / np.pi) % 180 - sy, sx = image.shape cx, cy = pixels_per_cell bx, by = cells_per_block @@ -117,7 +114,7 @@ def hog(image, orientations=9, pixels_per_cell=(8, 8), orientation_histogram = np.zeros((n_cellsy, n_cellsx, orientations)) _hoghistogram.hog_histograms(gx, gy, cx, cy, sx, sy, n_cellsx, n_cellsy, - visualise, orientations, orientation_histogram) + orientations, orientation_histogram) # now for each cell, compute the histogram hog_image = None diff --git a/skimage/feature/_hoghistogram.pyx b/skimage/feature/_hoghistogram.pyx index 76ad2add..cec180f7 100644 --- a/skimage/feature/_hoghistogram.pyx +++ b/skimage/feature/_hoghistogram.pyx @@ -62,7 +62,7 @@ def hog_histograms(cnp.float64_t[:, :] gx, int cx, int cy, int sx, int sy, int n_cellsx, int n_cellsy, - int visualise, int orientations, + int orientations, cnp.float64_t[:, :, :] orientation_histogram): """Extract Histogram of Oriented Gradients (HOG) for a given image. @@ -84,8 +84,6 @@ def hog_histograms(cnp.float64_t[:, :] gx, Number of cells (x). n_cellsy : int Number of cells (y). - visualise : int - Also return an image of the HOG. orientations : int Number of orientation bins. orientation_histogram : ndarray From 605eac2e8cf2e320125a0a50296e52b672a671eb Mon Sep 17 00:00:00 2001 From: Korijn van Golen Date: Tue, 16 Jun 2015 09:54:54 +0200 Subject: [PATCH 5/6] Renamed to use row/column naming convention. --- skimage/feature/_hoghistogram.pyx | 102 +++++++++++++++--------------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/skimage/feature/_hoghistogram.pyx b/skimage/feature/_hoghistogram.pyx index cec180f7..02a6c92e 100644 --- a/skimage/feature/_hoghistogram.pyx +++ b/skimage/feature/_hoghistogram.pyx @@ -7,10 +7,10 @@ cimport numpy as cnp cdef float cell_hog(cnp.float64_t[:, :] magnitude, cnp.float64_t[:, :] orientation, - float ori1, float ori2, - int cx, int cy, - int xi, int yi, - int sx, int sy): + float orientation_start, float orientation_end, + int cell_columns, int cell_rows, + int column_index, int row_index, + int size_columns, int size_rows): """Calculation of the cell's HOG value Parameters @@ -19,21 +19,21 @@ cdef float cell_hog(cnp.float64_t[:, :] magnitude, The gradient magnitudes of the pixels. orientation : ndarray Lookup table for orientations. - ori1 : float + orientation_start : float Orientation range start. - ori2 : float + orientation_end : float Orientation range end. - cx : int + cell_columns : int Pixels per cell (x). - cy : int + cell_rows : int Pixels per cell (y). - xi : int + column_index : int Block column index. - yi : int + row_index : int Block row index. - sx : int + size_columns : int Number of columns. - sy : int + size_rows : int Number of rows. Returns @@ -41,85 +41,85 @@ cdef float cell_hog(cnp.float64_t[:, :] magnitude, total : float The total HOG value. """ - cdef int cx1, cy1 + cdef int cell_column, cell_row cdef float total = 0. - for cy1 in range(-cy/2, cy/2): - for cx1 in range(-cx/2, cx/2): - if (yi + cy1 < 0 - or yi + cy1 >= sy - or xi + cx1 < 0 - or xi + cx1 >= sx - or orientation[yi + cy1, xi + cx1] >= ori1 - or orientation[yi + cy1, xi + cx1] < ori2): continue + for cell_row in range(-cell_rows/2, cell_rows/2): + for cell_column in range(-cell_columns/2, cell_columns/2): + if (row_index + cell_row < 0 + or row_index + cell_row >= size_rows + or column_index + cell_column < 0 + or column_index + cell_column >= size_columns + or orientation[row_index + cell_row, column_index + cell_column] >= orientation_start + or orientation[row_index + cell_row, column_index + cell_column] < orientation_end): continue - total += magnitude[yi + cy1, xi + cx1] + total += magnitude[row_index + cell_row, column_index + cell_column] return total -def hog_histograms(cnp.float64_t[:, :] gx, - cnp.float64_t[:, :] gy, - int cx, int cy, - int sx, int sy, - int n_cellsx, int n_cellsy, - int orientations, +def hog_histograms(cnp.float64_t[:, :] gradient_columns, + cnp.float64_t[:, :] gradient_rows, + int cell_columns, int cell_rows, + int size_columns, int size_rows, + int number_of_cells_columns, int number_of_cells_rows, + int number_of_orientations, cnp.float64_t[:, :, :] orientation_histogram): """Extract Histogram of Oriented Gradients (HOG) for a given image. Parameters ---------- - gx : ndarray + gradient_columns : ndarray First order image gradients (x). - gy : ndarray + gradient_rows : ndarray First order image gradients (y). - cx : int + cell_columns : int Pixels per cell (x). - cy : int + cell_rows : int Pixels per cell (y). - sx : int + size_columns : int Number of columns. - sy : int + size_rows : int Number of rows. - n_cellsx : int + number_of_cells_columns : int Number of cells (x). - n_cellsy : int + number_of_cells_rows : int Number of cells (y). - orientations : int + number_of_orientations : int Number of orientation bins. orientation_histogram : ndarray The histogram to fill. """ - cdef cnp.float64_t[:, :] magnitude = np.hypot(gx, gy) - cdef cnp.float64_t[:, :] orientation = np.arctan2(gy, gx) * (180 / np.pi) % 180 + cdef cnp.float64_t[:, :] magnitude = np.hypot(gradient_columns, gradient_rows) + cdef cnp.float64_t[:, :] orientation = np.arctan2(gradient_rows, gradient_columns) * (180 / np.pi) % 180 cdef int i, x, y, o, yi, xi, cy1, cy2, cx1, cx2 - cdef float ori1, ori2 + cdef float orientation_start, orientation_end # compute orientations integral images - for i in range(orientations): + for i in range(number_of_orientations): # isolate orientations in this range - ori1 = 180. / orientations * (i + 1) - ori2 = 180. / orientations * i + orientation_start = 180. / number_of_orientations * (i + 1) + orientation_end = 180. / number_of_orientations * i - y = cy / 2 - cy2 = cy * n_cellsy - x = cx / 2 - cx2 = cx * n_cellsx + y = cell_rows / 2 + cy2 = cell_rows * number_of_cells_rows + x = cell_columns / 2 + cx2 = cell_columns * number_of_cells_columns yi = 0 xi = 0 while y < cy2: xi = 0 - x = cx / 2 + x = cell_columns / 2 while x < cx2: orientation_histogram[yi, xi, i] = cell_hog(magnitude, - orientation, ori1, ori2, cx, cy, x, y, sx, sy) + orientation, orientation_start, orientation_end, cell_columns, cell_rows, x, y, size_columns, size_rows) xi += 1 - x += cx + x += cell_columns yi += 1 - y += cy + y += cell_rows From 03b7d638a7ac9ba71844be9f008ba1656e192db5 Mon Sep 17 00:00:00 2001 From: Korijn van Golen Date: Tue, 16 Jun 2015 09:57:49 +0200 Subject: [PATCH 6/6] Wrapped long lines according to pep8. --- skimage/feature/_hoghistogram.pyx | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/skimage/feature/_hoghistogram.pyx b/skimage/feature/_hoghistogram.pyx index 02a6c92e..1851b907 100644 --- a/skimage/feature/_hoghistogram.pyx +++ b/skimage/feature/_hoghistogram.pyx @@ -50,8 +50,10 @@ cdef float cell_hog(cnp.float64_t[:, :] magnitude, or row_index + cell_row >= size_rows or column_index + cell_column < 0 or column_index + cell_column >= size_columns - or orientation[row_index + cell_row, column_index + cell_column] >= orientation_start - or orientation[row_index + cell_row, column_index + cell_column] < orientation_end): continue + or orientation[row_index + cell_row, column_index + cell_column] + >= orientation_start + or orientation[row_index + cell_row, column_index + cell_column] + < orientation_end): continue total += magnitude[row_index + cell_row, column_index + cell_column] @@ -90,8 +92,10 @@ def hog_histograms(cnp.float64_t[:, :] gradient_columns, The histogram to fill. """ - cdef cnp.float64_t[:, :] magnitude = np.hypot(gradient_columns, gradient_rows) - cdef cnp.float64_t[:, :] orientation = np.arctan2(gradient_rows, gradient_columns) * (180 / np.pi) % 180 + cdef cnp.float64_t[:, :] magnitude = np.hypot(gradient_columns, + gradient_rows) + cdef cnp.float64_t[:, :] orientation = \ + np.arctan2(gradient_rows, gradient_columns) * (180 / np.pi) % 180 cdef int i, x, y, o, yi, xi, cy1, cy2, cx1, cx2 cdef float orientation_start, orientation_end @@ -116,7 +120,8 @@ def hog_histograms(cnp.float64_t[:, :] gradient_columns, while x < cx2: orientation_histogram[yi, xi, i] = cell_hog(magnitude, - orientation, orientation_start, orientation_end, cell_columns, cell_rows, x, y, size_columns, size_rows) + orientation, orientation_start, orientation_end, + cell_columns, cell_rows, x, y, size_columns, size_rows) xi += 1 x += cell_columns