def _augment_noise(in_images, noise_type=NoiseType.gaussian_noise, std=1.):
    for i in range(len(in_images)):
        if noise_type == NoiseType.gaussian_noise:
            in_images[i] = random_noise(in_images[i],
                                        mode='gaussian',
                                        var=std**2)
        elif noise_type == NoiseType.poisson_noise:
            in_images[i] = random_noise(in_images[i], mode='poisson')
        elif noise_type == NoiseType.speckle_noise:
            in_images[i] = random_noise(in_images[i],
                                        mode='speckle',
                                        var=std**2)
    return in_images
Exemple #2
0
def add_noise(img, mode, var):
    sigma = var
    sigma_ = sigma / 255.
    sigma_ = sigma_ * sigma_
    if mode is None:
        raise Exception('please add the noise type !!')
    if var is None:
        noisemat = random_noise(img, mode=mode)
    elif mode == 'poisson':
        noisemat = random_noise(img, mode=mode)
    else:
        noisemat = random_noise(img, mode=mode, var=sigma_)
    return noisemat
def generateGaussianRandomVarNoise(img):
    #choosen_var = np.random.uniform(0.0005,0.01) # sigma from 0.02~0.1
    choosen_var = np.random.uniform(0.0001, 0.0025)  # sigma from 0.01~0.05
    #sigma = 10**(np.random.uniform(-4,-1))
    #sigma = sigma**2

    return np.clip(random_noise(img, mode='gaussian', var=choosen_var), 0, 1)
Exemple #4
0
    def __init__(self, sensor, colour=True, depth=True, **kwargs):
        """
        A wrapper class for the random_noise function
        from the skimage.util.noise package.
        The default noise type is gaussian additive noise.
        For a list of arguments refer to the
        scikit-image documentation.
        The 'seed' argument is disabled, since it would cause the
        class to generate the same noise for all images.

        Args:
            colour(bool): Whether to apply noise to the colour image.
            depth(bool): Whether to apply noise to the depth image.
        """
        self.logger = getLogger(f'{LOGGER_ID}.RandomImageNoise')
        self.logger.setLevel(DEBUG)
        super().__init__(sensor)
        img_types = ['colour', 'depth']
        use = [colour, depth]
        self._data = dict()
        self._img_types = [t for t, do in zip(img_types, use) if do]
        self.logger.debug('Creating noise for the following image types: '
                          f'{", ".join(self._img_types)}')
        if 'seed' in kwargs:
            self.logger.debug('The seed option is disabled and will '
                              'be removed from argument list.')
        kwargs.update({'seed': None})
        self._generate_noise = lambda img: skinoise.random_noise(img, **kwargs)
Exemple #5
0
def initialize(content, style, segmentation, L_max):
    # apply color transfer from S to C
    new_content = color_transform(content, style)
    # Build the Gaussian pyramids of C, S, and W
    images = pd.DataFrame(
        index=["L" + str(i) for i in range(L_max + 1)],
        columns=['content', 'style', 'segmentation', 'estimation'])
    images['original'] = list(
        pyramid_gaussian(content, multichannel=True, max_layer=L_max))
    images['content'] = list(
        pyramid_gaussian(new_content, multichannel=True, max_layer=L_max))
    images['style'] = list(
        pyramid_gaussian(style, multichannel=True, max_layer=L_max))
    images['segmentation'] = list(
        pyramid_gaussian(segmentation, multichannel=False, max_layer=L_max))
    #mages['segmentation'] = [None]*(L_max+1)

    # initialize X with additive gaussian to content ∼ N (0, 50)
    estimated_image = noise.random_noise(
        new_content, mode="gaussian", mean=0,
        var=50 / 256)  # normalized variance because image is normalized
    images['estimation'] = list(
        pyramid_gaussian(estimated_image, multichannel=True, max_layer=L_max))
    #    images['estimation'][images.index[-1]] = rescale(estimated_image, 1 / (2 ** L_max), multichannel=True,anti_aliasing=True, mode='constant', cval=0)

    return images
def generateGaussianRandomVarNoiseChannelSpecificSTD(img, std):
    out = np.zeros_like(img)
    for channel in range(img.shape[-1]):
        choosen_var = np.random.uniform(std - 0.005, std + 0.005)
        out[:, :, channel] = random_noise(img[:, :, channel],
                                          mode='gaussian',
                                          var=choosen_var)
    return out
