Ë
    ÿ[;j,  ã                   óˆ   — d Z ddlZddlZddlZddlmZ ddlmZm	Z	 ddl
mZ ddlmZ d	Z ee	«      dd
„«       Zd„ Zd„ Zd„ Zy)zô
Adapted from "Contrast Limited Adaptive Histogram Equalization" by Karel
Zuiderveld, Graphics Gems IV, Academic Press, 1994.

http://tog.acm.org/resources/GraphicsGems/

Relicensed with permission of the author under the Modified BSD license.
é    Né   )Ú_supported_float_type)Ú	adapt_rgbÚ	hsv_valueé   )Úrescale_intensity)Úimg_as_uinti @  c           	      ó„  — t        | j                  «      }t        | «      } t        j                  t        | dt        dz
  f¬«      «      j                  t        j                  t        «      «      } |€1t        | j                  D �cg c]  }t        |dz  d«      ‘Œ c}«      }nQt        |t        j                  «      r|f| j                  z  }n&t!        |«      | j                  k7  rt#        d|› �«      ‚|D �cg c]  }t%        |«      ‘Œ }}t'        | |||«      } | j                  |d¬«      } t        | «      S c c}w c c}w )aî  Contrast Limited Adaptive Histogram Equalization (CLAHE).

    An algorithm for local contrast enhancement, that uses histograms computed
    over different tile regions of the image. Local details can therefore be
    enhanced even in regions that are darker or lighter than most of the image.

    Parameters
    ----------
    image : (M[, ...][, C]) ndarray
        Input image.
    kernel_size : int or array_like, optional
        Defines the shape of contextual regions used in the algorithm. If
        iterable is passed, it must have the same number of elements as
        ``image.ndim`` (without color channel). If integer, it is broadcasted
        to each `image` dimension. By default, ``kernel_size`` is 1/8 of
        ``image`` height by 1/8 of its width.
    clip_limit : float, optional
        Clipping limit, normalized between 0 and 1 (higher values give more
        contrast).
    nbins : int, optional
        Number of gray bins for histogram ("data range").

    Returns
    -------
    out : (M[, ...][, C]) ndarray
        Equalized image with float64 dtype.

    See Also
    --------
    equalize_hist, rescale_intensity

    Notes
    -----
    * For color images, the following steps are performed:
       - The image is converted to HSV color space
       - The CLAHE algorithm is run on the V (Value) channel
       - The image is converted back to RGB space and returned
    * For RGBA images, the original alpha channel is removed.

    .. versionchanged:: 0.17
        The values returned by this function are slightly shifted upwards
        because of an internal change in rounding behavior.

    References
    ----------
    .. [1] http://tog.acm.org/resources/GraphicsGems/
    .. [2] https://en.wikipedia.org/wiki/CLAHE#CLAHE
    r   r   )Ú	out_rangeé   z"Incorrect value of `kernel_size`: F)Úcopy)r   Údtyper	   ÚnpÚroundr   Ú
NR_OF_GRAYÚastypeÚmin_scalar_typeÚtupleÚshapeÚmaxÚ
isinstanceÚnumbersÚNumberÚndimÚlenÚ
ValueErrorÚintÚ_clahe)ÚimageÚkernel_sizeÚ
clip_limitÚnbinsÚfloat_dtypeÚsÚks          údG:\00. PROJECTS\API\Inventory\templateJSON\kerjaOCR\Lib\site-packages\skimage/exposure/_adapthist.pyÚequalize_adapthistr'      s  € ôf (¨¯©Ó4€KÜ˜Ó€EÜ�H‰HÔ& u¸¼JÈ¹NÐ8KÔLÓM×TÑTÜ
×Ñœ:Ó&ó€Eð ÐÜ°U·[²[ÓA±[°œS  a¡¨�^°[ÑAÓB‰Ü	�K¤§¡Ô	0Ø"�n u§z¡zÑ1‰Ü	ˆ[Ó	˜UŸZ™ZÒ	'ÜÐ=¸k¸]ÐKÓLÐLá#.Ó/¡;˜a”3�q•6 ;€KÐ/ä�5˜+ z°5Ó9€EØ�L‰L˜¨5ˆLÓ1€EÜ˜UÓ#Ð#ùò Bùò 0s   ÂD8Ã8D=c                 óð  — | j                   }| j                  }|D �cg c]  }|dz  ‘Œ	 }}t        || j                  «      D ��cg c]2  \  }}|||z  z
  |z  t	        t        j                  |dz  «      «      z   ‘Œ4 }	}}t        j                  | t        ||	«      D �
�cg c]	  \  }
}|
|g‘Œ c}}
d¬«      } dt        |z  z   }t        j                  t        t        j                  t        «      ¬«      }||z  }||    } t        | j                  |«      D ��cg c]  \  }}t	        ||z  «      dz
  ‘Œ }}}t        j                  ||g«      j                  j                  «       }t        j                  t        j                  d|dz  d«      t        j                  d|dz  d«      g«      j                  «       }t        ||«      D ��cg c]  \  }}t        |dz  |dz  ||z  z   «      ‘Œ }}}| t        |«         j!                  |«      }t        j"                  ||¬«      }|j                  }|j!                  t%        j&                  |«      d	f«      }t%        j&                  |«      }|d
