
    a6d                     b    d dl ZddlmZ ddlmZ ddlmZ ddlm	Z	m
Z
mZ  G d d	e	          ZdS )
    N   )gaussian)check_nD   )_brief_loop)DescriptorExtractor_mask_border_keypoints_prepare_grayscale_input_2Dc                   $    e Zd ZdZ	 	 d	dZd ZdS )
BRIEFar  BRIEF binary descriptor extractor.

    BRIEF (Binary Robust Independent Elementary Features) is an efficient
    feature point descriptor. It is highly discriminative even when using
    relatively few bits and is computed using simple intensity difference
    tests.

    For each keypoint, intensity comparisons are carried out for a specifically
    distributed number N of pixel-pairs resulting in a binary descriptor of
    length N. For binary descriptors the Hamming distance can be used for
    feature matching, which leads to lower computational cost in comparison to
    the L2 norm.

    Parameters
    ----------
    descriptor_size : int, optional
        Size of BRIEF descriptor for each keypoint. Sizes 128, 256 and 512
        recommended by the authors. Default is 256.
    patch_size : int, optional
        Length of the two dimensional square patch sampling region around
        the keypoints. Default is 49.
    mode : {'normal', 'uniform'}, optional
        Probability distribution for sampling location of decision pixel-pairs
        around keypoints.
    sample_seed : {None, int, `numpy.random.Generator`}, optional
        If `sample_seed` is None the `numpy.random.Generator` singleton is
        used.
        If `sample_seed` is an int, a new ``Generator`` instance is used,
        seeded with `sample_seed`.
        If `sample_seed` is already a ``Generator`` instance then that instance
        is used.

        Seed for the random sampling of the decision pixel-pairs. From a square
        window with length `patch_size`, pixel pairs are sampled using the
        `mode` parameter to build the descriptors using intensity comparison.
        The value of `sample_seed` must be the same for the images to be
        matched while building the descriptors.
    sigma : float, optional
        Standard deviation of the Gaussian low-pass filter applied to the image
        to alleviate noise sensitivity, which is strongly recommended to obtain
        discriminative and good descriptors.

    Attributes
    ----------
    descriptors : (Q, `descriptor_size`) array of dtype bool
        2D ndarray of binary descriptors of size `descriptor_size` for Q
        keypoints after filtering out border keypoints with value at an
        index ``(i, j)`` either being ``True`` or ``False`` representing
        the outcome of the intensity comparison for i-th keypoint on j-th
        decision pixel-pair. It is ``Q == np.sum(mask)``.
    mask : (N, ) array of dtype bool
        Mask indicating whether a keypoint has been filtered out
        (``False``) or is described in the `descriptors` array (``True``).

    Examples
    --------
    >>> from skimage.feature import (corner_harris, corner_peaks, BRIEF,
    ...                              match_descriptors)
    >>> import numpy as np
    >>> square1 = np.zeros((8, 8), dtype=np.int32)
    >>> square1[2:6, 2:6] = 1
    >>> square1
    array([[0, 0, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 0, 0],
           [0, 0, 1, 1, 1, 1, 0, 0],
           [0, 0, 1, 1, 1, 1, 0, 0],
           [0, 0, 1, 1, 1, 1, 0, 0],
           [0, 0, 1, 1, 1, 1, 0, 0],
           [0, 0, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 0, 0]], dtype=int32)
    >>> square2 = np.zeros((9, 9), dtype=np.int32)
    >>> square2[2:7, 2:7] = 1
    >>> square2
    array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 0, 0, 0],
           [0, 0, 1, 1, 1, 1, 1, 0, 0],
           [0, 0, 1, 1, 1, 1, 1, 0, 0],
           [0, 0, 1, 1, 1, 1, 1, 0, 0],
           [0, 0, 1, 1, 1, 1, 1, 0, 0],
           [0, 0, 1, 1, 1, 1, 1, 0, 0],
           [0, 0, 0, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=int32)
    >>> keypoints1 = corner_peaks(corner_harris(square1), min_distance=1)
    >>> keypoints2 = corner_peaks(corner_harris(square2), min_distance=1)
    >>> extractor = BRIEF(patch_size=5)
    >>> extractor.extract(square1, keypoints1)
    >>> descriptors1 = extractor.descriptors
    >>> extractor.extract(square2, keypoints2)
    >>> descriptors2 = extractor.descriptors
    >>> matches = match_descriptors(descriptors1, descriptors2)
    >>> matches
    array([[0, 0],
           [1, 1],
           [2, 2],
           [3, 3]])
    >>> keypoints1[matches[:, 0]]
    array([[2, 2],
           [2, 5],
           [5, 2],
           [5, 5]])
    >>> keypoints2[matches[:, 1]]
    array([[2, 2],
           [2, 6],
           [6, 2],
           [6, 6]])

       1   normalr   c                    |                                 }|dvrt          d          || _        || _        || _        || _        |)t          j                                        j	        | _
        n|| _
        d | _        d | _        d S )N)r   uniformz%`mode` must be 'normal' or 'uniform'.)lower
