Esempio n. 1
0
def test_localvar():
    seed = 42
    data = np.zeros((128, 128)) + 0.5
    local_vars = np.zeros((128, 128)) + 0.001
    local_vars[:64, 64:] = 0.1
    local_vars[64:, :64] = 0.25
    local_vars[64:, 64:] = 0.45

    data_gaussian = random_noise(data, mode='localvar', seed=seed,
                                 local_vars=local_vars, clip=False)
    assert 0. < data_gaussian[:64, :64].var() < 0.002
    assert 0.095 < data_gaussian[:64, 64:].var() < 0.105
    assert 0.245 < data_gaussian[64:, :64].var() < 0.255
    assert 0.445 < data_gaussian[64:, 64:].var() < 0.455

    # Ensure local variance bounds checking works properly
    bad_local_vars = np.zeros_like(data)
    with testing.raises(ValueError):
        random_noise(data, mode='localvar', seed=seed,
                     local_vars=bad_local_vars)
    bad_local_vars += 0.1
    bad_local_vars[0, 0] = -1
    with testing.raises(ValueError):
        random_noise(data, mode='localvar', seed=seed,
                     local_vars=bad_local_vars)
def noise(image):
    r = np.random.rand(1)[0]
    # TODO randomize parameters of the noises; check how to init seed
    if r < 0.33:
        return random_noise(x, 's&p', seed=np.random.randint(1000000))
    if r < 0.66:
        return random_noise(x, 'gaussian', seed=np.random.randint(1000000))
    return random_noise(x, 'speckle', seed=np.random.randint(1000000))
Esempio n. 3
0
def test_gaussian():
    seed = 42
    data = np.zeros((128, 128)) + 0.5
    data_gaussian = random_noise(data, seed=seed, var=0.01)
    assert 0.008 < data_gaussian.var() < 0.012

    data_gaussian = random_noise(data, seed=seed, mean=0.3, var=0.015)
    assert 0.28 < data_gaussian.mean() - 0.5 < 0.32
    assert 0.012 < data_gaussian.var() < 0.018
Esempio n. 4
0
def test_poisson():
    seed = 42
    data = camera()  # 512x512 grayscale uint8
    cam_noisy = random_noise(data, mode='poisson', seed=seed)
    cam_noisy2 = random_noise(data, mode='poisson', seed=seed, clip=False)

    np.random.seed(seed=seed)
    expected = np.random.poisson(img_as_float(data) * 256) / 256.
    assert_allclose(cam_noisy, np.clip(expected, 0., 1.))
    assert_allclose(cam_noisy2, expected)
    def load(self):
        self._log.info('Initiating load of Chars74K data set')

        image_paths, image_labels = self.get_all_image_paths()
        image_matrices = []
        all_labels = []

        for index in range(len(image_paths)):
            raw_image = io.imread(image_paths[index], as_grey=True)  # As grey to get 2D without RGB
            raw_image = raw_image / 255.0  # Normalize image by dividing image by 255.0
            image_matrices.append(raw_image.reshape((self.config['img_size'][0] ** 2)))
            all_labels.append(image_labels[index])
            if self.config['extend_data_set']:
                # Add noisy images
                for noise in self.config['noise_types']:
                    noisy_img = random_noise(raw_image, mode=noise)
                    image_matrices.append(noisy_img.reshape((400, )))
                    all_labels.append(image_labels[index])

                # Add shifted images
                shifted_images = [np.roll(raw_image, 1, axis=i) for i in range(-1, 2)]
                for image in shifted_images:
                    image_matrices.append(image.reshape((400, )))
                    all_labels.append(image_labels[index])

        # Split data set into (X_train, y_train, X_test and y_test)
        data_set_tuple = self.split_data_set(image_matrices, all_labels)
        log.info('Loaded %i images of %s pixels' % (len(all_labels), self.config['img_size']))
        return data_set_tuple
def filtre2D(img):
    N = 3
    type_bruit = 'AG'

    selem = np.ones([N, N])
    if type_bruit == 'AG':
        bruit = util.random_noise(np.zeros(img.shape), mode='gaussian')
        img_bruitee = util.random_noise(img, mode='gaussian')
    else:
        bruit = util.random_noise(np.zeros(img.shape), mode='s&p')
        img_bruitee = util.random_noise(img, mode='s&p')

    img_bruit_median = filters.median(img_bruitee, selem)
    img_bruit_linear = ndimage.convolve(img_bruitee, selem)

    fig = plt.figure()

    if type_bruit == 'AG':
        bruit_linear = ndimage.convolve(bruit, selem)
        img_linear = ndimage.convolve(img, selem)
        fig.add_subplot(3, 3, 1)
        plt.imshow(img, cmap='gray')
        fig.add_subplot(3, 3, 2)
        plt.imshow(bruit, cmap='gray')
        fig.add_subplot(3, 3, 3)
        plt.imshow(img_bruitee, cmap='gray')
        fig.add_subplot(3, 3, 4)
        plt.imshow(img_linear, cmap='gray')
        fig.add_subplot(3, 3, 5)
        plt.imshow(bruit_linear, cmap='gray')
        fig.add_subplot(3, 3, 6)
        plt.imshow(img_bruit_linear, cmap='gray')
        fig.add_subplot(3, 3, 9)
        plt.imshow(img_bruit_median, cmap='gray')
    else:
        fig.add_subplot(2, 2, 1)
        plt.imshow(img, cmap='gray')
        fig.add_subplot(2, 2, 2)
        plt.imshow(img_bruitee, cmap='gray')
        fig.add_subplot(2, 2, 3)
        plt.imshow(img_bruit_linear, cmap='gray')
        fig.add_subplot(2, 2, 4)
        plt.imshow(img_bruit_median, cmap='gray')
    # fig.tight_layout()
    plt.show()
Esempio n. 7
0
def test_extended_search_area_sig2noise():
    """ test of the extended area PIV with sig2peak """
    frame_a = np.zeros((64,64))
    frame_a = random_noise(frame_a)
    frame_a = img_as_ubyte(frame_a)
    frame_b = np.roll(np.roll(frame_a,3,axis=1),2,axis=0)
    u,v,s2n = piv(frame_a,frame_b,window_size=16,search_size=32,
                  sig2noise_method='peak2peak')
    assert(np.max(np.abs(u-3)+np.abs(v+2)) <= 0.2)