kD  r$t	        t        j(                  ||z  dd«      «      }n|}t        j*                  t
        j,                  d	||¬«      }t        j*                  t.        d	||¬«      }t1        |dt        dz
  |«      }|j!                  |d| dz   «      }t        j                  |t3        |«      D �cg c]  }ddg‘Œ c}ddggz   d¬«      }t        | j                  |«      D ��cg c]  \  }}t	        ||z  «      ‘Œ }}}t        j                  ||g«      j                  j                  «       }t        j                  t        j                  d|dz  d«      t        j                  d|dz  d«      g«      j                  «       }| j!                  |«      }t        j"                  ||¬«      }|j                  }t        j                   |t%        j&                  |«      t%        j&                  |j                  |d «      f«      }t        j4                  t        |ddd	…   D �cg c]  }t        j                  |«      |z  ‘Œ c}«      ddiŽ}|D � cg c]%  } t        j"                  | «      j                  «       ‘Œ' }} t7        |«      D �!� cg c]
  \  }!} d| z
  ‘Œ }"}!} t        j8                  |j                  t
        j:                  ¬«      }#t7        t        j<                  dg|z  Ž «      D ]Ü  \  }$}%|t        t        |%|«      D �&�cg c]  \  }&}t        |&|&|z   «      ‘Œ c}}&«         }'|'j!                  t%        j&                  |«      d	f«      }'t        j>                  |'|d	¬«      }(t        j&                  t7        |%ddd	…   «      D �)�&cg c]  \  })}&|"|g|&   |)   ‘Œ c}&})d«      }*|#|(|*z  jA                  |#j                  «      z  }#ŒÞ |#jA                  |«      }#|#j!                  |«      }#t        j                  t        j                  d|«      t        j                  ||dz  «      g«      j                  j                  «       }+t        j"                  |#|+¬«      }#|#j!                  | j                  «      }#t        t        ||	| j                  «      D �
��cg c]  \  }
}}t        |
||z
  «      ‘Œ c}}}
«      },|#|,   }#|#S c c}w c c}}w c c}}
w c c}}w c c}}w c c}w c c}}w c c}w c c} w c c} }!w c c}}&w c c}&})w c c}}}
w )a.  Contrast Limited Adaptive Histogram Equalization.

    Parameters
    ----------
    image : (M[, ...]) ndarray
        Input image.
    kernel_size : int or N-tuple of int
        Defines the shape of contextual regions used in the algorithm.
    clip_limit : float
        Normalized clipping limit between 0 and 1 (higher values give more
        contrast).
    nbins : int
        Number of gray bins for histogram ("data range").

    Returns
    -------
    out : (M[, ...]) ndarray
        Equalized image.

    The number of "effective" graylevels in the output image is set by `nbins`;
    selecting a small value (e.g. 128) speeds up processing and still produces
    an output image of good quality. A clip limit of 0 or larger than or equal
    to 1 results in standard (non-contrast limited) AHE.
    r   g       @Úreflect)Úmoder   )r   r   )Úaxeséÿÿÿÿg        N)Ú	minlength)r!   )r,   ÚedgeÚindexingÚij©Úaxis)!r   r   Úzipr   r   r   ÚceilÚpadr   Úaranger   ÚarrayÚTÚflattenÚslicer   ÚreshapeÚ	transposeÚmathÚprodÚclipÚapply_along_axisÚbincountÚclip_histogramÚmap_histogramÚrangeÚmeshgridÚ	enumerateÚzerosÚfloat32ÚndindexÚtake_along_axisr   )-r   r    r!   r"   r   r   r%   Úpad_start_per_dimr$   Úpad_end_per_dimÚp_iÚp_fÚbin_sizeÚlutÚns_histÚhist_blocks_shapeÚhist_blocks_axis_orderÚnÚhist_slicesÚhist_blocksÚhist_block_assembled_shapeÚkernel_elementsÚclimÚhistÚ_Ú	map_arrayÚns_procÚblocks_shapeÚblocks_axis_orderÚblocksÚblocks_flattened_shapeÚcoeffsÚcÚdimÚ
