import torch


def norm_intensity(x, max_val=1):
    """
    This function normalizes the intensity of the input tensor x into [0, max_val]
    :param x: input tensor
    :param max_val: maximum value
    :return: a normalized tensor
    """
    if max_val is None:
        max_val = 1

    if len(list(torch.unique(input).size())) != 1:  # avoid the case that the tensor contains only one value
        x = x - torch.min(x)

    x = x / torch.max(x) * max_val
    return x