o
    :ήc]                     @   sT  d dl Zd dlZd dlmZmZm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 dlmZ d d	lmZ d d
lmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$ d dl%m&Z&m'Z' ej(dd Z)ej*+dej,ej-ej.gdd Z/ej*+dej,ej-ej.gdd Z0dd Z1dd Z2ej*+dddgdd Z3ej*+dej,ej-ej.gdd Z4dd Z5ej*+dej,ej-ej.gd d! Z6d"d# Z7d$d% Z8ej*+dej,ej-ej.gd&d' Z9ej*+dej,ej-ej.gd(d) Z:e d*d+ Z;ej*+dej,ej-ej.gd,d- Z<d.d/ Z=e d0d1 Z>ej*+dej,ej-ej.gej*+d2eeeegd3d4 Z?ej*+dej,ej-ej.gd5d6 Z@d7d8 ZAd9d: ZBd;d< ZCej*+dej,ej-ej.gd=d> ZDd?d@ ZEdAdB ZFdCdD ZGdEdF ZHdGdH ZIdIdJ ZJdKdL ZKe dMdN ZLdOdP ZMdQdR ZNe dSdT ZOej*+dej,ej-ej.gdUdV ZPdS )W    N)assert_almost_equalassert_array_equalassert_equal)data)img_as_float)draw)_supported_float_type)expected_warnings)test_parallel)rgb2gray)corner_moraveccorner_harriscorner_shi_tomasicorner_subpixpeak_local_maxcorner_peakscorner_kitchen_rosenfeldcorner_foerstnercorner_fastcorner_orientationsstructure_tensorstructure_tensor_eigvalsstructure_tensor_eigenvalueshessian_matrixhessian_matrix_eigvalshessian_matrix_detshape_index)cubeoctagonc                  C   s2   d} d}t | | | }tj||ddtj}|S )N
   constantmode)r   	ellipsoidnppadastypeuint8)rr%   im3 r*   H/tmp/pip-target-vg8gfxp4/lib/python/skimage/feature/tests/test_corner.pyim3d   s
   r,   dtypec              
      s   t jd| d}d|d< t|ddd\}}}t|  t fdd	|||fD s)J t|t g d
g dg dg dg d
g t|t g d
g dg d
g dg d
g t|t g d
g dg d
g dg d
g d S )N   r/   r-         r3   皙?rcsigmaorderc                 3       | ]}|j  kV  qd S Nr0   .0a	out_dtyper*   r+   	<genexpr>&       z(test_structure_tensor.<locals>.<genexpr>r   r   r   r   r   r   r1   r   r1   r   r      r   rE   r   r   r1   r   r   r   rG   r   r1   r   r   r1   rE   r1   r   )r$   zerosr   r   allr   array)r-   squareArrArcAccr*   r>   r+   test_structure_tensor    s.    rQ   c              
      s   t jd d}d|d< t|dd}t fdd|D sJ tt|d	 t|d
 d d dd d f t g dg dg dg dg dg t|d
 d t g dg dg dg dg dg t|d d t g dg dg dg dg dg d S )Nr/   r/   r/   r0   r1   r3   r3   r3   r4   r7   c                 3   s    | ]
}|j t kV  qd S r:   )r-   r   r;   r0   r*   r+   r@   =   s    z+test_structure_tensor_3d.<locals>.<genexpr>   r   rB   rI   )r   rE      rE   r      r3   )r$   rJ   r   rK   r   lenr   rL   )r-   r   A_elemsr*   r0   r+   test_structure_tensor_3d8   s.   $rZ   c                  C   sj   t d} tt t| ddd W d    n1 sw   Y  t| ddd}t| dd}t|| d S )NrR   r4   xyr6   r5   rT   )r$   rJ   pytestraises
ValueErrorr   r   )r   
A_elems_rcA_elems_noner*   r*   r+    test_structure_tensor_3d_rc_onlyP   s   
ra   c                  C   s   t d} d| d< tdg t| dd}W d    n1 sw   Y  t| ddd}t| dd	d}t|| t||d d d
  d S )Nr.   r1   r2   z"the default order of the structurer4   rT   r[   r6   r5   rG   )r$   rJ   r	   r   r   )rM   A_elems_default
A_elems_xyr_   r*   r*   r+   test_structure_tensor_ordersY   s   

rd   ndimr3   rW   c                 C   s   t d|  }d|dg|  < t|ddd}t|d|  dd}t|dg|  dd}t|| t|| tt t|d| d  dd W d    n1 sMw   Y  tt t|dg| d  dd W d    d S 1 sow   Y  d S )N)r/   r1   r3   r4   r5   r6   )r4   )r$   rJ   r   r   r\   r]   r^   )re   img	A_defaultA_tupleA_listr*   r*   r+   test_structure_tensor_sigmad   s   