inv_coeffsÚresultÚiedger.   ÚeÚ	edge_mapsÚedge_mappedÚdÚedge_coeffsÚblocks_axis_rebuild_orderÚunpad_slicess-                                                r&   r   r   ^   s¤  € ð2 �:‰:€DØ�K‰K€Eñ
 *5Ó5© A˜˜a›¨ÐÐ5ô ˜ U§[¡[Ô1ôá1‰DˆAˆqð 
ˆQ�‰U‰�a‰œ#œbŸg™g a¨#¡gÓ.Ó/Ó/Ø1ð ñ ô
 �F‰FØÜ$'Ð(9¸?Ô$KÔLÑ$K™˜˜Sˆ#ˆsŠÐ$KÒLØô€Eð ”: Ñ&Ñ&€HÜ
�)‰)”J¤b×&8Ñ&8¼Ó&DÔ
E€CØˆHÑ€Cà�‰J€Eô +.¨e¯k©k¸;Ô*GÔHÑ*G¡$ ! QŒs�1�q‘5‹z˜A‹~Ð*G€GÑHÜŸ™ '¨;Ð!7Ó8×:Ñ:×BÑBÓDÐÜŸX™XÜ	�‰�1�d˜Q‘h Ó	"¤B§I¡I¨a°¸±¸1Ó$=Ð>óç�gƒið ô >AÀÈgÔ=VÔWÑ=V±T°Q¸”5˜˜a™  a¡¨!¨a©%¡Õ0Ð=V€KÑWØœ˜kÓ*Ñ+×3Ñ3Ð4EÓF€KÜ—,‘,˜{Ð1GÔH€KØ!,×!2Ñ!2ÐØ×%Ñ%¤t§y¡y°Ó'9¸2Ð&>Ó?€Kô —i‘i Ó,€OØ�CÒÜ”2—7‘7˜:¨Ñ7¸¸DÓAÓB‰ð ˆä×ÑœrŸ{™{¨B°ÀuÔM€DÜ×Ñœ~¨r°4ÀDÔI€DÜ˜˜q¤*¨q¡.°/ÓB€DØ�<‰<Ð2°5°DÐ9¸EÑAÓB€Dô —‘�t¬e°D¬kÓ:©k¨˜q !šf¨kÑ:¸qÀ!¸f¸XÑEÈFÔS€Iô '*¨%¯+©+°{Ô&CÔDÑ&C™d˜a Œs�1�q‘5�zÐ&C€GÑDÜ—8‘8˜W kÐ2Ó3×5Ñ5×=Ñ=Ó?€LÜŸ™Ü	�‰�1�d˜Q‘h Ó	"¤B§I¡I¨a°¸±¸1Ó$=Ð>óç�gƒið ð �]‰]˜<Ó(€FÜ�\‰\˜&Ð'8Ô9€FØ#Ÿ\™\ÐÜ�Z‰Z˜¤§¡¨7Ó!3´T·Y±Y¸v¿|¹|ÈDÈEÐ?RÓ5SÐ TÓU€Fô �[‰[Ü	¨+±d¸°dÒ*;Ó<Ñ*; Q”—	‘	˜!“˜qÓ Ð*;Ñ<Ó	=ðØHLñ€Fñ 28Ó8±¨AŒb�l‰l˜1‹o×%Ñ%Õ'°€FÐ8Ü&/°Ô&7Ô8Ñ&7™F˜C �!�a“%Ð&7€JÑ8ô �X‰X�f—l‘l¬"¯*©*Ô5€FÜ ¤§¡¨q¨c°D©jÐ!:Ö;‰ˆˆtØœeÄÀTÈ7ÔASÔ$TÑAS¹¸¸A¤U¨1¨a°!©e¥_ÐASÒ$TÓUÑVˆ	Ø×%Ñ%¤t§y¡y°Ó'9¸2Ð&>Ó?ˆ	ô ×(Ñ(¨°FÀÔDˆô —g‘gÜ4=¸dÁ4ÀRÀ4¹jÔ4IÔJÑ4I©D¨A¨qˆj˜&Ð! !Ñ$ QÓ'Ð4IÒJÈAó
ˆð 	�; Ñ,×4Ñ4°V·\±\ÓBÑB‰ð <ð �]‰]˜5Ó!€Fð �^‰^Ð2Ó3€FÜ "§¡Ü	�‰�1�dÓ	œRŸY™Y t¨T°A©XÓ6Ð7ó!ç�a�‰‹	ð ô �\‰\˜&Ð'@ÔA€FØ�^‰^˜EŸK™KÓ(€Fô ô  #Ð#4°oÀuÇ{Á{ÔSõ	
áS‘��S˜!ô �#�q˜3‘wÕØSó	
ó€Lð �LÑ!€Fà€MùòY 6ùóùó 	Mùó Iùó
 Xùò( ;ùó Eùò =ùâ8ùÛ8ùó %Uùó Kùô"	
sM   �\-Á7\2Â\8Ä\>Ç"]Ì]

Í]Ò]Ò<*]Ó6]Õ1]%×5]+Ü]1c                 óŒ  — | |kD  }| |   }|j                  «       |j                  |z  z
  }|| |<   || j                  z  }||z
  }| |k  }|| |   j                  |z  z  }| |xx   |z  cc<   t        j                  | |k\  | |k  «      }| |   }	||	j                  «       |	j                  |z  z
  z  }|| |<   |dkD  r‹|}