Esempio n. 8
0
def test_extended_search_area_overlap():
    """ test of the extended area PIV with different overlap """
    frame_a = np.zeros((64,64))
    frame_a = random_noise(frame_a)
    frame_a = img_as_ubyte(frame_a)
    frame_b = np.roll(np.roll(frame_a,3,axis=1),2,axis=0)
    u,v = piv(frame_a,frame_b,window_size=16,search_size=32,overlap=8)
    # print u,v
    assert(np.max(np.abs(u-3)+np.abs(v+2)) <= 0.3)
Esempio n. 9
0
def test_speckle():
    seed = 42
    data = np.zeros((128, 128)) + 0.1
    np.random.seed(seed=42)
    noise = np.random.normal(0.1, 0.02 ** 0.5, (128, 128))
    expected = np.clip(data + data * noise, 0, 1)

    data_speckle = random_noise(data, mode="speckle", seed=seed, m=0.1, v=0.02)
    assert_allclose(expected, data_speckle)
Esempio n. 10
0
def test_clip_gaussian():
    seed = 42
    data = camera()                             # 512x512 grayscale uint8
    data_signed = img_as_float(data) * 2. - 1.  # Same image, on range [-1, 1]

    # Signed and unsigned, clipped
    cam_gauss = random_noise(data, mode='gaussian', seed=seed, clip=True)
    cam_gauss2 = random_noise(data_signed, mode='gaussian', seed=seed,
                              clip=True)
    assert (cam_gauss.max() == 1.) and (cam_gauss.min() == 0.)
    assert (cam_gauss2.max() == 1.) and (cam_gauss2.min() == -1.)

    # Signed and unsigned, unclipped
    cam_gauss = random_noise(data, mode='gaussian', seed=seed, clip=False)
    cam_gauss2 = random_noise(data_signed, mode='gaussian', seed=seed,
                              clip=False)
    assert (cam_gauss.max() > 1.22) and (cam_gauss.min() < -0.36)
    assert (cam_gauss2.max() > 1.219) and (cam_gauss2.min() < -1.337)
Esempio n. 11
0
def test_clip_poisson():
    seed = 42
    data = camera()                             # 512x512 grayscale uint8
    data_signed = img_as_float(data) * 2. - 1.  # Same image, on range [-1, 1]

    # Signed and unsigned, clipped
    cam_poisson = random_noise(data, mode='poisson', seed=seed, clip=True)
    cam_poisson2 = random_noise(data_signed, mode='poisson', seed=seed,
                                clip=True)
    assert (cam_poisson.max() == 1.) and (cam_poisson.min() == 0.)
    assert (cam_poisson2.max() == 1.) and (cam_poisson2.min() == -1.)

    # Signed and unsigned, unclipped
    cam_poisson = random_noise(data, mode='poisson', seed=seed, clip=False)
    cam_poisson2 = random_noise(data_signed, mode='poisson', seed=seed,
                                clip=False)
    assert (cam_poisson.max() > 1.15) and (cam_poisson.min() == 0.)
    assert (cam_poisson2.max() > 1.3) and (cam_poisson2.min() == -1.)
Esempio n. 12
0
def test_clip_speckle():
    seed = 42
    data = camera()                             # 512x512 grayscale uint8
    data_signed = img_as_float(data) * 2. - 1.  # Same image, on range [-1, 1]

    # Signed and unsigned, clipped
    cam_speckle = random_noise(data, mode='speckle', seed=seed, clip=True)
    cam_speckle2 = random_noise(data_signed, mode='speckle', seed=seed,
                                clip=True)
    assert (cam_speckle.max() == 1.) and (cam_speckle.min() == 0.)
    assert (cam_speckle2.max() == 1.) and (cam_speckle2.min() == -1.)

    # Signed and unsigned, unclipped
    cam_speckle = random_noise(data, mode='speckle', seed=seed, clip=False)
    cam_speckle2 = random_noise(data_signed, mode='speckle', seed=seed,
                                clip=False)
    assert (cam_speckle.max() > 1.219) and (cam_speckle.min() == 0.)
    assert (cam_speckle2.max() > 1.219) and (cam_speckle2.min() < -1.306)
Esempio n. 13
0
def test_extended_search_area():
    """ test of the extended area PIV """
    frame_a = np.zeros((64,64))
    frame_a = random_noise(frame_a)
    frame_a = img_as_ubyte(frame_a)
    frame_b = np.roll(np.roll(frame_a,3,axis=1),2,axis=0)
    u,v = piv(frame_a.astype(np.int32),frame_b.astype(np.int32),window_size=16,search_area_size=32,overlap=0)
    # print u,v
    assert(np.max(np.abs(u-3)+np.abs(v+2)) <= 0.5)
Esempio n. 14
0
def test_piv_smaller_window():
    """ test of the search area larger than the window """
    frame_a = np.zeros((32,32))
    frame_a = random_noise(frame_a)
    frame_a = img_as_ubyte(frame_a)
    frame_b = np.roll(np.roll(frame_a,-3,axis=1),-2,axis=0)
    u,v = piv(frame_a,frame_b,window_size=16,search_size=32)
    # print u,v
    assert(np.max(np.abs(u+3)) < 0.2)
    assert(np.max(np.abs(v-2)) < 0.2)
Esempio n. 15
0
def test_piv():
    """ test of the simplest PIV run """
    frame_a = np.zeros((32,32))
    frame_a = random_noise(frame_a)
    frame_a = img_as_ubyte(frame_a)
    frame_b = np.roll(np.roll(frame_a,3,axis=1),2,axis=0)
    u,v = piv(frame_a,frame_b,window_size=32)
    # print u,v
    assert(np.max(np.abs(u-3)) < 0.2)
    assert(np.max(np.abs(v+2)) < 0.2)
Esempio n. 16
0
def test_piv_smaller_window():
    """ test of the simplest PIV run """
    frame_a = np.zeros((32,32))
    frame_a = random_noise(frame_a)
    frame_a = img_as_ubyte(frame_a)
    frame_b = np.roll(np.roll(frame_a,-3,axis=1),-2,axis=0)
    u,v = piv(frame_a.astype(np.int32),frame_b.astype(np.int32),window_size=16,search_area_size=32)
    # print u,v
    assert(np.max(np.abs(u+3)) < 0.2)
    assert(np.max(np.abs(v-2)) < 0.2)