"rj   c              
      s   t jd| d}d|d< t|ddd\}}}t|  t fdd	|||fD s)J t|t g d
g d
g dg d
g d
g t|t g d
g dg d
g dg d
g t|t g dg d
g dg d
g dg d S )Nr.   r0   rE   r2   r4   r5   r6   c                 3   r9   r:   r0   r;   r>   r*   r+   r@   y   rA   z&test_hessian_matrix.<locals>.<genexpr>rB   r3   r   r   r3   rF   rH   r   r   r3   r   r   r   r   rl   r   r   )r$   rJ   r   r   rK   r   rL   )r-   rM   HrrHrcHccr*   r>   r+   test_hessian_matrixs   s.    rr   c               
   C   s   t d} d| d< t| ddd}t|dksJ dt| t|d	 d d d	d d f t g d
g dg d
g dg d
g d S )NrR   rE   rS   r4   r5   r6   rU   z.incorrect number of Hessian images (%i) for 3Dr3   rB   rF   rH   )r$   rJ   r   rX   r   rL   )r   Hsr*   r*   r+   test_hessian_matrix_3d   s   
$rt   c              
      s   t jd| d}d|d< t|ddd}t|\}}t|  t fdd	||fD s+J t|t g d
g dg dg dg d
g t|t g d
g d
g d
g d
g d
g d S )Nr.   r0   r1   r2   r4   r5   r6   c                 3   r9   r:   r0   r;   r>   r*   r+   r@      rA   z4test_structure_tensor_eigenvalues.<locals>.<genexpr>rB   )r   r3   rE   r3   r   rD   )r$   rJ   r   r   r   rK   r   rL   )r-   rM   rY   l1l2r*   r>   r+   !test_structure_tensor_eigenvalues   $   rw   c                  C   sz   t jtddddd } t jtddddt jtdddd t}t| dd	}t|\}}}t || d
ks;J d S )N	   r/   r    r!   i     rU   r4   rT   r   )r$   r%   r   r&   boolr   r   rK   )imageboundaryrY   e0e1e2r*   r*   r+   $test_structure_tensor_eigenvalues_3d   s   r   c                  C   sh   t d} d| d< t| ddd}tdg t| }W d    n1 s$w   Y  t|}t|| d S )Nr.   r1   r2   r4   r5   r6   z&structure_tensor_eigvals is deprecated)r$   rJ   r   r	   r   r   r   )rM   rY   eigvalseigenvaluesr*   r*   r+   test_structure_tensor_eigvals   s   

r   c              
      s   t jd| d}d|d< t|ddd}t|\}}t|  t fdd	||fD s+J t|t g d
g dg dg dg d
g t|t g dg dg dg dg dg d S )Nr.   r0   rE   r2   r4   r5   r6   c                 3   r9   r:   r0   r;   r>   r*   r+   r@      rA   z.test_hessian_matrix_eigvals.<locals>.<genexpr>rm   rC   rk   rB   )r   rG   r   rG   r   rn   )r$   rJ   r   r   r   rK   r   rL   )r-   rM   Hru   rv   r*   r>   r+   test_hessian_matrix_eigvals   rx   r   c                    s8  | j |dd} t| }t|}t|tfdd|D s J |\}}}t||kr3t||ks5J |d d |jd d f \ }t jd \ fddtd jd d d D }t fd	d|D }	tfd
d|D }
t	|
t
|	k sJ t|
dk sJ t|	dksJ d S )NFcopyc                 3   r9   r:   r0   r;   r>   r*   r+   r@      rA   z1test_hessian_matrix_eigvals_3d.<locals>.<genexpr>r1   r3   c                        g | ]}t j| jd qS )shaper   circle_perimeterr   )r<   radius)E0
col_center
row_centerr*   r+   
<listcomp>   s
    

z2test_hessian_matrix_eigvals_3d.<locals>.<listcomp>c                       g | ]	}t  | qS r*   r$   meanr<   c)r   r*   r+   r          c                    r   r*   r   r   )E2r*   r+   r      r   r   )r&   r   r   r   rK   r$   r   rL   rangeargminargmaxminmax)r,   r-   r   Er~   r   r   E1circles	response0	response2r*   )r   r   r   r?   r   r+   test_hessian_matrix_eigvals_3d   s"   
  r   c                  C   s.   t d} d| d< t| d}t|ddd d S )Nr.   r1   r2   r/   r   rW   )decimal)r$   rJ   r   r   )r|   detr*   r*   r+   test_hessian_matrix_det   s   