t	        | j                  «      D ]c  }| |k  }t        dt        j                  |«      |z  «      }||d|…   }| |d|…   |xx   dz  cc<   |t        j                  |«      z  }|dk  sŒc n |
|k(  r	 | S |dkD  rŒ‹| S )aé  Perform clipping of the histogram and redistribution of bins.

    The histogram is clipped and the number of excess pixels is counted.
    Afterwards the excess pixels are equally redistributed across the
    whole histogram (providing the bin count is smaller than the cliplimit).

    Parameters
    ----------
    hist : ndarray
        Histogram array.
    clip_limit : int
        Maximum allowed bin count.

    Returns
    -------
    hist : ndarray
        Clipped histogram.
    r   r   N)ÚsumÚsizer   Úlogical_andrD   r   Úcount_nonzero)rZ   r!   Úexcess_maskÚexcessÚn_excessÚbin_incrÚupperÚlow_maskÚmid_maskÚmidÚprev_n_excessÚindexÚ
under_maskÚ	step_sizes                 r&   rB   rB   ì   sƒ  € ð( ˜Ñ#€KØ�+Ñ€FØ�z‰z‹|˜fŸk™k¨JÑ6Ñ6€HØ"€DˆÑð ˜4Ÿ9™9Ñ$€HØ˜Ñ!€Eà�e‰|€HØ��X‘×#Ñ# hÑ.Ñ.€HØˆƒN�hÑƒNä�~‰~˜d e™m¨T°JÑ->Ó?€HØ
ˆx‰.€CØ�—‘“	˜CŸH™H zÑ1Ñ1Ñ1€HØ€Dˆ�Nà
�QŠ,Ø ˆÜ˜4Ÿ9™9Ö%ˆEØ 
Ñ*ˆJÜ˜Aœr×/Ñ/°
Ó;¸xÑGÓHˆIØ# EÐ$4¨9Ð$4Ñ5ˆJØ�Ð!˜	Ð!Ñ" :Ó.°!Ñ3Ó.Øœ×(Ñ(¨Ó4Ñ4ˆHØ˜1‹}Ùð &ð ˜HÒ$Øà€Kð �Q‹,ð €Kó    c                 óÒ   — t        j                  | d¬«      j                  t        «      }|||z
  |z  z  }||z  }t        j                  |d||¬«       |j                  t
        «      S )aï  Calculate the equalized lookup table (mapping).

    It does so by cumulating the input histogram.
    Histogram bins are assumed to be represented by the last array dimension.

    Parameters
    ----------
    hist : ndarray
        Clipped histogram.
    min_val : int
        Minimum value for mapping.
    max_val : int
        Maximum value for mapping.
    n_pixels : int
        Number of pixels in the region.

    Returns
    -------
    out : ndarray
       Mapped intensity LUT.
    r,   r1   N)Úa_minÚa_maxÚout)r   Úcumsumr   Úfloatr?   r   )rZ   Úmin_valÚmax_valÚn_pixelsr„   s        r&   rC   rC   "  s\   € ô, �)‰)�D˜rÔ
"×
)Ñ
)¬%Ó
0€CØˆG�gÑ Ñ)Ñ)€CØˆ7�N€CÜ‡G�GˆC�t 7°Õ4à�:‰:”c‹?Ðr€   )Ng{®Gáz„?é   )Ú__doc__r=   r   Únumpyr   Ú_shared.utilsr   Úcolor.adapt_rgbr   r   Úexposurer   Úutilr	   r   r'   r   rB   rC   © r€   r&   Ú<module>r’      sT   ðñó Û ã å 1ß 2Ý 'Ý à€
ñ ˆ9ÓòC$ó ðC$òLKò\3ólr€   