(im, cloud_mask, prob_high)
return im_adj
Rescales the intensity of an image (multispectral or single band) by applying
    a cloud mask and clipping the prob_high upper percentile. This functions allows
    to stretch the contrast of an image, only for visualisation purposes.

    KV WRL 2018

    Arguments:
    -----------
    im: np.array
        Image to rescale, can be 3D (multispectral) or 2D (single band)
    cloud_mask: np.array
        2D cloud mask with True where cloud pixels are
    prob_high: float
        probability of exceedence used to calculate the upper percentile

    Returns:
    -----------
    im_adj: np.array
        rescaled image
Rescales the intensity of an image (multispectral or single band) by applying
    a cloud mask and clipping the prob_high upper percentile. This functions allows
    to stretch the contrast of an image, only for visualisation purposes.
Rescales the intensity of an image ( multispectral or single band ) by applying a cloud mask and clipping the prob_high upper percentile . This functions allows to stretch the contrast of an image only for visualisation purposes .
def rescale_image_intensity(im, cloud_mask, prob_high):
    """
    Rescales the intensity of an image (multispectral or single band) by applying
    a cloud mask and clipping the prob_high upper percentile. This functions allows
    to stretch the contrast of an image, only for visualisation purposes.

    KV WRL 2018

    Arguments:
    -----------
    im: np.array
        Image to rescale, can be 3D (multispectral) or 2D (single band)
    cloud_mask: np.array
        2D cloud mask with True where cloud pixels are
    prob_high: float
        probability of exceedence used to calculate the upper percentile

    Returns:
    -----------
    im_adj: np.array
        rescaled image
    """

    # lower percentile is set to 0
    prc_low = 0

    # reshape the 2D cloud mask into a 1D vector
    vec_mask = cloud_mask.reshape(im.shape[0] * im.shape[1])

    # if image contains several bands, stretch the contrast for each band
    if len(im.shape) > 2:
        # reshape into a vector
        vec =  im.reshape(im.shape[0] * im.shape[1], im.shape[2])
        # initiliase with NaN values
        vec_adj = np.ones((len(vec_mask), im.shape[2])) * np.nan
        # loop through the bands
        for i in range(im.shape[2]):
            # find the higher percentile (based on prob)
            prc_high = np.percentile(vec[~vec_mask, i], prob_high)
            # clip the image around the 2 percentiles and rescale the contrast
            vec_rescaled = exposure.rescale_intensity(vec[~vec_mask, i],
                                                      in_range=(prc_low, prc_high))
            vec_adj[~vec_mask,i] = vec_rescaled
        # reshape into image
        im_adj = vec_adj.reshape(im.shape[0], im.shape[1], im.shape[2])

    # if image only has 1 bands (grayscale image)
    else:
        vec =  im.reshape(im.shape[0] * im.shape[1])
        vec_adj = np.ones(len(vec_mask)) * np.nan
        prc_high = np.percentile(vec[~vec_mask], prob_high)
        vec_rescaled = exposure.rescale_intensity(vec[~vec_mask], in_range=(prc_low, prc_high))
        vec_adj[~vec_mask] = vec_rescaled
        im_adj = vec_adj.reshape(im.shape[0], im.shape[1])

    return im_adj
def rescale_image_intensity ( im , cloud_mask , prob_high ) : # lower percentile is set to 0 prc_low = 0 # reshape the 2D cloud mask into a 1D vector vec_mask = cloud_mask . reshape ( im . shape [ 0 ] * im . shape [ 1 ] ) # if image contains several bands, stretch the contrast for each band if len ( im . shape ) > 2 : # reshape into a vector vec = im . reshape ( im . shape [ 0 ] * im . shape [ 1 ] , im . shape [ 2 ] ) # initiliase with NaN values vec_adj = np . ones ( ( len ( vec_mask ) , im . shape [ 2 ] ) ) * np . nan # loop through the bands for i in range ( im . shape [ 2 ] ) : # find the higher percentile (based on prob) prc_high = np . percentile ( vec [ ~ vec_mask , i ] , prob_high ) # clip the image around the 2 percentiles and rescale the contrast vec_rescaled = exposure . rescale_intensity ( vec [ ~ vec_mask , i ] , in_range = ( prc_low , prc_high ) ) vec_adj [ ~ vec_mask , i ] = vec_rescaled # reshape into image im_adj = vec_adj . reshape ( im . shape [ 0 ] , im . shape [ 1 ] , im . shape [ 2 ] ) # if image only has 1 bands (grayscale image) else : vec = im . reshape ( im . shape [ 0 ] * im . shape [ 1 ] ) vec_adj = np . ones ( len ( vec_mask ) ) * np . nan prc_high = np . percentile ( vec [ ~ vec_mask ] , prob_high ) vec_rescaled = exposure . rescale_intensity ( vec [ ~ vec_mask ] , in_range = ( prc_low , prc_high ) ) vec_adj [ ~ vec_mask ] = vec_rescaled im_adj = vec_adj . reshape ( im . shape [ 0 ] , im . shape [ 1 ] ) return im_adj