U
    ?h1                     @   sR   d dl ZddlmZ ddlmZ dddZd	d
 ZejdddddddZ	dS )    N   )_hoghistogram   )utilsh㈵>c                 C   s   |dkr"| t t | |  }n|dkrJt | t t | |  }n|dkrt| t t | d |d   }n^|dkr| t t | d |d   }t |d}|t t |d |d   }ntd|S )NZL1zL1-sqrtZL2r   L2-Hysg?z/Selected block normalization method is invalid.)npsumabssqrtminimum
ValueError)blockmethodepsout r   F/var/www/html/venv/lib/python3.8/site-packages/skimage/feature/_hog.py_hog_normalize_block   s     " "r   c                 C   s   t j| j| jd}d|dddf< d|dddf< | ddddf | ddddf  |ddddf< t j| j| jd}d|dddf< d|dddf< | ddddf | ddddf  |ddddf< ||fS )a  Compute unnormalized gradient image along `row` and `col` axes.

    Parameters
    ----------
    channel : (M, N) ndarray
        Grayscale image or one of image channel.

    Returns
    -------
    g_row, g_col : channel gradient along `row` and `col` axes correspondingly.
    dtyper   Nr   r   )r   emptyshaper   )Zchannelg_rowg_colr   r   r   _hog_channel_gradient   s    88r   F)Zmultichannel_output	      r       r"   r   T)channel_axisc          /      C   s  t | } t| j}	| j|	dd} |dk	}
|
r:| jd n| j}|dkrPtd|r^t | } |
r~t j	| |	d}t j	| |	d}t j	| |	d}t
| jd D ]}t| dddd|f \|dddd|f< |dddd|f< t |dddd|f |dddd|f |dddd|f< q|jdd}t jt | jd	 t | jd d
dd\}}||||f }||||f }nt| \}}| jdd \}}|\}}|\}}t|| }t|| }t j|||ftd}|jtdd}|jtdd}t||||||||||
 d}|rBddlm} t||d d } t |}!t j|!d  | }"| t |" }#| t |" }$t j||f|	d}t