Exemple #7
0
def noise_injection(x, noise_kwargs):
    from skimage.util.noise import random_noise
    channel = x.shape[2]
    if channel == 1 or channel == 3:
        y = random_noise(x, **noise_kwargs)
    else:
        raise RuntimeError('Channel is not grayscale or RGB')
    return y
def generateGaussianRandomVarNoiseChannelSpecific(img):
    out = np.zeros_like(img)
    for channel in range(img.shape[-1]):
        choosen_var = np.random.uniform(0.0001, 0.0025)  # sigma from 0.01~0.05
        out[:, :, channel] = random_noise(img[:, :, channel],
                                          mode='gaussian',
                                          var=choosen_var)
    return out
def generateGaussianRandomVarNoiseChannelSpecific(img, std=0.02):
    out = np.zeros_like(img)
    for channel in range(img.shape[-1]):
        choosen_var = std**2  #CHOSEN_STD(1)[0][0]**2 #std from 1600: 0.06585 #np.random.uniform(0.0005,0.01)
        out[:, :, channel] = random_noise(img[:, :, channel],
                                          mode='gaussian',
                                          var=choosen_var)
    out = np.clip(out, 0, 1)
    return out
def add_noise(img_path, i):
    img=Image.open(img_path)
    img=np.array(img)
    var=np.random.uniform(0,0.01)
    mean=np.random.uniform(0,0.1)
    noise_img=random_noise(img,  mean=mean, var=var)
    print(var, "\t", mean)
    Image.fromarray(np.uint8(noise_img*255)).save("C:/Users/Administrator/Desktop/train_B/{}.jpg".format(str(i)))
    print(i, "done")
Exemple #11
0
def poisson_noise(ref):
    """Poisson noise

    Parameters
    ----------
    ref : :class:`numpy.ndarray`
        Image to be noised.

    Returns
    -------
    inp : :class:`numpy.ndarray`
        Noised image.
    """
    inp = random_noise(ref, mode='poisson', seed=RANDOM_SEED, clip=True)

    assert inp.shape == ref.shape, "Shape mismatch between input ({}) and output ({})".format(inp.shape, ref.shape)
    return inp
Exemple #12
0
def gaussian_noise(ref, noise_level=15):
    """Gaussian noise

    Parameters
    ----------
    ref : :class:`numpy.ndarray`
        Image to be noised.
    noise_level : :class:`numpy.ndarray`
        Level of corruption. Always give the noise_level in terms of 0-255 pixel intensity range.

    Returns
    -------
    inp : :class:`numpy.ndarray`
        Noised image.
    """
    inp = random_noise(ref, mode='gaussian', clip=False, var=(noise_level / 255) ** 2)

    assert inp.shape == ref.shape, "Shape mismatch between input ({}) and output ({})".format(inp.shape, ref.shape)
    return inp
Exemple #13
0
def speckle_noise(ref, noise_level=15):
    """Speckle noise

    Parameters
    ----------
    ref : :class:`numpy.ndarray`
        Image to be noised.
    noise_level : :class:`numpy.ndarray`
        Percentage of perturbed pixels.

    Returns
    -------
    inp : :class:`numpy.ndarray`
        Noised image.
    """
    inp = random_noise(ref, mode='speckle', clip=True, var=noise_level)

    assert inp.shape == ref.shape, "Shape mismatch between input ({}) and output ({})".format(inp.shape, ref.shape)
    return inp
Exemple #14
0
 def _generate_noisy_data(self):
     """
     This member function is called to generate the noise
     and applies it to the point cloud.
     """
     point_cloud = np.array(self._sensor.data['points'])
     if point_cloud.size > 0:
         point_cloud = point_cloud.astype(np.float64)
         magnitude = max(point_cloud.max(), np.abs(point_cloud.min()))
         point_cloud /= magnitude
         point_cloud = skinoise.random_noise(point_cloud,
                                             mode='gaussian',
                                             mean=self._mean,
                                             var=self._var)
         point_cloud = point_cloud * magnitude
         point_cloud = skitype.img_as_float32(point_cloud)
     else:
         point_cloud = self._sensor.data['points']
     self._data.update({'points': point_cloud})
