<s> from __future__ import division <EOL> import numpy as np <EOL> from . . _shared . utils import assert_nD <EOL> from . import _hoghistogram <EOL> import warnings <EOL> def hog ( image , orientations = <NUM_LIT:9> , pixels_per_cell = ( <NUM_LIT:8> , <NUM_LIT:8> ) , <EOL> cells_per_block = ( <NUM_LIT:3> , <NUM_LIT:3> ) , visualise = False , transform_sqrt = False , <EOL> feature_vector = True , normalise = None ) : <EOL> """<STR_LIT>""" <EOL> image = np . atleast_2d ( image ) <EOL> """<STR_LIT>""" <EOL> assert_nD ( image , <NUM_LIT:2> ) <EOL> if normalise is not None : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if transform_sqrt : <EOL> image = np . sqrt ( image ) <EOL> """<STR_LIT>""" <EOL> if image . dtype . kind == '<STR_LIT:u>' : <EOL> image = image . astype ( '<STR_LIT:float>' ) <EOL> gx = np . empty ( image . shape , dtype = np . double ) <EOL> gx [ : , <NUM_LIT:0> ] = <NUM_LIT:0> <EOL> gx [ : , - <NUM_LIT:1> ] = <NUM_LIT:0> <EOL> gx [ : , <NUM_LIT:1> : - <NUM_LIT:1> ] = image [ : , <NUM_LIT:2> : ] - image [ : , : - <NUM_LIT:2> ] <EOL> gy = np . empty ( image . shape , dtype = np . double ) <EOL> gy [ <NUM_LIT:0> , : ] = <NUM_LIT:0> <EOL> gy [ - <NUM_LIT:1> , : ] = <NUM_LIT:0> <EOL> gy [ <NUM_LIT:1> : - <NUM_LIT:1> , : ] = image [ <NUM_LIT:2> : , : ] - image [ : - <NUM_LIT:2> , : ] <EOL> """<STR_LIT>""" <EOL> sy , sx = image . shape <EOL> cx , cy = pixels_per_cell <EOL> bx , by = cells_per_block <EOL> n_cellsx = int ( np . floor ( sx // cx ) ) <EOL> n_cellsy = int ( np . floor ( sy // cy ) ) <EOL> orientation_histogram = np . zeros ( ( n_cellsy , n_cellsx , orientations ) ) <EOL> _hoghistogram . hog_histograms ( gx , gy , cx , cy , sx , sy , n_cellsx , n_cellsy , <EOL> orientations , orientation_histogram ) <EOL> hog_image = None <EOL> if visualise : <EOL> from . . import draw <EOL> radius = min ( cx , cy ) // <NUM_LIT:2> - <NUM_LIT:1> <EOL> orientations_arr = np . arange ( orientations ) <EOL> dx_arr = radius * np . cos ( orientations_arr / orientations * np . pi ) <EOL> dy_arr = radius * np . sin ( orientations_arr / orientations * np . pi ) <EOL> hog_image = np . zeros ( ( sy , sx ) , dtype = float ) <EOL> for x in range ( n_cellsx ) : <EOL> for y in range ( n_cellsy ) : <EOL> for o , dx , dy in zip ( orientations_arr , dx_arr , dy_arr ) : <EOL> centre = tuple ( [ y * cy + cy // <NUM_LIT:2> , x * cx + cx // <NUM_LIT:2> ] ) <EOL> rr , cc = draw . line ( int ( centre [ <NUM_LIT:0> ] - dx ) , <EOL> int ( centre [ <NUM_LIT:1> ] + dy ) , <EOL> int ( centre [ <NUM_LIT:0> ] + dx ) , <EOL> int ( centre [ <NUM_LIT:1> ] - dy ) ) <EOL> hog_image [ rr , cc ] += orientation_histogram [ y , x , o ] <EOL> """<STR_LIT>""" <EOL> n_blocksx = ( n_cellsx - bx ) + <NUM_LIT:1> <EOL> n_blocksy = ( n_cellsy - by ) + <NUM_LIT:1> <EOL> normalised_blocks = np . zeros ( ( n_blocksy , n_blocksx , <EOL> by , bx , orientations ) ) <EOL> for x in range ( n_blocksx ) : <EOL> for y in range ( n_blocksy ) : <EOL> block = orientation_histogram [ y : y + by , x : x + bx , : ] <EOL> eps = <NUM_LIT> <EOL> normalised_blocks [ y , x , : ] = block / np . sqrt ( block . sum ( ) ** <NUM_LIT:2> + eps ) <EOL> """<STR_LIT>""" <EOL> if feature_vector : <EOL> normalised_blocks = normalised_blocks . ravel ( ) <EOL> if visualise : <EOL> return normalised_blocks , hog_image <EOL> else : <EOL> return normalised_blocks </s>