Esempio n. 17
0
def test_process_extended_search_area():
    """ test of the extended area PIV from Cython """
    frame_a = np.zeros((64,64))
    frame_a = random_noise(frame_a)
    frame_a = img_as_ubyte(frame_a)
    frame_b = np.roll(np.roll(frame_a,3,axis=1),2,axis=0)
    u,v = process.extended_search_area_piv(frame_a.astype(np.int32),
                                           frame_b.astype(np.int32),
window_size=16,search_area_size=32,dt=1,overlap=0)
    # print u,v
    assert(np.max(np.abs(u[:-1,:-1]-3)+np.abs(v[:-1,:-1]+2)) <= 0.2)
Esempio n. 18
0
def AddNoise(img):
    # mean = 0
    # sigma = 0.045
    # gauss = np.random.normal(mean, sigma, img.shape)
    # gauss = gauss.reshape(img.shape[0], img.shape[1])
    # noisy = img + gauss
    noisy = util.random_noise(img, mode='gaussian', var=0.002)
    # noisy = util.random_noise(img, mode='gaussian', var=0.0015)

    # noisy = util.random_noise(img, mode='s&p')
    return noisy
Esempio n. 19
0
def SaltPepper(request):

    from skimage.util import random_noise

    image, response = process(request, False)

    image = random_noise(image, mode = 's&p')

    io.imsave(response['filename'], image)

    return JsonResponse(response)
Esempio n. 20
0
def test_pepper():
    seed = 42
    cam = img_as_float(camera())
    cam_noisy = random_noise(cam, seed=seed, mode="pepper", d=0.15)
    peppermask = cam != cam_noisy

    # Ensure all changes are to 1.0
    assert_allclose(cam_noisy[peppermask], np.zeros(peppermask.sum()))

    # Ensure approximately correct amount of noise was added
    proportion = float(peppermask.sum()) / (cam.shape[0] * cam.shape[1])
    assert 0.11 < proportion <= 0.18
Esempio n. 21
0
def test_salt():
    seed = 42
    cam = img_as_float(camera())
    cam_noisy = random_noise(cam, seed=seed, mode='salt', amount=0.15)
    saltmask = cam != cam_noisy

    # Ensure all changes are to 1.0
    assert_allclose(cam_noisy[saltmask], np.ones(saltmask.sum()))

    # Ensure approximately correct amount of noise was added
    proportion = float(saltmask.sum()) / (cam.shape[0] * cam.shape[1])
    assert 0.11 < proportion <= 0.15
def recall_with_noise(clf, X, noise_amount=0.05):
    X = X.astype(float)
    X_noise = random_noise(X, mode='s&p', amount=noise_amount)
    X_noise = binarize(X_noise, binary_values=(-1,1))
    X_recall = []
    for x in X_noise:
        recall = clf.recall(x=x, n_times=10).reshape(-1)
        recall[recall < 0] = -1
        recall[recall >= 0] = 1
        X_recall.append(recall)
    X_recall = np.array(X_recall)
    return X, X_noise, X_recall
Esempio n. 23
0
def test_piv():
    """ test of the simplest PIV run """
    frame_a = np.zeros((32,32))
    frame_a = random_noise(frame_a)
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        frame_a = img_as_ubyte(frame_a)
    frame_b = np.roll(np.roll(frame_a,3,axis=1),2,axis=0)
    u,v = piv(frame_a.astype(np.int32),frame_b.astype(np.int32),window_size=32)
    # print u,v
    assert(np.max(np.abs(u-3)) < 0.2)
    assert(np.max(np.abs(v+2)) < 0.2)
Esempio n. 24
0
def input_data(path, filename, blur_amount):
    
    img_path = os.path.join(path, filename)
    img = io.imread(img_path)
    img = img[85:341,90:346]
    
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    
    blur = cv2.GaussianBlur(gray,(blur_amount,blur_amount),0)
    
    noise = random_noise(gray, mode = "gaussian")

    return img, gray, blur
Esempio n. 25
0
def test_pepper():
    seed = 42
    cam = img_as_float(camera())
    data_signed = cam * 2. - 1.   # Same image, on range [-1, 1]

    cam_noisy = random_noise(cam, seed=seed, mode='pepper', amount=0.15)
    peppermask = cam != cam_noisy

    # Ensure all changes are to 1.0
    assert_allclose(cam_noisy[peppermask], np.zeros(peppermask.sum()))

    # Ensure approximately correct amount of noise was added
    proportion = float(peppermask.sum()) / (cam.shape[0] * cam.shape[1])
    assert 0.11 < proportion <= 0.15

    # Check to make sure pepper gets added properly to signed images
    orig_zeros = (data_signed == -1).sum()
    cam_noisy_signed = random_noise(data_signed, seed=seed, mode='pepper',
                                    amount=.15)

    proportion = (float((cam_noisy_signed == -1).sum() - orig_zeros) /
                  (cam.shape[0] * cam.shape[1]))
    assert 0.11 < proportion <= 0.15
Esempio n. 26
0
def test_salt_and_pepper():
    seed = 42
    cam = img_as_float(camera())
    cam_noisy = random_noise(cam, seed=seed, mode="s&p", d=0.15, p=0.25)
    saltmask = np.logical_and(cam != cam_noisy, cam_noisy == 1.0)
    peppermask = np.logical_and(cam != cam_noisy, cam_noisy == 0.0)

    # Ensure all changes are to 0. or 1.
    assert_allclose(cam_noisy[saltmask], np.ones(saltmask.sum()))
    assert_allclose(cam_noisy[peppermask], np.zeros(peppermask.sum()))

    # Ensure approximately correct amount of noise was added
    proportion = float(saltmask.sum() + peppermask.sum()) / (cam.shape[0] * cam.shape[1])
    assert 0.11 < proportion <= 0.18

    # Verify the relative amount of salt vs. pepper is close to expected
    assert 0.18 < saltmask.sum() / float(peppermask.sum()) < 0.32
