a
    
d94                     @   s   d dl ZddlmZmZmZmZ ddlmZm	Z	m
Z
mZ ddlmZ ddlmZ ddlmZ ed	Zg d
ZeddD ]>Zeeee  eee d D ]Zdede de f< qqxG dd deeZdS )    N   )FeatureDetectorDescriptorExtractor_mask_border_keypoints_prepare_grayscale_input_2D)corner_fastcorner_orientationscorner_peakscorner_harris)pyramid_gaussian)check_nD   )	_orb_loop)   r   )   r   r   r      r   r      r         
   	            i   r   c                   @   sJ   e Zd ZdZddd	Zd
d Zdd Zdd Zdd Zdd Z	dd Z
dS )ORBa  Oriented FAST and rotated BRIEF feature detector and binary descriptor
    extractor.

    Parameters
    ----------
    n_keypoints : int, optional
        Number of keypoints to be returned. The function will return the best
        `n_keypoints` according to the Harris corner response if more than
        `n_keypoints` are detected. If not, then all the detected keypoints
        are returned.
    fast_n : int, optional
        The `n` parameter in `skimage.feature.corner_fast`. Minimum number of
        consecutive pixels out of 16 pixels on the circle that should all be
        either brighter or darker w.r.t test-pixel. A point c on the circle is
        darker w.r.t test pixel p if ``Ic < Ip - threshold`` and brighter if
        ``Ic > Ip + threshold``. Also stands for the n in ``FAST-n`` corner
        detector.
    fast_threshold : float, optional
        The ``threshold`` parameter in ``feature.corner_fast``. Threshold used
        to decide whether the pixels on the circle are brighter, darker or
        similar w.r.t. the test pixel. Decrease the threshold when more
        corners are desired and vice-versa.
    harris_k : float, optional
        The `k` parameter in `skimage.feature.corner_harris`. Sensitivity
        factor to separate corners from edges, typically in range ``[0, 0.2]``.
        Small values of `k` result in detection of sharp corners.
    downscale : float, optional
        Downscale factor for the image pyramid. Default value 1.2 is chosen so
        that there are more dense scales which enable robust scale invariance
        for a subsequent feature description.
    n_scales : int, optional
        Maximum number of scales from the bottom of the image pyramid to
        extract the features from.

    Attributes
    ----------
    keypoints : (N, 2) array
        Keypoint coordinates as ``(row, col)``.
    scales : (N, ) array
        Corresponding scales.
    orientations : (N, ) array
        Corresponding orientations in radians.
    responses : (N, ) array
        Corresponding Harris corner responses.
    descriptors : (Q, `descriptor_size`) array of dtype bool
        2D array 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)``.

    References
    ----------
    .. [1] Ethan Rublee, Vincent Rabaud, Kurt Konolige and Gary Bradski
          "ORB: An efficient alternative to SIFT and SURF"
          http://www.vision.cs.chubu.ac.jp/CV-R/pdf/Rublee_iccv2011.pdf

    Examples
    --------
    >>> from skimage.feature import ORB, match_descriptors
    >>> img1 = np.zeros((100, 100))
    >>> img2 = np.zeros_like(img1)
    >>> rng = np.random.default_rng(19481137)  # do not copy this value
    >>> square = rng.random((20, 20))
    >>> img1[40:60, 40:60] = square
    >>> img2[53:73, 53:73] = square
    >>> detector_extractor1 = ORB(n_keypoints=5)
    >>> detector_extractor2 = ORB(n_keypoints=5)
    >>> detector_extractor1.detect_and_extract(img1)
    >>> detector_extractor2.detect_and_extract(img2)
    >>> matches = match_descriptors(detector_extractor1.descriptors,
    ...                             detector_extractor2.descriptors)
    >>> matches
    array([[0, 0],
           [1, 1],
           [2, 2],
           [3, 4],
           [4, 3]])
    >>> detector_extractor1.keypoints[matches[:, 0]]
    array([[59. , 59. ],
           [40. , 40. ],
           [57. , 40. ],
           [46. , 58. ],
           [58.8, 58.8]])
    >>> detector_extractor2.keypoints[matches[:, 1]]
    array([[72., 72.],
           [53., 53.],
           [70., 53.],
           [59., 71.],
           [72., 72.]])

    333333?r     r   {Gz?{Gz?c                 C   sF   || _ || _|| _|| _|| _|| _d | _d | _d | _d | _	d | _
d S )N)	downscalen_scalesn_keypointsfast_nfast_thresholdharris_k	keypointsscales	responsesorientationsdescriptors)selfr    r!   r"   r#   r$   r%    r,   \/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/skimage/feature/orb.py__init__u   s    zORB.__init__c                 C   s$   t |}tt|| jd | jd dS )Nr   )Zchannel_axis)r   listr   r!   r    )r+   imager,   r,   r-   _build_pyramid   s    zORB._build_pyramidc           	      C   s   |j }t|| j| j}t|dd}t|dkrVtjd|dtjd|dtjd|dfS t|j	|dd}|| }t
||t}t|d	| jd
}||d d df |d d df f }|||fS )Nr   )Zmin_distancer   )r   r   dtype)r   r   Zdistancek)methodr5   )r3   r   r#   r$   r	   lennpzerosr   shaper   
OFAST_MASKr
   r%   )	r+   octave_imager3   Zfast_responser&   maskr)   Zharris_responser(   r,   r,   r-   _detect_octave   s,    $zORB._detect_octavec                 C   sP  t |d | |}g }g }g }g }tt|D ]}t|| }t|jdk rV q| |\}	}
}|	|	| j
|   |	|
 |	tj|	jd | j