Exemple #15
0
def imgNoise(oriImg: str, flag=True,noise_type:list=[]):
    if len(noise_type) == 0:
        noise_type = ['gaussian', 'poisson', 's&p', 'speckle']
    l = np.random.randint(2, size=len(noise_type)).tolist()
    p = list(zip(noise_type, l))

    if isinstance(oriImg, str):
        if os.path.exists(oriImg):
            img = io.imread(oriImg)
        else:
            raise FileNotFoundError('Original image not found')
    else:
        img = oriImg

    for i in p:
        if i[1] != 0:
            img = snoise.random_noise(img, mode=i[0])

    # print(np.max(img))
    # print(np.min(img))

    img = np.array(img * 255).astype(np.uint8)

    if flag:
        parent_path = os.path.dirname(oriImg)
        if os.path.exists(parent_path + os.sep + 'augimgs_'):
            pass
        else:
            os.makedirs(parent_path + os.sep + 'augimgs_')
        # fileName = oriLabel.split(os.sep)[-1].replace('.json','')
        tmp = os.path.splitext(oriImg)[0]
        fileName = tmp.split(os.sep)[-1]
        io.imsave(
            parent_path + os.sep + 'augimgs_' + os.sep + fileName +
            '_noise.jpg', img)

    else:
        d = dict()
        d['noise'] = Ori_Pro(img, None)

        return d
Exemple #16
0
def salt_and_pepper_noise(ref, noise_level=15):
    """Salt and pepper noise

    Parameters
    ----------
    ref : :class:`numpy.ndarray`
        Image to be noised.
    noise_level : :class:`numpy.ndarray`
        Percentage of perturbed pixels.

    Returns
    -------
    inp : :class:`numpy.ndarray`
        Noised image.
    """
    assert noise_level >= 0 and noise_level <= 100, "Expected noise_level to be a percentage," \
                                                    "but got {}".format(noise_level)
    inp = random_noise(ref, mode='s&p', seed=RANDOM_SEED, clip=True, amount=noise_level / 100.0)

    assert inp.shape == ref.shape, "Shape mismatch between input ({}) and output ({})".format(inp.shape, ref.shape)
    return inp
def regular_augmenter(img, gt):
    shapes = len(img.shape)
    img = np.squeeze(img)
    gt = np.squeeze(gt)
    if np.random.random() > 0.5:
        img = np.fliplr(img)
        gt = np.fliplr(gt)
    if np.random.random() > 0.5:
        idx_x = [576, 544, 512, 480]
        idx_y = [768, 736, 704, 672]
        img = cropND(
            img, (idx_x[np.random.randint(4)], idx_y[np.random.randint(4)], 3))
        gt = cropND(gt, img.shape)
    if np.random.random() > 0.5:
        img = random_noise(img)
    img = img[np.newaxis, ...]
    gt = gt[np.newaxis, ..., np.newaxis]
    assert len(img.shape) == shapes
    assert len(gt.shape) == shapes
    assert img.shape[1] % 32 == 0
    assert img.shape[2] % 32 == 0
    return img, gt