Esempio n. 27
0
def test_piv_vs_extended_search():
    """ test of the simplest PIV run """
    import openpiv.process
    import openpiv.pyprocess
    frame_a = np.zeros((32,32))
    frame_a = random_noise(frame_a)
    frame_a = img_as_ubyte(frame_a)
    frame_b = np.roll(np.roll(frame_a,3,axis=1),2,axis=0)
    u,v = openpiv.process.extended_search_area_piv(frame_a.astype(np.int32),frame_b.astype(np.int32),window_size=32)
    u1,v1 = openpiv.pyprocess.extended_search_area_piv(frame_a.astype(np.int32),
                                           frame_b.astype(np.int32),
window_size=32,search_area_size=32,dt=1,overlap=0)
    
    print(u,v)
    print(u1,v1)
    
    assert(np.allclose(u,u1))
    assert(np.allclose(v,v1))
Esempio n. 28
0
def add_gaussnoise(img, noise_sigma):
	""" 
	add iid gaussian noise on image

	Params
	------
	img
	noise_sigma

	Return
	------
	img
	"""
	if noise_sigma == 0:
		result = img
	else:
		result = img+random_noise(np.zeros(img.shape), mode='gaussian',var=noise_sigma**2,clip=False)

	return result
Esempio n. 29
0
def get_test_image(
        blur_sigma, noise_sigma,
        contrast=DEFAULT_CONTRAST):
    
    # Initialize new image
    # image = np.empty(DEFAULT_DIMENSIONS)
    # image.fill(contrast)
    # # image = image.astype(np.uint8)
    # image = util.img_as_ubyte(image)

    # image = PIL.Image.fromarray(image, mode='L')
    # image = PIL.Image.new('L', DEFAULT_DIMENSIONS, color=bg_color)
    image = PIL.Image.new('L', DEFAULT_DIMENSIONS, (contrast * 255))

    # Draw text
    draw = ImageDraw.Draw(image)
    font = ImageFont.truetype(FONT_PATH, 24)
    text_lines = textwrap.wrap(DEFAULT_TEXT, width=16)

    y_text = 3
    x_text = 10
    line_height = 24
    for line in text_lines:
        width, height = font.getsize(line)
        # draw.text(((DEFAULT_DIMENSIONS[1] - width)/2, y_text), line,
        draw.text((x_text, y_text), line,
            font=font, fill=DEFAULT_FG_COLOR)
        y_text += line_height

    np_image = util.img_as_float(np.array(image))

    # Add blur and gaussian noise
    np_image = gaussian_filter(np_image, blur_sigma)
    if noise_sigma > 0.0:
        np_image = util.random_noise(np_image, mode='gaussian', var=(noise_sigma ** 2))

    np_image = np_image.clip(min=0.0, max=1.0)
    return np_image
Esempio n. 30
0
# X = x + dx
# Y = y + dy

transform = AffineTransform(translation=(
    -200, 0))  # (-200,0) are x and y coordinate, change it see the effect
warp_image = warp(img, transform, mode="wrap")  #mode parameter is optional
# mode= {'constant', 'edge', 'symmetric', 'reflect', 'wrap'}
#these are possible values of mode, you can try them and decide which one to use, default value for mode is constant
plt.subplot(1, 2, 1)
plt.title('original image')
plt.imshow(img)
plt.subplot(1, 2, 2)
plt.title('Wrap Shift')
plt.imshow(warp_image)

noisy_image = random_noise(img)

plt.subplot(1, 2, 1)
plt.title('original image')
plt.imshow(img)
plt.subplot(1, 2, 2)
plt.title('Image after adding noise')
plt.imshow(noisy_image)

blur_image = cv2.GaussianBlur(img, (11, 11), 0)

plt.subplot(1, 2, 1)
plt.title('original image')
plt.imshow(img)
plt.subplot(1, 2, 2)
plt.title('Blurry image')
Esempio n. 31
0
def add_noise(image):
    return random_noise(image)
                filter_ = np.sum(mult_) / np.sum(kernel)
                filtered_image[i,j] = filter_
            else:
                subm_ =  image[ i:kernel_size+i , j:kernel_size+j]
                median = mediana(subm_)
                filtered_image[i,j] = median
            
    return filtered_image


image = rgb2gray(data.rocket())
plt.figure()
plt.imshow(image)

#image_ruido = image + 2.4*image.std()*np.random.random(image.shape)
image_ruido = random_noise(image,mode="s&p", amount = 0.3)

filtered = filter_application(image_ruido,kernel_size=5,filter_type=2)


plt.figure()
plt.subplot(1,3,1)
plt.title("Original")
plt.imshow(image, cmap='gray')
plt.subplot(1,3,2)
plt.title("Noise")
plt.imshow(image_ruido, cmap='gray')
plt.subplot(1,3,3)
plt.title("Filtered")
plt.imshow(filtered, cmap='gray')
plt.show()
def add_gaussian(img, sigma):
    img = random_noise(img, mode='gaussian', mean=0, var=sigma)
    return (img * 255).astype(np.uint8)
Esempio n. 34
0
 def add_noise(self, method='gaussian'):
     self._img = random_noise(self._img)