| |jd |	| q0t|}	t|}
t|}t|}|	jd | jk r|	| _|| _|
| _|| _nD| ddd d| j }|	| | _|| | _|
| | _|| | _dS )zDetect oriented FAST keypoints along with the corresponding scale.

        Parameters
        ----------
        image : 2D array
            Input image.

        r   r   r2   N)r   r1   ranger7   r8   ascontiguousarraysqueezendimr>   appendr    fullr:   r3   vstackhstackr"   r&   r'   r)   r(   argsort)r+   r0   pyramidkeypoints_listorientations_listscales_listresponses_listoctaver<   r&   r)   r(   r'   best_indicesr,   r,   r-   detect   sD    	










z
ORB.detectc                 C   sP   t |j|dd}tj|| tjddd}tj|| ddd}t|||}||fS )N   r4   CF)r3   ordercopy)rS   rT   )r   r:   r8   arrayintpr   )r+   r<   r&   r)   r=   r*   r,   r,   r-   _extract_octave   s    zORB._extract_octavec                 C   s   t |d | |}g }g }t|t| j tj}tt|D ]l}	||	k}
t	|
dkrFt
||	 }||
 }|| j|	  }||
 }| |||\}}|| || qFt|t| _t|| _dS )a  Extract rBRIEF binary descriptors for given keypoints in image.

        Note that the keypoints must be extracted using the same `downscale`
        and `n_scales` parameters. Additionally, if you want to extract both
        keypoints and descriptors you should use the faster
        `detect_and_extract`.

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

        r   r   N)r   r1   r8   logr    ZastyperV   r@   r7   sumrA   rW   rD   rF   viewboolr*   rG   Zmask_)r+   r0   r&   r'   r)   rI   descriptors_listZ	mask_listZoctavesrN   Zoctave_maskr<   Zoctave_keypointsZoctave_orientationsr*   r=   r,   r,   r-   extract   s(    


zORB.extractc                 C   s  t |d | |}g }g }g }g }g }tt|D ]}t|| }	t|	jdk r\ q| |	\}
}}t|
dkr|	|
 |	| |	tj
dtd q4| |	|
|\}}|
| | j|  }|	| |	||  |	||  |	| j| tj|jd tjd  |	| q4t|dkr4tdt|}
t|}t|}t|}t|t}|
jd | jk r|
| _|| _|| _|| _|| _nN| ddd d| j }|
| | _|| | _|| | _|| | _|| | _dS )zDetect oriented FAST keypoints and extract rBRIEF descriptors.

        Note that this is faster than first calling `detect` and then
        `extract`.

        Parameters
        ----------
        image : 2D array
            Input image.

        r   r   )r      r2   znORB found no features. Try passing in an image containing greater intensity contrasts between adjacent pixels.Nr?   )r   r1   r@   r7   r8   rA   rB   rC   r>   rD   r9   r[   rW   r    Zonesr:   rV   RuntimeErrorrF   rG   rZ   r"   r&   r'   r)   r(   r*   rH   )r+   r0   rI   rJ   rM   rL   rK   r\   rN   r<   r&   r)   r(   r*   r=   Zscaled_keypointsr'   rO   r,   r,   r-   detect_and_extract  sf    













zORB.detect_and_extractN)r   r   r   r   r   r   )__name__
__module____qualname____doc__r.   r1   r>   rP   rW   r]   r`   r,   r,   r,   r-   r      s   ]   
65r   )numpyr8   Zfeature.utilr   r   r   r   featurer   r   r	   r
   Z	transformr   Z_shared.utilsr   Zorb_cyr   r9   r;   Z
OFAST_UMAXr@   iabsjr   r,   r,   r,   r-   <module>   s   
$