
    1-Phy2                         d dl mZ d dlmZ d dlZddlmZ ddlmZ ddl	m
Z
 dd	lmZ dd
lmZ dZd ZddZ	 ddZ	 	 	 	 	 ddZdS )    )chain)addN   haar_like_feature_coord_wrapperhaar_like_feature_wrapper   )gray2rgb)	rectangle)img_as_float)ztype-2-xztype-2-yztype-3-xztype-3-yztype-4c                     | t           }nDt          | t                    r| g}n| }|D ]&}|t           vrt          d| dt            d          '|S )z?Transform feature type to an iterable and check that it exists.Nz'The given feature type is unknown. Got z instead of one of .)FEATURE_TYPE
isinstancestr
ValueError)feature_typefeature_type_feat_ts      T/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/skimage/feature/haar.py_validate_feature_typer      s    $lC(( 	))NMM(M# 	 	F\)) *f * *&* * *   *
     c                      t          |          }t           fd|D              \  }}t          j        |          t          j        |          fS )aT  Compute the coordinates of Haar-like features.

    Parameters
    ----------
    width : int
        Width of the detection window.
    height : int
        Height of the detection window.
    feature_type : str or list of str or None, optional
        The type of feature to consider:

        - 'type-2-x': 2 rectangles varying along the x axis;
        - 'type-2-y': 2 rectangles varying along the y axis;
        - 'type-3-x': 3 rectangles varying along the x axis;
        - 'type-3-y': 3 rectangles varying along the y axis;
        - 'type-4': 4 rectangles varying along x and y axis.

        By default all features are extracted.

    Returns
    -------
    feature_coord : (n_features, n_rectangles, 2, 2), ndarray of list of tuple coord
        Coordinates of the rectangles for each feature.
    feature_type : (n_features,), ndarray of str
        The corresponding type for each feature.

    Examples
    --------
    >>> import numpy as np
    >>> from skimage.transform import integral_image
    >>> from skimage.feature import haar_like_feature_coord
    >>> feat_coord, feat_type = haar_like_feature_coord(2, 2, 'type-4')
    >>> feat_coord # doctest: +SKIP
    array([ list([[(0, 0), (0, 0)], [(0, 1), (0, 1)],
                  [(1, 1), (1, 1)], [(1, 0), (1, 0)]])], dtype=object)
    >>> feat_type
    array(['type-4'], dtype=object)

    c                 2    g | ]}t          |          S  r   ).0r   heightwidths     r   
<listcomp>z+haar_like_feature_coord.<locals>.<listcomp>M   s5     

 

 

 ,E66BB

 

 

r   )r   zipnpconcatenatehstack)r   r   r   r   
feat_coord	feat_types   ``    r   haar_like_feature_coordr'   !   so    R +<88M

 

 

 

 

'

 

 

J	 >*%%ry';';;;r   c                     St                    }t          j        t          t	          j         fd|D                                           S j        d         j        d         k    rt          d          fdt          D             }t           fdt          |t                    D              \  }	}
t          j
        |	          }	t          j
        |
          }
|
                                |