Esempio n. 35
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: shielding
"""

from skimage import io, util

img_embedded = io.imread('嵌入水印的图像.jpg')
img_gaussian = util.random_noise(img_embedded, mode='gaussian', var=0.004)
io.imsave("0.004高斯.jpg", img_gaussian)
img_gaussian = util.random_noise(img_embedded, mode='gaussian', var=0.006)
io.imsave("0.006高斯.jpg", img_gaussian)
img_gaussian = util.random_noise(img_embedded, mode='gaussian', var=0.008)
io.imsave("0.008高斯.jpg", img_gaussian)
img_sp = util.random_noise(img_embedded, mode='s&p', amount=0.01)
io.imsave("0.01椒盐.jpg", img_sp)
img_sp = util.random_noise(img_embedded, mode='s&p', amount=0.02)
io.imsave("0.02椒盐.jpg", img_sp)
img_sp = util.random_noise(img_embedded, mode='s&p', amount=0.03)
io.imsave("0.03椒盐.jpg", img_sp)
# import data from scikit-image library
from skimage import data
# from matplotlib import pyplot
import matplotlib.pyplot as plt
# import utli from scikit-image library
from skimage import util

image = data.camera()

# add noise
# newImage = util.random_noise(image, mode='pepper')
# newImage = util.random_noise(image, mode='salt')
newImage = util.random_noise(image, mode='s&p')

# display image
plt.imshow(image, cmap='gray')
plt.title('before')
plt.show()
plt.imshow(newImage, cmap='gray')
plt.title('after')
plt.show()
# scipy.signal.medfilt2d

import matplotlib.pyplot as plt
from skimage import io
from skimage.util import random_noise
from skimage.filters import threshold_local
from skimage.morphology import disk
from skimage.filters import rank

plt.rcParams['font.size'] = 18

fig = plt.figure(figsize=(16, 9))
# ax1, ax2, ax3, ax4, ax5, ax6 = fig.subplots(2, 3)
f = io.imread("./img.tif")
f_sp = random_noise(f, mode='s&p', salt_vs_pepper=0.25, clip=True)
ax1 = fig.add_subplot(131)
ax1.axis('off')
ax1.imshow(f_sp, cmap='gray')
ax1.set_title("image with s&p noise")

f1 = rank.median(f_sp, disk(7))
ax2 = fig.add_subplot(132)
ax2.axis('off')
ax2.imshow(f1, cmap='gray')
ax2.set_title("median filter of size 7*7")

f2 = threshold_local(f_sp, 7, 'mean')
ax3 = fig.add_subplot(133)
ax3.axis('off')
ax3.imshow(f2, cmap='gray')
Esempio n. 38
0
def test_set_seed():
    seed = 42
    cam = camera()
    test = random_noise(cam, seed=seed)
    assert_array_equal(test, random_noise(cam, seed=seed))
Esempio n. 39
0
def hyperspectral_image_generator_jp2(files,
                                      shape_file,
                                      class_indices_column,
                                      batch_size=32,
                                      image_mean=None,
                                      rotation_range=0,
                                      shear_range=0,
                                      scale_range=1,
                                      transform_range=0,
                                      horizontal_flip=False,
                                      vertical_flip=False,
                                      crop_size=None,
                                      filling_mode='edge',
                                      speckle_noise=None):
    from rasterio.mask import mask
    from rasterio import open
    from shapely.geometry import box
    import geopandas as gpd
    import numpy as np
    from random import sample
    from image_functions import categorical_label_from_full_file_name, preprocessing_image_ms
    from keras.utils import to_categorical

    geometry_df = gpd.read_file(shape_file)
    centroids = geometry_df['geometry'].values
    class_indices = geometry_df[class_indices_column].values.astype(int)
    number_of_classes = class_indices.max()
    files_centroids = list(
        zip(files * len(centroids),
            list(centroids) * len(files),
            list(class_indices) * len(files)))
    while True:
        # select batch_size number of samples without replacement
        batch_files = sample(files_centroids, batch_size)
        # get one_hot_label

        batch_Y = []
        # array for images
        batch_X = []
        # loop over images of the current batch
        for idx, (rf, polycenter, label) in enumerate(batch_files):
            raster_file = open(rf)
            mask_polygon = box(
                max(
                    polycenter.coords.xy[0][0] -
                    raster_file.transform[0] * crop_size[0] * 2,
                    raster_file.bounds.left),
                max(
                    polycenter.coords.xy[1][0] -
                    raster_file.transform[4] * crop_size[1] * 2,
                    raster_file.bounds.bottom),
                min(
                    polycenter.coords.xy[0][0] +
                    raster_file.transform[0] * crop_size[0] * 2,
                    raster_file.bounds.right),
                min(
                    polycenter.coords.xy[1][0] +
                    raster_file.transform[4] * crop_size[1] * 2,
                    raster_file.bounds.top))
            image, out_transform = mask(raster_file,
                                        shapes=[mask_polygon],
                                        crop=True,
                                        all_touched=True)
            image = np.transpose(image, (1, 2, 0))
            if image_mean is not None:
                mean_std_data = np.loadtxt(image_mean, delimiter=',')
                image = preprocessing_image_ms(image.astype(np.float64),
                                               mean_std_data[0],
                                               mean_std_data[1])
            # process image
            image = augmentation_image_ms(image,
                                          rotation_range=rotation_range,
                                          shear_range=shear_range,
                                          scale_range=scale_range,
                                          transform_range=transform_range,
                                          horizontal_flip=horizontal_flip,
                                          vertical_flip=vertical_flip,
                                          warp_mode=filling_mode)
            if speckle_noise is not None:
                from skimage.util import random_noise
                image_max = np.max(np.abs(image), axis=(0, 1))
                image /= image_max

                image = random_noise(image, mode='speckle', var=speckle_noise)
                image *= image_max

            image = crop_image(image, crop_size)

            # put all together
            batch_X += [image]
            batch_Y += [to_categorical(label, num_classes=number_of_classes)]
        # convert lists to np.array
        X = np.array(batch_X)
        Y = np.array(batch_Y)

        yield (X, Y)
Esempio n. 40
0
def test_salt_p1():
    image = np.random.rand(2, 3)
    noisy = random_noise(image, mode='salt', amount=1)
    assert_array_equal(noisy, [[1, 1, 1], [1, 1, 1]])
Esempio n. 41
0
def SaltPepper_noise(image):
    return image + random_noise(image, mode="s&p")
Esempio n. 42
0
    fspecial('gaussian',[shape],[sigma])
    """
    m, n = [(ss - 1.) / 2. for ss in shape]
    y, x = np.ogrid[-m:m + 1, -n:n + 1]
    h = np.exp(-(x * x + y * y) / (2. * sigma * sigma))
    h[h < np.finfo(h.dtype).eps * h.max()] = 0
    sumh = h.sum()
    if sumh != 0:
        h /= sumh
    return h


img = cv2.normalize(
    cv2.imread("Lenna.jpg", cv2.IMREAD_GRAYSCALE).astype("float"), None, 0.0,
    1.0, cv2.NORM_MINMAX)