Exemple #18
0
def imgNoise(oriImg: str, oriLabel: str, flag=True):
    """
    see skimage.util.random_noise    
    """
    noise_type = ['gaussian', 'poisson', 's&p', 'speckle']

    l = np.random.randint(2, size=len(noise_type)).tolist()
    # print(l)
    p = list(zip(noise_type, l))

    if isinstance(oriImg, str):
        if os.path.exists(oriImg):
            img = io.imread(oriImg)
        else:
            raise FileNotFoundError('Original image not found')
    elif isinstance(oriImg, np.ndarray):
        img = oriImg
    else:
        logger.error('input {} type error'.format(imgFlip))
        return

    for i in p:
        if i[1] != 0:
            img = snoise.random_noise(img, mode=i[0])

    img = np.array(img * 255).astype(np.uint8)

    if flag:
        parent_path = os.path.dirname(oriLabel)
        if os.path.exists(parent_path + os.sep + 'jsons_'):
            pass
        else:
            os.makedirs(parent_path + os.sep + 'jsons_')
        fileName = oriLabel.split(os.sep)[-1].replace('.json', '')

        io.imsave(
            parent_path + os.sep + 'jsons_' + os.sep + fileName + '_noise.jpg',
            img)

        try:
            if isinstance(oriLabel, str):
                shutil.copyfile(
                    oriLabel, parent_path + os.sep + 'jsons_' + os.sep +
                    fileName + '_noise.json')

                base64code = imgEncode(parent_path + os.sep + 'jsons_' +
                                       os.sep + fileName + '_noise.jpg')

                with open(
                        parent_path + os.sep + 'jsons_' + os.sep + fileName +
                        '_noise.json', 'r') as f:
                    load_dict = json.load(f)

                load_dict['imageData'] = base64code

                with open(
                        parent_path + os.sep + 'jsons_' + os.sep + fileName +
                        '_noise.json', 'w') as f:
                    # json.dump(load_dict,parent_path+os.sep+'jsons_'+os.sep+fileName+'_noise.json')
                    f.write(json.dumps(load_dict))

            elif isinstance(oriLabel, np.ndarray):
                """
                labeled file can be an Image
                """
                noisedMask_j = getMultiShapes(parent_path + os.sep + 'jsons_' +
                                              os.sep + fileName + '_noise.jpg',
                                              oriLabel,
                                              flag=True,
                                              labelYamlPath='')
                with open(
                        parent_path + os.sep + 'jsons_' + os.sep + fileName +
                        '_noise.json', 'w') as f:
                    f.write(json.dumps(noisedMask_j))

        except Exception:
            print(traceback.format_exc())

    else:
        d = dict()
        # mask = processor(oriLabel,flag=True)
        if isinstance(oriLabel, str):
            mask = processor(oriLabel, flag=True)
        elif isinstance(oriLabel, np.ndarray):
            mask = oriLabel
        else:
            raise TypeError(
                "input parameter 'oriLabel' type {} is not supported".format(
                    type(oriLabel)))
        d['noise'] = Ori_Pro(img, mask)

        return d
Exemple #19
0
def style_transfer(content_path, style_path, I_irls, I_alg, seg_fac, L_max):
    content = io.imread(content_path).astype('float64') / 255
    content = cv2.resize(content, (400, 400))
    style = io.imread(style_path).astype('float64') / 255
    style = cv2.resize(style, (400, 400))
    segmentation = get_segmentation(content_path) * seg_fac + 0.25 * seg_fac
    #    segmentation = np.zeros(content.shape[:-1])

    images = initialize(content, style, segmentation, L_max)
    scaler_objects, pca_objects, nn_objects, style_patches = prepare_style_patches(
        images['style'], L_max=L_max)
    for layer in reversed(style_patches.index):
        for index, patch_size in enumerate(style_patches.columns):
            # add another loop for IRLS itarations
            scaler = scaler_objects[patch_size][layer]
            pca = pca_objects[patch_size][layer]
            nn = nn_objects[patch_size][layer]
            current_style_patches = style_patches[patch_size][layer]

            for _ in range(I_alg):
                # convert estimation image of the current layer to patches we can operate on
                estimation_image = noise.random_noise(
                    images['estimation'][layer],
                    mode="gaussian",
                    mean=0,
                    var=100 /
                    256)  # normalized variance because image is normalized

                current_estimation_patches = image_to_patches(estimation_image,
                                                              is_pyramid=False,
                                                              index=index)
                flat_curr_estimation_patches = flatten_patches(
                    current_estimation_patches, rotate=False)
                scaled_estimation_patches = apply_standard_Scaler(
                    flat_curr_estimation_patches, scaler=scaler)
                reduced_estimation_patches = apply_pca(
                    scaled_estimation_patches, pca=pca)
                nn_indices = apply_nearest_neighbor(reduced_estimation_patches,
                                                    nbrs=nn)
                nn_patches = get_nn_style_patch_from_indices(
                    current_style_patches, nn_indices,
                    current_estimation_patches.shape)
                # IRLS
                estimation_image = IRLS(images['estimation'][layer],
                                        nn_patches, patch_size,
                                        subsampling_gaps[index], I_irls)
                e1 = np.copy(estimation_image)
                # content fusion
                estimation_image = content_fusion(
                    images['content'][layer], estimation_image,
                    images['segmentation'][layer])
                e2 = np.copy(estimation_image)
                # color transfer
                estimation_image = color_transform(estimation_image,
                                                   images['style'][layer])
                e3 = np.copy(estimation_image)
                # domain transform filter
                #                sigma_s = 100
                #                sigma_r = 3
                #
                #                estimation_image = Iterative_C(estimation_image * 255, sigma_s, sigma_r,3)/255
                #                e4 = np.copy(estimation_image)

                show_animated([
                    images['style'][layer], images['original'][layer],
                    estimation_image
                ], ["style", "content", "estimation"])
                # print("style max", images['style'][layer].max(),"style min", images['style'][layer].min())
                # print("content max", images['content'][layer].max(),"content min", images['content'][layer].min())
                # print("est max", images['estimation'][layer].max(),"est min", images['estimation'][layer].min())
                #                estimation_image = noise.random_noise(estimation_image, mode="gaussian", mean=0, var=50 / 256)  # normalized variance because image is normalized
                #                e5 = estimation_image
                #                show_animated([e1, e2, e3, e4, e5], ["irls", "content fusion", "color transfer", "domain transform", "noise"])
                images['estimation'][layer] = estimation_image

            # scaling up
        images['estimation'][list(images.index).index(layer) - 1] =\
                rescale(images['estimation'][layer], 2, multichannel=True,anti_aliasing=True, mode='constant', cval=0)


