src/data_utils/loader.py
def _salt_and_pepper_noise(image: object, noise_typ: object = "gauss") -> object:
    """
    add Gaussian noise to distort the high-frequency features (zero pixels)
    Args:
        image (numpy.ndarray): the target image

    Returns:
        numpy.ndarray: the image with random noise
    """
    if noise_typ == "gauss":
        row, col, ch = image.shape  # one channel
        mean = 0
        var = 0.05
        sigma = var ** 0.5
        gauss = np.random.normal(mean, sigma, (row, col, ch))
        gauss = gauss.reshape(row, col, ch)
        noisy = image + gauss
        return noisy
    elif noise_typ == "s&p":
        s_vs_p = 0.5
        amount = 0.004
        out = np.copy(image)
        # Salt mode
        num_salt = np.ceil(amount * image.size * s_vs_p)
        coords = [np.random.randint(0, i - 1, int(num_salt))
                  for i in image.shape]
        out[coords] = 1

        # Pepper mode
        num_pepper = np.ceil(amount * image.size * (1. - s_vs_p))
        coords = [np.random.randint(0, i - 1, int(num_pepper))
                  for i in image.shape]
        out[coords] = 0
        return out
    elif noise_typ == "poisson":
        vals = len(np.unique(image))
        vals = 2 ** np.ceil(np.log2(vals))
        noisy = np.random.poisson(image * vals) / float(vals)
        return noisy
    elif noise_typ == "speckle":
        row, col, ch = image.shape
        gauss = np.random.randn(row, col, ch)
        gauss = gauss.reshape(row, col, ch)
        noisy = image + image * gauss
        return noisy

    add Gaussian noise to distort the high-frequency features (zero pixels)
    Args:
        image (numpy.ndarray): the target image

    Returns:
        numpy.ndarray: the image with random noise
    
add Gaussian noise to distort the high-frequency features (zero pixels)
add Gaussian noise to distort the high - frequency features ( zero pixels )
def _salt_and_pepper_noise(image: object, noise_typ: object = "gauss") -> object:
    if noise_typ == "gauss":
        row, col, ch = image.shape  
        mean = 0
        var = 0.05
        sigma = var ** 0.5
        gauss = np.random.normal(mean, sigma, (row, col, ch))
        gauss = gauss.reshape(row, col, ch)
        noisy = image + gauss
        return noisy
    elif noise_typ == "s&p":
        s_vs_p = 0.5
        amount = 0.004
        out = np.copy(image)
        num_salt = np.ceil(amount * image.size * s_vs_p)
        coords = [np.random.randint(0, i - 1, int(num_salt))
                  for i in image.shape]
        out[coords] = 1
        num_pepper = np.ceil(amount * image.size * (1. - s_vs_p))
        coords = [np.random.randint(0, i - 1, int(num_pepper))
                  for i in image.shape]
        out[coords] = 0
        return out
    elif noise_typ == "poisson":
        vals = len(np.unique(image))
        vals = 2 ** np.ceil(np.log2(vals))
        noisy = np.random.poisson(image * vals) / float(vals)
        return noisy
    elif noise_typ == "speckle":
        row, col, ch = image.shape
        gauss = np.random.randn(row, col, ch)
        gauss = gauss.reshape(row, col, ch)
        noisy = image + image * gauss
        return noisy
def _salt_and_pepper_noise ( image : object , noise_typ : object = "gauss" ) -> object : if noise_typ == "gauss" : row , col , ch = image . shape mean = 0 var = 0.05 sigma = var ** 0.5 gauss = np . random . normal ( mean , sigma , ( row , col , ch ) ) gauss = gauss . reshape ( row , col , ch ) noisy = image + gauss return noisy elif noise_typ == "s&p" : s_vs_p = 0.5 amount = 0.004 out = np . copy ( image ) num_salt = np . ceil ( amount * image . size * s_vs_p ) coords = [ np . random . randint ( 0 , i - 1 , int ( num_salt ) ) for i in image . shape ] out [ coords ] = 1 num_pepper = np . ceil ( amount * image . size * ( 1. - s_vs_p ) ) coords = [ np . random . randint ( 0 , i - 1 , int ( num_pepper ) ) for i in image . shape ] out [ coords ] = 0 return out elif noise_typ == "poisson" : vals = len ( np . unique ( image ) ) vals = 2 ** np . ceil ( np . log2 ( vals ) ) noisy = np . random . poisson ( image * vals ) / float ( vals ) return noisy elif noise_typ == "speckle" : row , col , ch = image . shape gauss = np . random . randn ( row , col , ch ) gauss = gauss . reshape ( row , col , ch ) noisy = image + image * gauss return noisy
add Gaussian noise to distort the high-frequency features (zero pixels)
add Gaussian noise to distort the high - frequency features ( zero pixels )
"""
    add Gaussian noise to distort the high-frequency features (zero pixels)
    Args:
        image (numpy.ndarray): the target image

    Returns:
        numpy.ndarray: the image with random noise
    """ # one channel # Salt mode # Pepper mode
the image with random noise
the image with random noise
numpy.ndarray
image
object
the target image
the target image
noise_typ
object