I = sut.random_noise(img, 'gaussian', mean=0.0, var=0.01)
method = 'pm2'
N = 8
K = 5
Delta = 0.25
sigma = 0.1
Sy, Sx = I.shape
for _ in range(1, N):
    if sigma > 0:
        Io = I
        g4 = style_gauss2D(sigma=sigma)
        I = cv2.filter2D(I, -1, g4, borderType=cv2.BORDER_REPLICATE)

    dn = [I[1, :], I[1:Sy - 1, :]] - I
    ds = [I[2:Sy, :], I[Sy, :]] - I
    de = [I[:, 2:Sy], I[:, Sx]] - I
Esempio n. 43
0
def gen3d(shape,
          r_range,
          h_range,
          n_ell=1,
          noise=False,
          label=False,
          ell_stats=False):
    '''
    3-dimensional image generator for hsi images

    :param shape:
    :param r_range:
    :param h_range:
    :param n_ell:
    :param noise:
    :return:
    '''

    data = np.zeros(shape)

    if noise:
        data = random_noise(data)
        data *= 200

    ells = []
    r1 = np.random.randint(r_range[0], r_range[1], n_ell)
    r2 = np.random.randint(r_range[0], r_range[1], n_ell)
    h = np.random.randint(h_range[0], h_range[1], n_ell)

    # assuming shape is 240, 200, 200
    # and r_range max val < 30
    # and h_range max val < 240
    # and 1 <= n_ell <= 6
    centers = [
        [120, 40, 40],
        [120, 40, 80],
        [120, 40, 120],
        [120, 80, 40],
        [120, 80, 80],
        [120, 80, 120],
        [120, 120, 40],
        [120, 120, 80],
        [120, 120, 120],
    ]

    for i in range(0, n_ell):
        ell = ellipsoid(h[i], r1[i], r2[i], levelset=True)
        ell *= -500
        ell += 500
        # c = centers[i]
        # xL = c[1] - r1[i] - 3
        # xR = c[1] + r1[i] - 3
        # yL = c[1] - r2[i] - 3
        # yR = c[1] + r2[i] - 3
        # bL = c[0] - h[i]
        # bR = c[0] + h[i]
        # data[bL:bR, xL:xR, yL:yR] += ell
        # ells.append(ell)
        data = add_ell(data, ell)

    if ell_stats:
        stats = []
        for i in range(0, n_ell):
            stat = ellipsoid_stats(h[i], r1[i], r2[i])
            stats.append(stat)

    return data
Esempio n. 44
0
def test_set_seed():
    seed = 42
    cam = camera()
    test = random_noise(cam, seed=seed)
    assert_array_equal(test, random_noise(cam, seed=seed))
def noise(image):
    return random_noise(image)
def greit_rec(p, t, el_pos, anomaly, fwd, continuous=False, step='random', n_pix=64, n_el=20, length=None, el_dist=None, num_per_el=3, continuousPerm=None):
    '''
    function that generates forward solution and a GREIT reconstruction for a given conductivity distribution
    
    Takes:
    p - array of point coordinates created by meshing algorithm [number of points, 2] array[float]
    t - array of a triangles included in meshing [number of triangles , 3] array[int]
    el_pos - array of electrode positions [number of electrodes , 2] array[float]
    anomaly - list of all the anomalies for which reconstruction should be made array[dict]
    step - array storing the distance (in number of electrodes) for each source/sink pair [number of source/sink pairs , 1] array[int]
    (Default is 'random' == generates random step for each measurement)
    n_pix - number of pixels to be created in each dimension for GREIT algorithm [int]
    n_el - number of electrodes of the system that participate in reconstruction [int]

    Returns:
    ds - recreated conductivity map by GREIT algorithm [number of pixels,number of pixels] array[float]
    '''
    #check order of points for each triangle and rearrange to ensure counter-clockwise arrangement (from pyEIT)
    t = checkOrder(p, t)
    #generate electrode indices
    el_pos = np.arange(n_el * num_per_el)
    #initialise unit uniform permitivity for the triangular mesh to be used
    perm = np.ones(t.shape[0], dtype=np.float)
    #build mesh structure given the input elements
    mesh_obj = {'element': t,
                'node': p,
                'perm': perm}

    # extract x, y coordinate of mesh vertices
    x, y = p[:, 0], p[:, 1]
    #check to see the state of the generated mesh (optional)
    #quality.stats(p, t)

    ex_mat = orderedExMat(n_el, length, el_dist)
    #if random step is invoked a random step is generated for each source/sink pair
    if step == 'random':
        step = np.array(rand.randint(1, n_el, size=ex_mat.shape[0]))
    #if the step is not an integer in the needed range, just randomize the same step for a source/sink pairs
    elif (step < 0 or step > n_el).any():
        step = rand.randint(1, n_el)
    start_t = time()
    # calculate simulated data using FEM (for uniform conductivity)
    f = fwd.solve_eit(ex_mat=ex_mat, step=step, perm=mesh_obj['perm'])
    # creating forward dictionary with solution of forward problem
    pde_result = namedtuple("pde_result", ['jac', 'v', 'b_matrix'])
    f0 = pde_result(jac=f.jac,
                    v=f.v,
                    b_matrix=f.b_matrix)
    # adding anomalies with a overwritten anomaly function originally in pyEIT (pyEIT.mesh.wrapper) (heavily changed)
    unp_t = time()
    print("Unperturbed forward solution t", unp_t - start_t)
    if continuous == False:
        mesh_new = set_perm(mesh_obj, anomaly=anomaly, background=None)
        permUsed = mesh_new['perm']
    elif continuous == True:
        permUsed = continuousPerm
    else:
        print('kurec')
    start_anom = time()
    # solving with anomalies
    f1 = fwd.solve_eit(ex_mat=ex_mat, step=step, perm=permUsed)
    # generate array for variance, 3% standard deviation for the voltage measurement
    variance = 0.0009 * np.power(f1.v, 2)
    # apply noise to voltage measurements v
    # here white Gaussian noise is assumed (since we don't have significant sources of systematic error like in medical applications, e.g. moving electrodes...)
    v = sk.random_noise(f1.v, 
                        mode='gaussian', 
                        clip=False, 
                    	mean=0.0, 
                        var=variance)
    # create the Forward object used by GREIT the voltage map with the Gaussian noise
    f1 = pde_result(jac=np.vstack(f1.jac),
                    v=np.hstack(v),
                    b_matrix=np.vstack(f1.b_matrix))
    end_anom = time()
    print("Anomaly forward t: ", end_anom - start_anom )
    # (optional) draw anomalies only
    
    delta_perm = np.real(mesh_new['perm'] - mesh_obj['perm'])
    fig, ax = plt.subplots()
    im = ax.tripcolor(p[:, 0], p[:, 1], t, delta_perm,
                      shading='flat', cmap=plt.cm.viridis)
    ax.set_title(r'$\Delta$ Conductivities')
    fig.colorbar(im)
    ax.axis('equal')
    fig.set_size_inches(6, 4)
    # fig.savefig('demo_bp_0.png', dpi=96)
    #plt.show()
    
    start_greit = time()
    # constructing GREIT object (from pyEIT) (classes changed singificantly for optimisation)
    eit = greit.GREIT(mesh_obj, el_pos, f=f, ex_mat=ex_mat, step=step, parser='std')
    # solving inverse problem with GREIT algorithm
    eit.setup(p=0.1, lamb=0.01, n=n_pix, s=15., ratio=0.1)
    ds = eit.solve(f1.v, f0.v)
    #reshaping output to the desired dimensions
    ds = ds.reshape((n_pix, n_pix))
    print("Greit solution time ", time() - start_greit)
    # (optional) plot to check whether generated sensibly
    
    fig, ax = plt.subplots(figsize=(6, 4))
    im = ax.imshow(np.real(ds), interpolation='none', cmap=plt.cm.viridis, origin='lower', extent=[-1,1,-1,1])
    fig.colorbar(im)
    ax.axis('equal')
    
    plt.show()
    '''
    gradConductivity = np.linalg.norm(np.gradient(ds), axis=0)
    figGrad, axGrad = plt.subplots(figsize=(6, 4))
    imGrad = axGrad.imshow(np.real(gradConductivity), interpolation='none', cmap=plt.cm.viridis, origin='lower', extent=[-1,1,-1,1])
    figGrad.colorbar(imGrad)
    axGrad.axis('equal')

    figGrad2, axGrad2 = plt.subplots(figsize=(6, 4))
    imGrad2 = axGrad2.imshow(np.real(gradConductivity * ds), interpolation='none', cmap=plt.cm.viridis, origin='lower', extent=[-1,1,-1,1])
    figGrad2.colorbar(imGrad2)
    axGrad2.axis('equal')


    v_pert = np.empty(shape=(len(f1.v), len(f1.v)))
    perturbing_mat = np.ones((len(f1.v), len(f1.v))) + 0.05 * np.identity(len(f1.v))
    v_pert[:] = np.dot(perturbing_mat, np.diag(f1.v))
    influence_mat = -np.dot(eit.H, v_pert).reshape(n_pix, n_pix, len(f1.v)) - ds[:, :, None]
    influence_mat = np.absolute(influence_mat)
    influence_mat = np.sum(influence_mat, axis=2)
    
    #mask = circleMask(npix, a)
    #influence_mat[~mask] = np.amax(influence_mat)

    figInfl, axInfl = plt.subplots(figsize=(6, 4))
    imInfl = axInfl.imshow(np.real(influence_mat), interpolation='none', cmap=plt.cm.viridis, origin='lower', extent=[-1,1,-1,1])
    figInfl.colorbar(imInfl)
    axInfl.axis('equal')

    totalMap = gradConductivity * ds * influence_mat
    figTotal, axTotal = plt.subplots(figsize=(6, 4))
    imTotal = axTotal.imshow(np.real(totalMap), interpolation='none', cmap=plt.cm.viridis, origin='lower', extent=[-1,1,-1,1])
    figTotal.colorbar(imTotal)
    axTotal.axis('equal')
    plt.show()
    '''
    return ds
