<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> import numbers <EOL> import numpy as np <EOL> from . . import img_as_float , img_as_uint <EOL> from . . color . adapt_rgb import adapt_rgb , hsv_value <EOL> from . . exposure import rescale_intensity <EOL> from . . _shared . utils import skimage_deprecation , warn <EOL> NR_OF_GREY = <NUM_LIT:2> ** <NUM_LIT> <EOL> @ adapt_rgb ( hsv_value ) <EOL> def equalize_adapthist ( image , ntiles_x = None , ntiles_y = None , clip_limit = <NUM_LIT> , <EOL> nbins = <NUM_LIT> , kernel_size = None ) : <EOL> """<STR_LIT>""" <EOL> image = img_as_uint ( image ) <EOL> image = rescale_intensity ( image , out_range = ( <NUM_LIT:0> , NR_OF_GREY - <NUM_LIT:1> ) ) <EOL> if ntiles_x is not None or ntiles_y is not None : <EOL> warn ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , skimage_deprecation ) <EOL> if kernel_size is None : <EOL> ntiles_x = ntiles_x or <NUM_LIT:8> <EOL> ntiles_y = ntiles_y or <NUM_LIT:8> <EOL> kernel_size = ( np . round ( image . shape [ <NUM_LIT:0> ] / ntiles_y ) , <EOL> np . round ( image . shape [ <NUM_LIT:1> ] / ntiles_x ) ) <EOL> if isinstance ( kernel_size , numbers . Number ) : <EOL> kernel_size = ( kernel_size , kernel_size ) <EOL> kernel_size = [ int ( k ) for k in kernel_size ] <EOL> image = _clahe ( image , kernel_size , clip_limit * nbins , nbins ) <EOL> image = img_as_float ( image ) <EOL> return rescale_intensity ( image ) <EOL> def _clahe ( image , kernel_size , clip_limit , nbins = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> if clip_limit == <NUM_LIT:1.0> : <EOL> return image <EOL> nr = int ( np . ceil ( image . shape [ <NUM_LIT:0> ] / kernel_size [ <NUM_LIT:0> ] ) ) <EOL> nc = int ( np . ceil ( image . shape [ <NUM_LIT:1> ] / kernel_size [ <NUM_LIT:1> ] ) ) <EOL> row_step = int ( np . floor ( image . shape [ <NUM_LIT:0> ] / nr ) ) <EOL> col_step = int ( np . floor ( image . shape [ <NUM_LIT:1> ] / nc ) ) <EOL> bin_size = <NUM_LIT:1> + NR_OF_GREY // nbins <EOL> lut = np . arange ( NR_OF_GREY ) <EOL> lut //= bin_size <EOL> map_array = np . zeros ( ( nr , nc , nbins ) , dtype = int ) <EOL> for r in range ( nr ) : <EOL> for c in range ( nc ) : <EOL> sub_img = image [ r * row_step : ( r + <NUM_LIT:1> ) * row_step , <EOL> c * col_step : ( c + <NUM_LIT:1> ) * col_step ] <EOL> if clip_limit > <NUM_LIT:0.0> : <EOL> clim = int ( clip_limit * sub_img . size / nbins ) <EOL> if clim < <NUM_LIT:1> : <EOL> clim = <NUM_LIT:1> <EOL> else : <EOL> clim = NR_OF_GREY <EOL> hist = lut [ sub_img . ravel ( ) ] <EOL> hist = np . bincount ( hist ) <EOL> hist = np . append ( hist , np . zeros ( nbins - hist . size , dtype = int ) ) <EOL> hist = clip_histogram ( hist , clim ) <EOL> hist = map_histogram ( hist , <NUM_LIT:0> , NR_OF_GREY - <NUM_LIT:1> , sub_img . size ) <EOL> map_array [ r , c ] = hist <EOL> rstart = <NUM_LIT:0> <EOL> for r in range ( nr + <NUM_LIT:1> ) : <EOL> cstart = <NUM_LIT:0> <EOL> if r == <NUM_LIT:0> : <EOL> r_offset = row_step / <NUM_LIT> <EOL> rU = <NUM_LIT:0> <EOL> rB = <NUM_LIT:0> <EOL> elif r == nr : <EOL> r_offset = row_step / <NUM_LIT> <EOL> rU = nr - <NUM_LIT:1> <EOL> rB = rU <EOL> else : <EOL> r_offset = row_step <EOL> rU = r - <NUM_LIT:1> <EOL> rB = rB + <NUM_LIT:1> <EOL> for c in range ( nc + <NUM_LIT:1> ) : <EOL> if c == <NUM_LIT:0> : <EOL> c_offset = col_step / <NUM_LIT> <EOL> cL = <NUM_LIT:0> <EOL> cR = <NUM_LIT:0> <EOL> elif c == nc : <EOL> c_offset = col_step / <NUM_LIT> <EOL> cL = nc - <NUM_LIT:1> <EOL> cR = cL <EOL> else : <EOL> c_offset = col_step <EOL> cL = c - <NUM_LIT:1> <EOL> cR = cL + <NUM_LIT:1> <EOL> mapLU = map_array [ rU , cL ] <EOL> mapRU = map_array [ rU , cR ] <EOL> mapLB = map_array [ rB , cL ] <EOL> mapRB = map_array [ rB , cR ] <EOL> cslice = np . arange ( cstart , cstart + c_offset ) <EOL> rslice = np . arange ( rstart , rstart + r_offset ) <EOL> interpolate ( image , cslice , rslice , <EOL> mapLU , mapRU , mapLB , mapRB , lut ) <EOL> cstart += c_offset <EOL> rstart += r_offset <EOL> return image <EOL> def clip_histogram ( hist , clip_limit ) : <EOL> """<STR_LIT>""" <EOL> excess_mask = hist > clip_limit <EOL> excess = hist [ excess_mask ] <EOL> n_excess = excess . sum ( ) - excess . size * clip_limit <EOL> bin_incr = int ( n_excess / hist . size ) <EOL> upper = clip_limit - bin_incr <EOL> hist [ excess_mask ] = clip_limit <EOL> low_mask = hist < upper <EOL> n_excess -= hist [ low_mask ] . size * bin_incr <EOL> hist [ low_mask ] += bin_incr <EOL> mid_mask = ( hist >= upper ) & ( hist < clip_limit ) <EOL> mid = hist [ mid_mask ] <EOL> n_excess -= mid . size * clip_limit - mid . sum ( ) <EOL> hist [ mid_mask ] = clip_limit <EOL> prev_n_excess = n_excess <EOL> while n_excess > <NUM_LIT:0> : <EOL> index = <NUM_LIT:0> <EOL> while n_excess > <NUM_LIT:0> and index < hist . size : <EOL> under_mask = hist < <NUM_LIT:0> <EOL> step_size = int ( hist [ hist < clip_limit ] . size / n_excess ) <EOL> step_size = max ( step_size , <NUM_LIT:1> ) <EOL> indices = np . arange ( index , hist . size , step_size ) <EOL> under_mask [ indices ] = True <EOL> under_mask = ( under_mask ) & ( hist < clip_limit ) <EOL> hist [ under_mask ] += <NUM_LIT:1> <EOL> n_excess -= under_mask . sum ( ) <EOL> index += <NUM_LIT:1> <EOL> if prev_n_excess == n_excess : <EOL> break <EOL> prev_n_excess = n_excess <EOL> return hist <EOL> def map_histogram ( hist , min_val , max_val , n_pixels ) : <EOL> """<STR_LIT>""" <EOL> out = np . cumsum ( hist ) . astype ( float ) <EOL> scale = ( ( float ) ( max_val - min_val ) ) / n_pixels <EOL> out *= scale <EOL> out += min_val <EOL> out [ out > max_val ] = max_val <EOL> return out . astype ( int ) <EOL> def interpolate ( image , xslice , yslice , <EOL> mapLU , mapRU , mapLB , mapRB , lut ) : <EOL> """<STR_LIT>""" <EOL> norm = xslice . size * yslice . size <EOL> x_coef , y_coef = np . meshgrid ( np . arange ( xslice . size ) , <EOL> np . arange ( yslice . size ) ) <EOL> x_inv_coef , y_inv_coef = x_coef [ : , : : - <NUM_LIT:1> ] + <NUM_LIT:1> , y_coef [ : : - <NUM_LIT:1> ] + <NUM_LIT:1> <EOL> view = image [ int ( yslice [ <NUM_LIT:0> ] ) : int ( yslice [ - <NUM_LIT:1> ] + <NUM_LIT:1> ) , <EOL> int ( xslice [ <NUM_LIT:0> ] ) : int ( xslice [ - <NUM_LIT:1> ] + <NUM_LIT:1> ) ] <EOL> im_slice = lut [ view ] <EOL> new = ( ( y_inv_coef * ( x_inv_coef * mapLU [ im_slice ] <EOL> + x_coef * mapRU [ im_slice ] ) <EOL> + y_coef * ( x_inv_coef * mapLB [ im_slice ] <EOL> + x_coef * mapRB [ im_slice ] ) ) <EOL> / norm ) <EOL> view [ : , : ] = new <EOL> return image </s>