|	<   |
S )a  Compute the Haar-like features for a region of interest (ROI) of an
    integral image.

    Haar-like features have been successfully used for image classification and
    object detection [1]_. It has been used for real-time face detection
    algorithm proposed in [2]_.

    Parameters
    ----------
    int_image : (M, N) ndarray
        Integral image for which the features need to be computed.
    r : int
        Row-coordinate of top left corner of the detection window.
    c : int
        Column-coordinate of top left corner of the detection window.
    width : int
        Width of the detection window.
    height : int
        Height of the detection window.
    feature_type : str or list of str or None, optional
        The type of feature to consider:

        - 'type-2-x': 2 rectangles varying along the x axis;
        - 'type-2-y': 2 rectangles varying along the y axis;
        - 'type-3-x': 3 rectangles varying along the x axis;
        - 'type-3-y': 3 rectangles varying along the y axis;
        - 'type-4': 4 rectangles varying along x and y axis.

        By default all features are extracted.

        If using with `feature_coord`, it should correspond to the feature
        type of each associated coordinate feature.
    feature_coord : ndarray of list of tuples or None, optional
        The array of coordinates to be extracted. This is useful when you want
        to recompute only a subset of features. In this case `feature_type`
        needs to be an array containing the type of each feature, as returned
        by :func:`haar_like_feature_coord`. By default, all coordinates are
        computed.

    Returns
    -------
    haar_features : (n_features,) ndarray of int or float
        Resulting Haar-like features. Each value is equal to the subtraction of
        sums of the positive and negative rectangles. The data type depends of
        the data type of `int_image`: `int` when the data type of `int_image`
        is `uint` or `int` and `float` when the data type of `int_image` is
        `float`.

    Notes
    -----
    When extracting those features in parallel, be aware that the choice of the
    backend (i.e. multiprocessing vs threading) will have an impact on the
    performance. The rule of thumb is as follows: use multiprocessing when
    extracting features for all possible ROI in an image; use threading when
    extracting the feature at specific location for a limited number of ROIs.
    Refer to the example
    :ref:`sphx_glr_auto_examples_applications_plot_haar_extraction_selection_classification.py`
    for more insights.

    Examples
    --------
    >>> import numpy as np
    >>> from skimage.transform import integral_image
    >>> from skimage.feature import haar_like_feature
    >>> img = np.ones((5, 5), dtype=np.uint8)
    >>> img_ii = integral_image(img)
    >>> feature = haar_like_feature(img_ii, 0, 0, 5, 5, 'type-3-x')
    >>> feature
    array([-1, -2, -3, -4, -5, -1, -2, -3, -4, -5, -1, -2, -3, -4, -5, -1, -2,
           -3, -4, -1, -2, -3, -4, -1, -2, -3, -4, -1, -2, -3, -1, -2, -3, -1,
           -2, -3, -1, -2, -1, -2, -1, -2, -1, -1, -1])

    You can compute the feature for some pre-computed coordinates.

    >>> from skimage.feature import haar_like_feature_coord
    >>> feature_coord, feature_type = zip(
    ...     *[haar_like_feature_coord(5, 5, feat_t)
    ...       for feat_t in ('type-2-x', 'type-3-x')])
    >>> # only select one feature over two
    >>> feature_coord = np.concatenate([x[::2] for x in feature_coord])
    >>> feature_type = np.concatenate([x[::2] for x in feature_type])
    >>> feature = haar_like_feature(img_ii, 0, 0, 5, 5,
    ...                             feature_type=feature_type,
    ...                             feature_coord=feature_coord)
    >>> feature
    array([ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
            0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
            0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, -1, -3, -5, -2, -4, -1,
           -3, -5, -2, -4, -2, -4, -2, -4, -2, -1, -3, -2, -1, -1, -1, -1, -1])

    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/Haar-like_feature
    .. [2] Oren, M., Papageorgiou, C., Sinha, P., Osuna, E., & Poggio, T.
           (1997, June). Pedestrian detection using wavelet templates.
           In Computer Vision and Pattern Recognition, 1997. Proceedings.,
           1997 IEEE Computer Society Conference on (pp. 193-199). IEEE.
           http://tinyurl.com/y6ulxfta
           :DOI:`10.1109/CVPR.1997.609319`
    .. [3] Viola, Paul, and Michael J. Jones. "Robust real-time face
           detection." International journal of computer vision 57.2
           (2004): 137-154.
           https://www.merl.com/publications/docs/TR2004-043.pdf
           :DOI:`10.1109/CVPR.2001.990517`

    Nc           
   3   B   K   | ]}t          |          V  d S Nr   )r   r   cfeature_coordr   	int_imagerr   s     r   	<genexpr>z$haar_like_feature.<locals>.<genexpr>   sR       $ $  .!1a $ $ $ $ $ $r   r   z?Inconsistent size between feature coordinatesand feature types.c                     g | ]}|k    	S r   r   )r   r   r   s     r   r    z%haar_like_feature.<locals>.<listcomp>   s    JJJ6.JJJr   c                     g | ]H\  }}t          j        |          t          j        |          t          ||                   fIS r   )r"   count_nonzeroflatnonzeror	   )	r   maskr   r+   r,   r   r-   r.   r   s	      r   r    z%haar_like_feature.<locals>.<listcomp>   sp     	 	 	 !D&#D))	N4((-!1ad@S 	 	 	r   )r   r"   r$   listr   from_iterableshaper   r   r!   r#   copy)r-   r.   r+   r   r   r   r,   r   mask_featurehaar_feature_idxhaar_features   ```````    r   haar_like_featurer<   V   sy   Z .|<<y# $ $ $ $ $ $ $ $ $ #0	$ $ $   	
 	
 		
 q!\%7%:::T   KJJJ\JJJ),	 	 	 	 	 	 	 	 	 %(l$C$C	 	 	*
&, >*:;;~l33)5):):)<)<%&r         ?        r?   r?   r>   r?         ?c           
      
   t           j                            |
          }