Esempio n. 47
0
def test_bad_mode():
    data = np.zeros((64, 64))
    with testing.raises(KeyError):
        random_noise(data, 'perlin')
Esempio n. 48
0
from skimage.exposure import rescale_intensity
from skimage.util import random_noise
import mrcfile
import argparse
import numpy as np

parser = argparse.ArgumentParser()
parser.add_argument('--SNR',
                    type=float,
                    default=0.4,
                    help='siganl noise ration')
parser.add_argument('--name', type=str, default='train', help='train or test')
opt = parser.parse_args()

data = mrcfile.open('./data/' + opt.name + '_clean.mrc').data
with mrcfile.new('./data/' + opt.name + '_SNR' + str(opt.SNR) +
                 '_Gaussian.mrc',
                 overwrite=True) as mrc:
    mrc.set_data(
        np.zeros((data.shape[0], data.shape[1], data.shape[2]),
                 dtype=np.float32))
    for k in range(data.shape[0]):
        im = data[k]
        im = rescale_intensity(1.0 * im, out_range=(0, 1))
        im1 = random_noise(im, mode='gaussian', var=im.var() / opt.SNR)
        mrc.data[k, :, :] = im1
        print(k)
plt.imshow(im1, cmap='gray')
plt.show()
print(np.var(im) / np.var(im1 - im))
Esempio n. 49
0
def test_singleton_dim():
    """Ensure images where size of a given dimension is 1 work correctly."""
    image = np.random.rand(1, 20)
    noisy = random_noise(image, mode='salt', amount=0.1, seed=42)
    assert np.sum(noisy == 1) == 2
Esempio n. 50
0
import matplotlib.pyplot as plt
import skimage.morphology as morph

for root, dirs, files in os.walk('noiseless'):
    for filename in files:
        print(filename)
        if filename == '.DS_Store':
            continue

        I = util.img_as_float(io.imread(os.path.join(root, filename)))

        #io.imsave(os.path.join(root, filename[:-4]+'.png'), I)

        spikenoise = util.random_noise(np.zeros(I.shape[0:2]),
                                       mode="s&p",
                                       amount=.02,
                                       salt_vs_pepper=1.0)
        # sigma = rand.uniform(0, 1)
        # G = matlab_style_gauss2D(sigma=sigma,shape=(9,9))
        # G = G / np.max(G)
        #spikenoise = nd.filters.correlate(spikenoise, G)

        I_gray = color.rgb2gray(I)
        spikenoise = spikenoise * (I_gray < .5)
        spikenoise = color.gray2rgb(spikenoise)

        noisy = spikenoise + I
        noisy = util.random_noise(noisy,
                                  'gaussian',
                                  var=rand.uniform(0.001, 0.005))
