
    9i3                     z    S SK rSSKJr  SSKJr  SS jrS r\R                  " SS	9       SSS
.S jj5       r	g)    N   )_hoghistogram   )utilsc                    US:X  a2  U [         R                  " [         R                  " U 5      5      U-   -  nU$ US:X  aF  [         R                  " U [         R                  " [         R                  " U 5      5      U-   -  5      nU$ US:X  a8  U [         R                  " [         R                  " U S-  5      US-  -   5      -  nU$ US:X  a  U [         R                  " [         R                  " U S-  5      US-  -   5      -  n[         R                  " US5      nU[         R                  " [         R                  " US-  5      US-  -   5      -  nU$ [        S5      e)NL1zL1-sqrtL2r   L2-Hysg?z/Selected block normalization method is invalid.)npsumabssqrtminimum
ValueError)blockmethodepsouts       T/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/skimage/feature/_hog.py_hog_normalize_blockr      s#   ~rvvbffUm,s23 J 
9	ggervvbffUm4s:;< J 
4bggbffUAX.a788 J 
8	bggbffUAX.a788jjc"BGGBFF36NS!V344 J JKK    c                 t   [         R                  " U R                  U R                  S9nSUSSS24'   SUSSS24'   U SS2SS24   U SS2SS24   -
  USS2SS24'   [         R                  " U R                  U R                  S9nSUSS2S4'   SUSS2S4'   U SS2SS24   U SS2SS24   -
  USS2SS24'   X4$ )zCompute 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   )channelg_rowg_cols      r   _hog_channel_gradientr"      s     HHW]]'--8EE!Q$KE"a%LQRU^gcrc1fo5E!B$'NHHW]]'--8EE!Q$KE!R%LQU^ga"fo5E!QrT'N<r   F)multichannel_output)channel_axisc                L	   [         R                  " U 5      n [        R                  " U R                  5      n	U R                  U	SS9n USLn
U
(       a  U R                  S-
  OU R                  nUS:w  a  [        S5      e U(       a  [         R                  " U 5      n  U
(       Ga8  [         R                  " X	S9n[         R                  " X	S9n[         R                  " X	S9n[        U R                  S   5       Hd  n[        U SS2SS2U4   5      u  USS2SS2U4'   USS2SS2U4'   [         R                  " USS2SS2U4   USS2SS2U4   5      USS2SS2U4'   Mf     UR                  SS9n[         R                  " [         R                   " U R                  S	   5      [         R                   " U R                  S   5      S
SS9u  nnUUUU4   nUUUU4   nO[        U 5      u  nn U R                  SS u  nnUu  nnUu  nn[#        UU-  5      n[#        UU-  5      n[         R$                  " UUU4[&        S9nUR                  [&        SS9nUR                  [&        SS9n[(        R*                  " UUUUUUUUUU5
        SnU(       GaS  SSKJn  [1        UU5      S-  S-
  n [         R                   " U5      n![         R2                  U!S-   -  U-  n"U [         R4                  " U"5      -  n#U [         R6                  " U"5      -  n$[         R$                  " UU4U	S9n[        U5       H  n%[        U5       H  n&[9        U!U#U$5       H  u  n'n(n)[;        U%U-  US-  -   U&U-  US-  -   /5      n*UR=                  [#        U*S	   U)-
  5      [#        U*S   U(-   5      [#        U*S	   U)-   5      [#        U*S   U(-
  5      5      u  nnUUU4==   UU%U&U'4   -  ss'   M     M     M      UU-
  S-   n+UU-
  S-   n,U,S	::  d  U+S	::  a  UU-  n-UU-  n.[        SU- SU. S35      e[         R$                  " U+U,UUU4U	S9n/[        U+5       H;  n%[        U,5       H)  n&UU%U%U-   2U&U&U-   2SS24   n0[?        U0US9U/U%U&SS24'   M+     M=      U(       a  U/RA                  5       n/U(       a  U/U4$ U/$ )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.

Raises
------
ValueError
    If the image is too small given the values of pixels_per_cell and
    cells_per_block.

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   )axisr   ijT)indexingsparse)drawg      ?zoThe input image is too small given the values of pixels_per_cell and cells_per_block. It should have at least: z
 rows and z cols.)r   )!r   
atleast_2dr   _supported_float_typer   astypendimr   r   
empty_likeranger   r"   hypotargmaxmeshgridarangeintzerosfloatr   hog_histograms r+   minpisincosziptupleliner   ravel)1imageorientationspixels_per_cellcells_per_block
block_norm	visualizetransform_sqrtfeature_vectorr$   float_dtypemultichannelndim_spatialg_row_by_chg_col_by_chg_magnidx_chidcs_maxrrccr    r!   s_rows_colc_rowc_colb_rowb_coln_cells_rown_cells_colorientation_histogram	hog_imager+   radiusorientations_arrorientation_bin_midpointsdr_arrdc_arrrcodrdccentren_blocks_rown_blocks_colmin_rowmin_colnormalized_blocksr   s1                                                    r   hogro   0   s   h MM% E--ekk:KLL5L1Et+L%15::>uzzLq.
 	
  mmE=mmE=u8EKKN+F &eAq&L&9:Aq&L)Aq&L)#%88Aq&L);q!V|+D$F1a<  , ==a=(IIekk!n%IIekk!n%	
B BH,-BH,-,U3u ;;r?LE5"LE5"LE5eun%Keun%K HH	k<0 LLUL+ELLUL+E   IUE"a'!+99\2$&EE-=-C$D|$S!"&&!:;;"&&!:;;HHeU^;?	{#A;'!$%5vv!FIAr2"AI
$:AIQR
<R#STF!YYF1IN+F1IN+F1IN+F1IN+	FB b"f%)>q!Qw)GG% "G ( $  %'1,L%'1,LqLA-%-%-( iz'&2
 	
 	|UE<@ < |$A)!a%i-QY*IJE)=eJ)WaAg& % !
 -335 )++  r   )gh㈵>)	   )   rq   )   rr   r
   FFT)
numpyr   r:   r   _sharedr   r   r"   channel_as_last_axisro    r   r   <module>rw      s[      "0 6 d! d! 7d!r   