t          j        |t           j                  }t          j        |t           j                  }|	|}n|
                    ||	d          }t          j        |           }t          | j                  dk     rt          |           }t          |          }|D ]}t          |          D ]\  }}|\  }}t          t          t          |||g                    }t          t          t          |||g                    }t          ||          \  }}|dz   dz  dk    rd|z
  |||f         z  ||z  z   }nd|z
  |||f         z  ||z  z   }||||f<   |S )	a+
  Visualization of Haar-like features.

    Parameters
    ----------
    image : (M, N) ndarray
        The region of an integral image for which the features need to be
        computed.
    r : int
        Row-coordinate of top left corner of the detection window.
    c : int
        Column-coordinate of top left corner of the detection window.
    width : int
        Width of the detection window.
    height : int
        Height of the detection window.
    feature_coord : ndarray of list of tuples or None, optional
        The array of coordinates to be extracted. This is useful when you want
        to recompute only a subset of features. In this case `feature_type`
        needs to be an array containing the type of each feature, as returned
        by :func:`haar_like_feature_coord`. By default, all coordinates are
        computed.
    color_positive_block : tuple of 3 floats
        Floats specifying the color for the positive block. Corresponding
        values define (R, G, B) values. Default value is red (1, 0, 0).
    color_negative_block : tuple of 3 floats
        Floats specifying the color for the negative block Corresponding values
        define (R, G, B) values. Default value is blue (0, 1, 0).
    alpha : float
        Value in the range [0, 1] that specifies opacity of visualization. 1 -
        fully transparent, 0 - opaque.
    max_n_features : int, default=None
        The maximum number of features to be returned.
        By default, all features are returned.
    rng : {`numpy.random.Generator`, int}, optional
        Pseudo-random number generator.
        By default, a PCG64 generator is used (see :func:`numpy.random.default_rng`).
        If `rng` is an int, it is used to seed the generator.

        The rng is used when generating a set of features smaller than
        the total number of available features.

    Returns
    -------
    features : (M, N), ndarray
        An image in which the different features will be added.

    Examples
    --------
    >>> import numpy as np
    >>> from skimage.feature import haar_like_feature_coord
    >>> from skimage.feature import draw_haar_like_feature
    >>> feature_coord, _ = haar_like_feature_coord(2, 2, 'type-4')
    >>> image = draw_haar_like_feature(np.zeros((2, 2)),
    ...                                0, 0, 2, 2,
    ...                                feature_coord,
    ...                                max_n_features=1)
    >>> image
    array([[[0. , 0.5, 0. ],
            [0.5, 0. , 0. ]],
    <BLANKLINE>
           [[0.5, 0. , 0. ],
            [0. , 0.5, 0. ]]])

    )dtypeNF)sizereplace   r   r
   r   )r"   randomdefault_rngasarrayfloat64choicer8   lenr7   r   r   	enumeratetuplemapr   r   )imager.   r+   r   r   r,   color_positive_blockcolor_negative_blockalphamax_n_featuresrngfeature_coord_outputcoordidx_rectrectcoord_start	coord_endrrcc	new_values                        r   draw_haar_like_featurer`      s   Z )


$
$C:&:"*MMM:&:"*MMM&MPUVVWU^^F
5;!%&!!F ' ''.. 
	' 
	'NHd%)"KCq!f = =>>Kc#y1a&99::I{I66FBA"q((Y&R.85CW;WW		Y&R.85CW;WW	&F2r6NN
	' Mr   r*   )NN)r=   r@   rA   NN)	itertoolsr   operatorr   numpyr"   _haarr   r	   colorr   drawr   utilr   r   r   r'   r<   r`   r   r   r   <module>rh      s                   2 2 2 2 2 2 , , , , , ,                  I  $2< 2< 2< 2<l FJR R R Rx )(
h h h h h hr   