Esempio n. 51
0
def hyperspectral_image_generator(files,
                                  class_indices,
                                  batch_size=32,
                                  image_mean=None,
                                  rotation_range=0,
                                  shear_range=0,
                                  scale_range=1,
                                  transform_range=0,
                                  horizontal_flip=False,
                                  vertical_flip=False,
                                  crop=False,
                                  crop_size=None,
                                  filling_mode='edge',
                                  speckle_noise=None):
    from skimage.io import imread
    import numpy as np
    from random import sample
    from image_functions import categorical_label_from_full_file_name, preprocessing_image_ms

    while True:
        # select batch_size number of samples without replacement
        batch_files = sample(files, batch_size)
        # get one_hot_label
        batch_Y = categorical_label_from_full_file_name(
            batch_files, class_indices)
        # array for images
        batch_X = []
        # loop over images of the current batch
        for idx, input_path in enumerate(batch_files):
            image = np.array(imread(input_path), dtype=float)
            if image_mean is not None:
                mean_std_data = np.loadtxt(image_mean, delimiter=',')
                image = preprocessing_image_ms(image, mean_std_data[0],
                                               mean_std_data[1])
            # process image
            image = augmentation_image_ms(image,
                                          rotation_range=rotation_range,
                                          shear_range=shear_range,
                                          scale_range=scale_range,
                                          transform_range=transform_range,
                                          horizontal_flip=horizontal_flip,
                                          vertical_flip=vertical_flip,
                                          warp_mode=filling_mode)
            if speckle_noise is not None:
                from skimage.util import random_noise
                image_max = np.max(np.abs(image), axis=(0, 1))
                image /= image_max

                image = random_noise(image, mode='speckle', var=speckle_noise)
                image *= image_max

            if crop:
                if crop_size is None:
                    crop_size = image.shape[0:2]
                image = crop_image(image, crop_size)
            # put all together
            batch_X += [image]
        # convert lists to np.array
        X = np.array(batch_X)
        Y = np.array(batch_Y)

        yield (X, Y)
Esempio n. 52
0
def add_gaussian_noise(image, *args, **kwargs):
    image = image_as_square(image)
    noisy = random_noise(image, *args, **kwargs)
    noisy = np.multiply(noisy, 255).astype(np.uint8)
    return image_as_array(noisy)
Esempio n. 53
0
 def noise(self, img):
     return random_noise(img, mode='gaussian', clip=True) * 255
Esempio n. 54
0
 def Add_gaussian_noise(self, mode='gaussian'):
     ##mode : 'gaussian' ,'salt' , 'pepper '
     noise_image = random_noise(self.image, mode=mode)
     return noise_image
Esempio n. 55
0
 def process(self, img):
     return random_noise(img, mode='gaussian', var=self.var)
Esempio n. 56
0
 def execute(self, image_array: ndarray):
     noise_mode = random.choice(
         ['gaussian', 'poisson', 'speckle', 'salt', 'pepper', 's&p'])
     return random_noise(image_array, noise_mode, seed=42)
Esempio n. 57
0
from PIL import Image
import numpy as np
from skimage.util import random_noise

im = Image.open("horizontal.png")
# convert PIL Image to ndarray
im_arr = np.asarray(im)

# random_noise() method will convert image in [0, 255] to [0, 1.0],
# inherently it use np.random.normal() to create normal distribution
# and adds the generated noised back to image
noise_img = random_noise(im_arr, mode='gaussian', var=0.05**2)
noise_img = (255*noise_img).astype(np.uint8)

img = Image.fromarray(noise_img)
img.show()
Esempio n. 58
0
import numpy as np
import matplotlib.pyplot as plt
from skimage.exposure import rescale_intensity
from skimage.util import random_noise

####transfer pgm to jpg
load_path = './data/pgm'
save_path = './data/jpg'
listpath = os.listdir(load_path)
for filename in np.sort(listpath):
    im = Image.open(os.path.join(load_path, filename))
    filename_new = filename[:-4] + '.jpg'
    im.save(os.path.join(save_path, filename_new))


#### add gaussian noise
clean_img = []
noisy_img = []
SNR = 1   # signal noise ratio
num = 200
listpath = os.listdir(save_path)
for filename in np.sort(listpath):
    im = plt.imread(os.path.join(save_path, filename))
    im = rescale_intensity(1.0 * im, out_range=(0, 1))
    print(im.shape)
    for i in range(num):
        clean_img.append(im)
        noisy_img.append(random_noise(im, mode='gaussian', var=im.var() / SNR))   ###add gaussian noise, here SNR = 1
np.save('./data/clean_img.npy', clean_img)
np.save('./data/noisy_img_SNR' + str(SNR) + '.npy', noisy_img)
Esempio n. 59
0
 def _addNoise(self, img):
     '''
     :param img: img array
     :return: img array with noise
     '''
     return random_noise(img, mode='gaussian', clip=True) * 255
Esempio n. 60
0
    running_loss = 0.0

    dt = datetime.datetime.now()
    print(f"[{dt.hour}:{dt.minute}:{dt.second}], Running Epoch {epoch}")

    for batch_data in (train_dataloader):
        # get the inputs; data is a list of [lr_image, hr_image]
        lr_image, hr_image = batch_data
        lr_image = lr_image.float().to(device)

        if MODE == "BD":
            blur_HR = GaussianBlur(hr_image.cpu().numpy(), (7, 7), 1.6)
            blur_HR = torch.from_numpy(blur_HR).float().to(device)
            blur_HR = torch.transpose(blur_HR, 3, 1)
        elif MODE == "DN":
            noisy_HR = random_noise(hr_image)
            noisy_HR = torch.from_numpy(noisy_HR).float().to(device)
            noisy_HR = torch.transpose(noisy_HR, 3, 1)

        hr_image = hr_image.to(device)

        lr_image = torch.transpose(lr_image, 3, 1)
        hr_image = torch.transpose(hr_image, 3, 1)

        # zero the parameter gradients
        optimizer.zero_grad()

        # forward + backward + optimize
        output = model(lr_image)
        curr_loss = 0.0
        for idx, image in enumerate(output):