Normalization

Normalization  

  By: SachaBernheim on April 6, 2024, 7:50 a.m.

The code provided to normalise the images is the following:

def percentile_normalization(image, pmin=2, pmax=99.8, axis=None):
    '''
    Compute a percentile normalization for the given image.

    Parameters:
    - image (array): array of the image file.
    - pmin  (int or float): the minimal percentage for the percentiles to compute. 
                            Values must be between 0 and 100 inclusive.
    - pmax  (int or float): the maximal percentage for the percentiles to compute. 
                            Values must be between 0 and 100 inclusive.
    - axis : Axis or axes along which the percentiles are computed. 
             The default (=None) is to compute it along a flattened version of the array.
    - dtype (dtype): type of the wanted percentiles (uint16 by default)

    Returns:
    Normalized image (np.ndarray): An array containing the normalized image.
    '''

    if not (np.isscalar(pmin) and np.isscalar(pmax) and 0 <= pmin < pmax <= 100 ):
        raise ValueError("Invalid values for pmin and pmax")

    low_p = np.percentile(image, pmin, axis=axis, keepdims=True)
    high_p = np.percentile(image, pmax, axis=axis, keepdims=True)

    if low_p == high_p:
        img_norm = image
        print(f"Same min {low_p} and high {high_p}, image may be empty")
    else:
        img_norm = (image - low_p) / (high_p - low_p)

    return img_norm

We are supposed to use it with axis=None exclusively right? Since we don't want to normalise across rows or columns but across the whole image?

Re: Normalization  

  By: JasonMendoza2008 on April 13, 2024, 9:54 a.m.

I guess if you have a batch size you'd probably not want to use None but rather 1

 Last edited by: JasonMendoza2008 on April 13, 2024, 9:54 a.m., edited 2 times in total.

Re: Normalization  

  By: dorian_kauffmann on April 15, 2024, 9:06 a.m.

Hi,

I'm not sure I understand the question.
This normalization is calculated over the entire image.
As you can see in the function, the axis is used to obtain the values of the upper and lower percentiles of the image in order to normalize the image according to these values.
By setting axis=None, you obtain a single value (the array is flattened) and normalize the entire array according to these two values. If you define axis=1, you don't get a percentile value, but a table with a value for each row (and for each column with axis=0). Normalization will therefore be based on these 2 tables of values.

After testing the function locally with axis=1, you may need to update the code with if np.all(low_p == high_p):.
If needed, you can have a look at the documentation here https://numpy.org/doc/stable/reference/generated/numpy.percentile.html

Is this clearer?