#NEWLINE# @ winitfeature #NEWLINE# def hog( pixels , mode = 'dense' , algorithm = 'dalaltriggs' , num_bins = 9 , cell_size = 8 , block_size = 2 , signed_gradient = True , l2_norm_clip = 0.2 , window_height = 1 , window_width = 1 , window_unit = 'blocks' , window_step_vertical = 1 , window_step_horizontal = 1 , window_step_unit = 'pixels' , padding = True , verbose = False ) : #NEWLINE# #INDENT# '\n    Extracts Histograms of Oriented Gradients (HOG) features from the input\n    image.\n\n    Parameters\n    ----------\n    pixels : :map:`Image` or subclass or ``(C, X, Y, ..., Z)`` `ndarray`\n        Either the image object itself or an array with the pixels. The first\n        dimension is interpreted as channels. This means an N-dimensional image\n        is represented by an N+1 dimensional array.\n    mode : {``dense``, ``sparse``}, optional\n        The ``sparse`` case refers to the traditional usage of HOGs, so\n        predefined parameters values are used.\n\n        The ``sparse`` case of ``dalaltriggs`` algorithm sets\n        ``window_height = window_width = block_size`` and\n        ``window_step_horizontal = window_step_vertical = cell_size``.\n\n        The ``sparse`` case of ``zhuramanan`` algorithm sets\n        ``window_height = window_width = 3 * cell_size`` and\n        ``window_step_horizontal = window_step_vertical = cell_size``.\n\n        In the ``dense`` case, the user can choose values for `window_height`,\n        `window_width`, `window_unit`, `window_step_vertical`,\n        `window_step_horizontal`, `window_step_unit` and `padding` to customize\n        the HOG calculation.\n    window_height : `float`, optional\n        Defines the height of the window. The metric unit is defined by\n        `window_unit`.\n    window_width : `float`, optional\n        Defines the width of the window. The metric unit is defined by\n        `window_unit`.\n    window_unit : {``blocks``, ``pixels``}, optional\n        Defines the metric unit of the `window_height` and `window_width`\n        parameters.\n    window_step_vertical : `float`, optional\n        Defines the vertical step by which the window is moved, thus it\n        controls the features\' density. The metric unit is defined by\n        `window_step_unit`.\n    window_step_horizontal : `float`, optional\n        Defines the horizontal step by which the window is moved, thus it\n        controls the features\' density. The metric unit is defined by\n        `window_step_unit`.\n    window_step_unit : {``pixels``, ``cells``}, optional\n        Defines the metric unit of the `window_step_vertical` and\n        `window_step_horizontal` parameters.\n    padding : `bool`, optional\n        If ``True``, the output image is padded with zeros to match the input\n        image\'s size.\n    algorithm : {``dalaltriggs``, ``zhuramanan``}, optional\n        Specifies the algorithm used to compute HOGs. ``dalaltriggs`` is the\n        implementation of [1] and ``zhuramanan`` is the implementation of [2].\n    cell_size : `float`, optional\n        Defines the cell size in pixels. This value is set to both the width\n        and height of the cell. This option is valid for both algorithms.\n    block_size : `float`, optional\n        Defines the block size in cells. This value is set to both the width\n        and height of the block. This option is valid only for the\n        ``dalaltriggs`` algorithm.\n    num_bins : `float`, optional\n        Defines the number of orientation histogram bins. This option is\n        valid only for the ``dalaltriggs`` algorithm.\n    signed_gradient : `bool`, optional\n        Flag that defines whether we use signed or unsigned gradient angles.\n        This option is valid only for the ``dalaltriggs`` algorithm.\n    l2_norm_clip : `float`, optional\n        Defines the clipping value of the gradients\' L2-norm. This option is\n        valid only for the ``dalaltriggs`` algorithm.\n    verbose : `bool`, optional\n        Flag to print HOG related information.\n\n    Returns\n    -------\n    hog : :map:`Image` or subclass or ``(X, Y, ..., Z, K)`` `ndarray`\n        The HOG features image. It has the same type as the input ``pixels``.\n        The output number of channels in the case of ``dalaltriggs`` is\n        ``K = num_bins * block_size *block_size`` and ``K = 31`` in the case of\n        ``zhuramanan``.\n\n    Raises\n    ------\n    ValueError\n        HOG features mode must be either dense or sparse\n    ValueError\n        Algorithm must be either dalaltriggs or zhuramanan\n    ValueError\n        Number of orientation bins must be > 0\n    ValueError\n        Cell size (in pixels) must be > 0\n    ValueError\n        Block size (in cells) must be > 0\n    ValueError\n        Value for L2-norm clipping must be > 0.0\n    ValueError\n        Window height must be >= block size and <= image height\n    ValueError\n        Window width must be >= block size and <= image width\n    ValueError\n        Window unit must be either pixels or blocks\n    ValueError\n        Horizontal window step must be > 0\n    ValueError\n        Vertical window step must be > 0\n    ValueError\n        Window step unit must be either pixels or cells\n\n    References\n    ----------\n    .. [1] N. Dalal and B. Triggs, "Histograms of oriented gradients for human\n        detection", Proceedings of the IEEE Conference on Computer Vision and\n        Pattern Recognition (CVPR), 2005.\n    .. [2] X. Zhu, D. Ramanan. "Face detection, pose estimation and landmark\n        localization in the wild", Proceedings of the IEEE Conference on\n        Computer Vision and Pattern Recognition (CVPR), 2012.\n    ' #NEWLINE# pixels = np . rollaxis ( pixels , 0 , len ( pixels . shape ) ) #NEWLINE# if ( mode not in [ 'dense' , 'sparse' ] ) : #NEWLINE# #INDENT# raise ValueError ( 'HOG features mode must be either dense or sparse' ) #NEWLINE# #UNINDENT# if ( algorithm not in [ 'dalaltriggs' , 'zhuramanan' ] ) : #NEWLINE# #INDENT# raise ValueError ( 'Algorithm must be either dalaltriggs or zhuramanan' ) #NEWLINE# #UNINDENT# if ( num_bins <= 0 ) : #NEWLINE# #INDENT# raise ValueError ( 'Number of orientation bins must be > 0' ) #NEWLINE# #UNINDENT# if ( cell_size <= 0 ) : #NEWLINE# #INDENT# raise ValueError ( 'Cell size (in pixels) must be > 0' ) #NEWLINE# #UNINDENT# if ( block_size <= 0 ) : #NEWLINE# #INDENT# raise ValueError ( 'Block size (in cells) must be > 0' ) #NEWLINE# #UNINDENT# if ( l2_norm_clip <= 0.0 ) : #NEWLINE# #INDENT# raise ValueError ( 'Value for L2-norm clipping must be > 0.0' ) #NEWLINE# #UNINDENT# if ( mode == 'dense' ) : #NEWLINE# #INDENT# if ( window_unit not in [ 'pixels' , 'blocks' ] ) : #NEWLINE# #INDENT# raise ValueError ( 'Window unit must be either pixels or blocks' ) #NEWLINE# #UNINDENT# window_height_temp = window_height #NEWLINE# window_width_temp = window_width #NEWLINE# if ( window_unit == 'blocks' ) : #NEWLINE# #INDENT# window_height_temp = ( ( window_height * block_size ) * cell_size ) #NEWLINE# window_width_temp = ( ( window_width * block_size ) * cell_size ) #NEWLINE# #UNINDENT# if ( ( window_height_temp < ( block_size * cell_size ) ) or ( window_height_temp > pixels . shape [ 0 ] ) ) : #NEWLINE# #INDENT# raise ValueError ( 'Window height must be >= block size and <= image height' ) #NEWLINE# #UNINDENT# if ( ( window_width_temp < ( block_size * cell_size ) ) or ( window_width_temp > pixels . shape [ 1 ] ) ) : #NEWLINE# #INDENT# raise ValueError ( 'Window width must be >= block size and <= image width' ) #NEWLINE# #UNINDENT# if ( window_step_horizontal <= 0 ) : #NEWLINE# #INDENT# raise ValueError ( 'Horizontal window step must be > 0' ) #NEWLINE# #UNINDENT# if ( window_step_vertical <= 0 ) : #NEWLINE# #INDENT# raise ValueError ( 'Vertical window step must be > 0' ) #NEWLINE# #UNINDENT# if ( window_step_unit not in [ 'pixels' , 'cells' ] ) : #NEWLINE# #INDENT# raise ValueError ( 'Window step unit must be either pixels or cells' ) #NEWLINE# #UNINDENT# #UNINDENT# pixels = np . asfortranarray ( pixels ) #NEWLINE# pixels *= 255.0 #NEWLINE# if ( mode == 'dense' ) : #NEWLINE# #INDENT# if ( algorithm == 'dalaltriggs' ) : #NEWLINE# #INDENT# algorithm = 1 #NEWLINE# if ( window_unit == 'blocks' ) : #NEWLINE# #INDENT# block_in_pixels = ( cell_size * block_size ) #NEWLINE# window_height = np . uint32 ( ( window_height * block_in_pixels ) ) #NEWLINE# window_width = np . uint32 ( ( window_width * block_in_pixels ) ) #NEWLINE# #UNINDENT# if ( window_step_unit == 'cells' ) : #NEWLINE# #INDENT# window_step_vertical = np . uint32 ( ( window_step_vertical * cell_size ) ) #NEWLINE# window_step_horizontal = np . uint32 ( ( window_step_horizontal * cell_size ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( algorithm == 'zhuramanan' ) : #NEWLINE# #INDENT# algorithm = 2 #NEWLINE# if ( window_unit == 'blocks' ) : #NEWLINE# #INDENT# block_in_pixels = ( 3 * cell_size ) #NEWLINE# window_height = np . uint32 ( ( window_height * block_in_pixels ) ) #NEWLINE# window_width = np . uint32 ( ( window_width * block_in_pixels ) ) #NEWLINE# #UNINDENT# if ( window_step_unit == 'cells' ) : #NEWLINE# #INDENT# window_step_vertical = np . uint32 ( ( window_step_vertical * cell_size ) ) #NEWLINE# window_step_horizontal = np . uint32 ( ( window_step_horizontal * cell_size ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# iterator = WindowIterator ( pixels , window_height , window_width , window_step_horizontal , window_step_vertical , padding ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( algorithm == 'dalaltriggs' ) : #NEWLINE# #INDENT# algorithm = 1 #NEWLINE# window_size = ( cell_size * block_size ) #NEWLINE# step = cell_size #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# algorithm = 2 #NEWLINE# window_size = ( 3 * cell_size ) #NEWLINE# step = cell_size #NEWLINE# #UNINDENT# iterator = WindowIterator ( pixels , window_size , window_size , step , step , False ) #NEWLINE# #UNINDENT# if verbose : #NEWLINE# #INDENT# print ( iterator ) #NEWLINE# #UNINDENT# hog_descriptor = iterator . HOG ( algorithm , num_bins , cell_size , block_size , signed_gradient , l2_norm_clip , verbose ) #NEWLINE# hog_descriptor = WindowIteratorResult ( np . ascontiguousarray ( np . rollaxis ( hog_descriptor . pixels , ( - 1 ) ) ) , hog_descriptor . centres ) #NEWLINE# return hog_descriptor