#    show_images([images['style'][layer], images['original'][layer], images['estimation']['L0']],["style", "content", "result"])
#    plt.pause(500)
    return images['estimation']['L0']
Exemple #20
0
                        'GT_SRGB', 'NOISY_SRGB')).astype('float32') / 255.0
                img_clean_NF = imageio.imread(
                    img_name.replace(
                        'samples_gts',
                        'samples_gtsNF')).astype('float32') / 255.0
                img_noisy_NF = imageio.imread(
                    img_name.replace('samples_gts', 'samples_noisyNF').replace(
                        'GT_SRGB', 'NOISY_SRGB')).astype('float32') / 255.0
                img_gan = imageio.imread(
                    img_name.replace('samples_gts', 'GAN').replace(
                        '.png', '_fake_B.png')).astype('float32') / 255.0
                #std = np.random.uniform(0.24,11.51)/255.0  #Noiseflow values
                std = gaussian_stds[str(cur_iso)]['both_dataset']
                std_linear = gaussian_stds[str(cur_iso)]['both_dataset_linear']
                img_gaussian = random_noise(img_clean,
                                            mode='gaussian',
                                            var=std**2)
                #img_gaussian = np.clip(random_noise(img_clean**2.2,mode='gaussian',var=std_linear**2)**(1/2.2),0,1) ## AWGN linear
                #img_awgn_poi_linear = generateGaussianPoissonNoise(img_clean,std)
                img_awgn_poi_linear = np.clip(
                    generateGaussianPoissonNoise((img_clean**2.2),
                                                 std_linear)**(1 / 2.2), 0,
                    1)  ## GaussianPoissonian in linear
                #img_gaussian = imageio.imread(img_name.replace('samples_GT_sRGB','samples_gaussian_sRGB')).astype('float32')/255.0
                img_noiseflow = imageio.imread(
                    img_name.replace(
                        'samples_gts',
                        'samples_noiseflowNF')).astype('float32') / 255.0

                #Computing diffs
                diff_natural = img_noisy - img_clean
def generateGaussianRandomVarNoiseSTD(img, std):
    choosen_var = np.random.uniform(std - 0.005,
                                    std + 0.005)  #sigma=[0,022~0.1]
    return random_noise(img, mode='gaussian', var=choosen_var)
def generateGaussianRandomVarNoise(img, std=0.02):
    #choosen_var = np.random.uniform(0.0005,0.01) #sigma=[0,022~0.1]
    choosen_var = std**2  #gaussian_stds['800']['both_dataset']**2  #CHOSEN_STD(1)[0][0]**2 #std from 1600: 0.06585
    return random_noise(img, mode='gaussian', var=choosen_var)
def generatePoissonNoise(img):
    return random_noise(img, mode='poisson')