|D ]}%t
|D ]}&t|!|#|$D ]\}'}(})t|%| |d  |&| |d  g}*|t|*d	 |) t|*d |( t|*d	 |) t|*d |( \}}|||f  ||%|&|'f 7  < qqq|| d }+|| d },t j|+|,|||f|	d}-t
|+D ]R}%t
|,D ]B}&||%|%| |&|&| ddf }.t|.|d|-|%|&ddf< qqz|r|-  }-|r|-|fS |-S dS )a  Extract Histogram of Oriented Gradients (HOG) for a given image.

    Compute a Histogram of Oriented Gradients (HOG) by

        1. (optional) global image normalization
        2. computing the gradient image in `row` and `col`
        3. computing gradient histograms
        4. normalizing across blocks
        5. flattening into a feature vector

    Parameters
    ----------
    image : (M, N[, C]) ndarray
        Input image.
    orientations : int, optional
        Number of orientation bins.
    pixels_per_cell : 2-tuple (int, int), optional
        Size (in pixels) of a cell.
    cells_per_block : 2-tuple (int, int), optional
        Number of cells in each block.
    block_norm : str {'L1', 'L1-sqrt', 'L2', 'L2-Hys'}, optional
        Block normalization method:

        ``L1``
           Normalization using L1-norm.
        ``L1-sqrt``
           Normalization using L1-norm, followed by square root.
        ``L2``
           Normalization using L2-norm.
        ``L2-Hys``
           Normalization using L2-norm, followed by limiting the
           maximum values to 0.2 (`Hys` stands for `hysteresis`) and
           renormalization using L2-norm. (default)
           For details, see [3]_, [4]_.

    visualize : bool, optional
        Also return an image of the HOG.  For each cell and orientation bin,
        the image contains a line segment that is centered at the cell center,
        is perpendicular to the midpoint of the range of angles spanned by the
        orientation bin, and has intensity proportional to the corresponding
        histogram value.
    transform_sqrt : bool, optional
        Apply power law compression to normalize the image before
        processing. DO NOT use this if the image contains negative
        values. Also see `notes` section below.
    feature_vector : bool, optional
        Return the data as a feature vector by calling .ravel() on the result
        just before returning.
    channel_axis : int or None, optional
        If None, the image is assumed to be a grayscale (single channel) image.
        Otherwise, this parameter indicates which axis of the array corresponds
        to channels.

        .. versionadded:: 0.19
           `channel_axis` was added in 0.19.

    Returns
    -------
    out : (n_blocks_row, n_blocks_col, n_cells_row, n_cells_col, n_orient) ndarray
        HOG descriptor for the image. If `feature_vector` is True, a 1D
        (flattened) array is returned.
    hog_image : (M, N) ndarray, optional
        A visualisation of the HOG image. Only provided if `visualize` is True.

    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/Histogram_of_oriented_gradients

    .. [2] Dalal, N and Triggs, B, Histograms of Oriented Gradients for
           Human Detection, IEEE Computer Society Conference on Computer
           Vision and Pattern Recognition 2005 San Diego, CA, USA,
           https://lear.inrialpes.fr/people/triggs/pubs/Dalal-cvpr05.pdf,
           :DOI:`10.1109/CVPR.2005.177`

    .. [3] Lowe, D.G., Distinctive image features from scale-invatiant
           keypoints, International Journal of Computer Vision (2004) 60: 91,
           http://www.cs.ubc.ca/~lowe/papers/ijcv04.pdf,
           :DOI:`10.1023/B:VISI.0000029664.99615.94`

    .. [4] Dalal, N, Finding People in Images and Videos,
           Human-Computer Interaction [cs.HC], Institut National Polytechnique
           de Grenoble - INPG, 2006,
           https://tel.archives-ouvertes.fr/tel-00390303/file/NavneetDalalThesis.pdf

    Notes
    -----
    The presented code implements the HOG extraction method from [2]_ with
    the following changes: (I) blocks of (3, 3) cells are used ((2, 2) in the
    paper); (II) no smoothing within cells (Gaussian spatial window with sigma=8pix
    in the paper); (III) L1 block normalization is used (L2-Hys in the paper).

    Power law compression, also known as Gamma correction, is used to reduce
    the effects of shadowing and illumination variations. The compression makes
    the dark regions lighter. When the kwarg `transform_sqrt` is set to
    ``True``, the function computes the square root of each color channel
    and then applies the hog algorithm to the image.
    F)copyNr   r   zwOnly images with two spatial dimensions are supported. If using with color/multichannel images, specify `channel_axis`.r   )Zaxisr   ZijT)Zindexingsparse)drawg      ?)r   )!r   Z
atleast_2dr   Z_supported_float_typer   Zastypendimr   r   Z
empty_likeranger   r   hypotZargmaxZmeshgridZarangeintZzerosfloatr   Zhog_histograms r&   minpisincosziptupleliner   Zravel)/imageZorientationsZpixels_per_cellZcells_per_blockZ
block_normZ	visualizeZtransform_sqrtZfeature_vectorr#   Zfloat_dtypeZmultichannelZndim_spatialZg_row_by_chZg_col_by_chZg_magnZidx_chZidcs_maxZrrccr   r   Zs_rowZs_colZc_rowZc_colZb_rowZb_colZn_cells_rowZn_cells_colZorientation_histogramZ	hog_imager&   ZradiusZorientations_arrZorientation_bin_midpointsZdr_arrZdc_arrrcoZdrZdcZcentreZn_blocks_rowZn_blocks_colZnormalized_blocksr   r   r   r   hog0   s    e

*
  
*"
	r9   )r   )r   r   r!   r   FFT)
numpyr   r,   r   Z_sharedr   r   r   Zchannel_as_last_axisr9   r   r   r   r   <module>   s   

       