ValueErrordescriptor_size
patch_sizemodesigmanprandomSeedSequenceentropysample_seeddescriptorsmask)selfr   r   r   r   r   s         5lib/python3.11/site-packages/skimage/feature/brief.py__init__zBRIEF.__init__w   s     zz||,,,DEEE.$	
!y5577?D*D			    c                    t          |d           t          j                            | j                  }t          |          }t          j        t          || j        d                    }| j	        }| j
        }| j        dk    r|dz  |                    |dz            z  }t          j        |t          j                  }|||dz  k     ||dz
   dz  k    z           }|d|dz                               |d          }||dz  |d	z                               |d          }nl| j        d
k    ra|                    |dz
   dz  |dz  dz   |dz  df          }t          j        |t          j                  }t          j        |d          \  }}t          j        |          }t          j        |          }t%          |j        ||dz            | _        t          j        || j        ddf         t          j        dd          }t          j        |j        d         |ft.          d          | _        t3          || j                            t          j                  |||           dS )zExtract BRIEF binary descriptors for given keypoints in image.

        Parameters
        ----------
        image : 2D array
            Input image.
        keypoints : (N, 2) array
            Keypoint coordinates as ``(row, col)``.

        r   reflect)r   r   g      @   )dtypeN   r   r   CF)r&   ordercopyr   )r&   r)   )r   r   r   default_rngr   r
   ascontiguousarrayr   r   r   r   r   standard_normalarrayint32reshapeintegerssplitr	   shaper   int64zerosboolr   r   viewuint8)	r   image	keypointsr   	desc_sizer   samplespos1pos2s	            r    extractzBRIEF.extract   s    	&&t'788+E22 $UDJY777
 

 (	_
9  !C'6+A+A)DECF ,G ,G GGhwbh777Gw*/:!(j1n+=+B!BD EG >IM>*229a@@D9q=Q67??	1MMDDY)##oo
Q&71&<'1Q!&;'01}a&8: :G hwbh777G'1--JD$#D))#D)) +5;	+5?< <	 HYty!!!|4BH#&U4 4 4	 8Y_Q%7$C*.c; ; ; 	E4+00::I$	  	  	  	  	 r"   N)r   r   r   r   r   )__name__
__module____qualname____doc__r!   r?    r"   r    r   r   
   sL        j jX 8:56   (8  8  8  8  8 r"   r   )numpyr   _shared.filtersr   _shared.utilsr   brief_cyr   utilr   r	   r
   r   rD   r"   r    <module>rJ      s        & & & & & & $ $ $ $ $ $ ! ! ! ! ! !0 0 0 0 0 0 0 0 0 0y  y  y  y  y  y  y  y  y  y r"   