r   c                    s   | j |dd} t| }|jt|ksJ ||jd d   t jd \ fddtd jd d d D }t fdd|D }t|}t	|}||k sXJ || dk s`J || dkshJ d S )	NFr   r   r3   c                    r   r   r   )r<   r(   D0r   r   r*   r+   r     s    z.test_hessian_matrix_det_3d.<locals>.<listcomp>r1   c                    r   r*   r   r   )r   r*   r+   r     r   )
r&   r   r-   r   r   r$   rL   r   r   r   )r,   r-   Dr   responselowesthighestr*   r   r+   test_hessian_matrix_det_3d   s   

r   c                  C   s   t d} d| d< tddg t| dd}W d    n1 s w   Y  t|t t jt jdt jt jgt jd	t jd	t jgdt jd
t jdgt jd	t jd	t jgt jt jdt jt jgg d S )Nr.   rE   r2   zdivide by zero|\A\Zzinvalid value|\A\Zr4   rT   g      r   rG   )r$   rJ   r	   r   r   rL   nan)rM   sr*   r*   r+   test_shape_index  s   
r   c                  C   s   t dt} d| d dd df< t| dk}t |dks!J tt| ddddd	}t|d
ks4J tt| ddddd	}t|d
ksGJ tt	| ddd	}t|d
ksXJ d S )N2   r         ?   r   \   kmethodr   min_distancethreshold_relr1   eps)
r$   rJ   r&   floatr   count_nonzeror   r   rX   r   imresultsr*   r*   r+   test_square_image  s    r   funcc                 C   sD   t jd| d}d|d dd df< t| }||}|j|ks J d S )Nr   r0   r   r   )r$   rJ   r   r-   )r-   r   r   r?   cornersr*   r*   r+   test_corner_dtype7  s
   r   c                    sL   t jd| d}d|d dd df< t|  t fddt|D s$J d S )Nr   r0   r   r   c                 3   r9   r:   r0   )r<   arrr>   r*   r+   r@   N  rA   z.test_corner_foerstner_dtype.<locals>.<genexpr>)r$   rJ   r   rK   r   )r-   r   r*   r>   r+   test_corner_foerstner_dtypeI  s   "r   c                  C   s   t dt} d| d dd df< t jjdd | t jj| jdd  } tt	| dd	d
}|
 s4J tt| dddd	d
}t|dksGJ tt| dddd	d
}t|dksZJ tt| dddd	d
}t|dksmJ d S )Nr   r   r   i  )seed)sizeg?r   r   r   r   r   r1   r   g      ?rT   )r$   rJ   r&   r   randomr   uniformr   r   r   anyr   rX   r   r   r*   r*   r+   test_noisy_square_imageQ  s(   r   c                  C   s   t d} d| ddddf< t| } tt| ddd}|t ddggk s*J tt| ddd}|t ddggk sAJ d S )	Nr   r1   rE      r   r   r   rU   )r$   rJ   r   r   r   rL   rK   r   r   r*   r*   r+   test_squared_dotk  s   
 r   c                  C   sJ  t t jdd} | j}tt| }tt|}t|d t|d k	 s-J t|d t|d k	 s?J tt
| }tt
|}t|d t|d k	 s_J t|d t|d k	 sqJ tt| }tt|}t|d t|d k	 sJ t|d t|d k	 sJ dS )z^
    The harris filter should yield the same results with an image and it's
    rotation.
    r3   )axisr   r1   N)r   r   	astronautr   Tr$   nonzeror   sortrK   r   r   )r   
im_rotatedr   results_rotatedr*   r*   r+   test_rotated_img}  s   $$$$$(r   c                 C   sx   t jd| d}d|d dd df< d|dd dd f< tt|dddd}t||}|jt| ks3J t|d d	 d S )
Nr   r0      r   r   r   r1   r   r   	num_peaks)     8@r   )r$   rJ   r   r   r   r-   r   r   )r-   rf   cornersubpixr*   r*   r+   test_subpix_edge  s   
r   c                  C   sB   t d} d| d< tt| dddd}t| |}t|d d d S )Nr   r   r   r   r   r   r1   r   )r$   rJ   r   r   r   r   )rf   r   r   r*   r*   r+   test_subpix_dot  s   

r   c                  C   s   t d} t| t ddgg}t|d t jt jf d| d< tt| dddd}t| t ddgg}t|d t jt jf d S )	Nr   r   r   g|=r   r   r1   r   )r$   rJ   r   rL   r   r   r   r   )rf   r   r   r*   r*   r+   test_subpix_no_class  s   
r   c               
   C   s   t d} d| ddddf< d| ddddf< tt| dd}t| |dd	}t d
d
gddgddgddgddgddgddgg}t|| d S )Nr   r   r1   r   rG   r   )r      )window_sizer   gkx/?g}:z8@gx98@g>Cc=H@)r$   rJ   r   r   r   rL   r   )rf   r   r   refr*   r*   r+   test_subpix_border  s   

r   c                  C   sR   t tt } tdD ]}tjdd}t| dd|d}|j	d |ks&J qdS )zFor a bunch of different values of num_peaks, check that
    peak_local_max returns exactly the right amount of peaks. Test
    is run on the astronaut image in order to produce a sufficient number of
    corners.
       r1      r   r   r   N)
r   r   r   r   r   r$   r   randintr   r   )img_cornersinr   r*   r*   r+   test_num_peaks  s   r   c                  C   s   t d} d| ddddf< d| ddddf< t| dddd	}|jd
ks(J t| dddd	}|jdks7J t| ddd}|jdksEJ t| dddd}t |dksVJ d S )N)r   r   r1   r3   r/   r   r   r   F)exclude_borderr   r   )r1   r3   r2   )r   r   )r/   r3   )r   r   indices)r$   rJ   r   r   sum)r   r   r*   r*   r+   test_corner_peaks  s"   
r   c                  C   sB   t ttttg} td}| D ]}||}tt|sJ qdS )zSome of the corner detectors had a weakness in terms of returning
    NaN when presented with regions of constant intensity. This should
    be fixed by now. We test whether each detector returns something
    finite in the case of constant inputr   r   N)	r   r   r   r   r   r$   rJ   rK   isfinite)	detectorsconstant_imager   r   r*   r*   r+   test_blank_image_nans  s   
r   c                  C   sB   t d} tt t|  W d    d S 1 sw   Y  d S )Nr   r   rW   )r$   rJ   r\   r]   r^   r   rf   r*   r*   r+   (test_corner_fast_image_unsupported_error  s   

"r   c                  C   sb  t t } tg ddgddgddgddgdd	gd
dgddgddgddgddgddgddgddgddgddgddgd dgd!d"gd#d$gd%d&gd'd(gd)d*gd+d,gd-d.gd/d0gd1d2gd2d3gd4d0gd5d6gdd7gd8d9gd:d;gd.d<gd=d>gd?d@gdAdBgdCdDg}tt| dEdFdGdHdI}t|| d S )JNi  i6  iv        i     i     i  i
  ij  iH  i     ia     iZ  i  i           i        u   i              i     i     e      ik     il     i,     iE           i        iS     q      i{     if  i3     iq     i     i1  9      iw     g333333?r   r   r   )r   r   r   r$   rL   r   r   r   )rf   expectedactualr*   r*   r+   test_corner_fast_astronaut  s   	
 !"#$%r"  c                  C   X   t d} tt t| t ddggt d W d    d S 1 s%w   Y  d S )Nr   rz   )rW   rW   r$   rJ   r\   r]   r^   r   asarrayonesr   r*   r*   r+   0test_corner_orientations_image_unsupported_error1     
"r'  c                  C   r#  )Nr   rz   )rE   rE   r$  r   r*   r*   r+   )test_corner_orientations_even_shape_error9  r(  r)  c                  C   sR   t t } tt| dddddd}tg d}t| |tdd	}t	|| d S )
Nr   gffffff?r   r   r4   )r   threshold_absr   )#g42ܿg5rgSױ$@g8g8XYY?g&䤬MgÑ+g׌UE?gLPxgï?gve ?g/E^@g?g쮧`Agi gFv#?gf-m@gB @g)??gYFM@g>9_ֿgwc|@g+1'?gU/g)_ |ȿgܫ@g7tgs?gt '(g&߮g{ܖL>ӿgi1g3	g%;,gJx4HrW   r3   )
r   r   r   r   r   r$   rL   r   r   r   )rf   r   r   r!  r*   r*   r+   "test_corner_orientations_astronautA  s   r+  c                 C   s   t jd| d}d|ddddf< tt|dddd}t||tdd}|jt| ks-J t |}t 	g d	}t
|| d S )
N)r  r  r0   r1   rW   ry   r   r   r3   )-      iiy)r$   rJ   r   r   r   r   r-   r   rad2degrL   r   )r-   rM   r   actual_orientationsactual_orientations_degreesexpected_orientations_degreer*   r*   r+   test_corner_orientations_square]  s   

r2  )Qnumpyr$   r\   numpy.testingr   r   r   skimager   r   r   skimage._shared.utilsr   skimage._shared._warningsr	   skimage._shared.testingr
   skimage.colorr   skimage.featurer   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   skimage.morphologyr   r   fixturer,   markparametrizefloat16float32float64rQ   rZ   ra   rd   rj   rr   rt   rw   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r"  r'  r)  r+  r2  r*   r*   r*   r+   <module>   s    L


	









	

	
,
