<s> import math <EOL> import numpy as np <EOL> from scipy import ndimage as ndi <EOL> from . . transform import resize <EOL> from . . util import img_as_float <EOL> def _smooth ( image , sigma , mode , cval ) : <EOL> """<STR_LIT>""" <EOL> smoothed = np . empty ( image . shape , dtype = np . double ) <EOL> if image . ndim == <NUM_LIT:3> : <EOL> for dim in range ( image . shape [ <NUM_LIT:2> ] ) : <EOL> ndi . gaussian_filter ( image [ ... , dim ] , sigma , <EOL> output = smoothed [ ... , dim ] , <EOL> mode = mode , cval = cval ) <EOL> else : <EOL> ndi . gaussian_filter ( image , sigma , output = smoothed , <EOL> mode = mode , cval = cval ) <EOL> return smoothed <EOL> def _check_factor ( factor ) : <EOL> if factor <= <NUM_LIT:1> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> def pyramid_reduce ( image , downscale = <NUM_LIT:2> , sigma = None , order = <NUM_LIT:1> , <EOL> mode = '<STR_LIT>' , cval = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> _check_factor ( downscale ) <EOL> image = img_as_float ( image ) <EOL> rows = image . shape [ <NUM_LIT:0> ] <EOL> cols = image . shape [ <NUM_LIT:1> ] <EOL> out_rows = math . ceil ( rows / float ( downscale ) ) <EOL> out_cols = math . ceil ( cols / float ( downscale ) ) <EOL> if sigma is None : <EOL> sigma = <NUM_LIT:2> * downscale / <NUM_LIT> <EOL> smoothed = _smooth ( image , sigma , mode , cval ) <EOL> out = resize ( smoothed , ( out_rows , out_cols ) , order = order , <EOL> mode = mode , cval = cval ) <EOL> return out <EOL> def pyramid_expand ( image , upscale = <NUM_LIT:2> , sigma = None , order = <NUM_LIT:1> , <EOL> mode = '<STR_LIT>' , cval = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> _check_factor ( upscale ) <EOL> image = img_as_float ( image ) <EOL> rows = image . shape [ <NUM_LIT:0> ] <EOL> cols = image . shape [ <NUM_LIT:1> ] <EOL> out_rows = math . ceil ( upscale * rows ) <EOL> out_cols = math . ceil ( upscale * cols ) <EOL> if sigma is None : <EOL> sigma = <NUM_LIT:2> * upscale / <NUM_LIT> <EOL> resized = resize ( image , ( out_rows , out_cols ) , order = order , <EOL> mode = mode , cval = cval ) <EOL> out = _smooth ( resized , sigma , mode , cval ) <EOL> return out <EOL> def pyramid_gaussian ( image , max_layer = - <NUM_LIT:1> , downscale = <NUM_LIT:2> , sigma = None , order = <NUM_LIT:1> , <EOL> mode = '<STR_LIT>' , cval = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> _check_factor ( downscale ) <EOL> image = img_as_float ( image ) <EOL> layer = <NUM_LIT:0> <EOL> rows = image . shape [ <NUM_LIT:0> ] <EOL> cols = image . shape [ <NUM_LIT:1> ] <EOL> prev_layer_image = image <EOL> yield image <EOL> while layer != max_layer : <EOL> layer += <NUM_LIT:1> <EOL> layer_image = pyramid_reduce ( prev_layer_image , downscale , sigma , order , <EOL> mode , cval ) <EOL> prev_rows = rows <EOL> prev_cols = cols <EOL> prev_layer_image = layer_image <EOL> rows = layer_image . shape [ <NUM_LIT:0> ] <EOL> cols = layer_image . shape [ <NUM_LIT:1> ] <EOL> if prev_rows == rows and prev_cols == cols : <EOL> break <EOL> yield layer_image <EOL> def pyramid_laplacian ( image , max_layer = - <NUM_LIT:1> , downscale = <NUM_LIT:2> , sigma = None , order = <NUM_LIT:1> , <EOL> mode = '<STR_LIT>' , cval = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> _check_factor ( downscale ) <EOL> image = img_as_float ( image ) <EOL> if sigma is None : <EOL> sigma = <NUM_LIT:2> * downscale / <NUM_LIT> <EOL> layer = <NUM_LIT:0> <EOL> rows = image . shape [ <NUM_LIT:0> ] <EOL> cols = image . shape [ <NUM_LIT:1> ] <EOL> smoothed_image = _smooth ( image , sigma , mode , cval ) <EOL> yield image - smoothed_image <EOL> while layer != max_layer : <EOL> layer += <NUM_LIT:1> <EOL> out_rows = math . ceil ( rows / float ( downscale ) ) <EOL> out_cols = math . ceil ( cols / float ( downscale ) ) <EOL> resized_image = resize ( smoothed_image , ( out_rows , out_cols ) , <EOL> order = order , mode = mode , cval = cval ) <EOL> smoothed_image = _smooth ( resized_image , sigma , mode , cval ) <EOL> prev_rows = rows <EOL> prev_cols = cols <EOL> rows = resized_image . shape [ <NUM_LIT:0> ] <EOL> cols = resized_image . shape [ <NUM_LIT:1> ] <EOL> if prev_rows == rows and prev_cols == cols : <EOL> break <EOL> yield resized_image - smoothed_image </s>
