Testing Class Material

This commit is contained in:
Lisa Schwetlick 2024-08-26 13:54:13 +02:00
commit 05b1f6cdd5
85 changed files with 102796 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 415 KiB

View file

@ -0,0 +1,22 @@
import os.path
import matplotlib.pyplot as plt
from scipy.ndimage import imread
from pkg_resources import resource_filename
from noiser.noise import white_noise
from noiser.utils import copy_image
def main():
path = resource_filename('noiser', os.path.join('images', 'baboon_kandinsky.png'))
print(path)
img = imread(path)
noisy = copy_image(white_noise(img, 20))
plt.imshow(noisy)
plt.draw()
plt.show()
if __name__ == '__main__':
main()

View file

@ -0,0 +1,7 @@
import numpy as np
def white_noise(image, std):
noise = np.random.normal(scale=std, size=image.shape).astype(image.dtype)
noisy = image + noise
return noisy

View file

@ -0,0 +1,8 @@
from setuptools import setup, find_packages
setup(
name='Noiser',
version='1.0',
packages=find_packages(),
)

View file

@ -0,0 +1,24 @@
import numpy as np
from numpy.testing import assert_allclose
from noiser.noise import white_noise
def test_white_noise():
n_images, height, width = 201, 101, 102
dtype = np.float32
# Create ``n_images`` identical image.
base_image = np.random.rand(1, height, width, 3).astype(dtype) - 0.5
images = np.repeat(base_image, n_images, axis=0)
std = 0.13
noisy = white_noise(images, std=std)
# dtype and shape are preserved.
assert noisy.dtype == dtype
assert noisy.shape == images.shape
# Mean and std of noisy image match expectations.
assert_allclose(images.mean(0), base_image[0], atol=1e-4)
assert np.isclose((noisy - images).std(), std, atol=1e-4)

View file

@ -0,0 +1,14 @@
import numpy as np
from numpy.testing import assert_array_equal
from noiser.utils import copy_image
def test_copy_image():
height, width = 101, 102
dtype = np.float32
image = np.random.rand(height, width, 3).astype(dtype)
copy = copy_image(image)
assert_array_equal(copy, image)

View file

@ -0,0 +1,15 @@
import numpy as np
cimport numpy as np
def copy_image(np.ndarray img):
cdef int h = img.shape[0]
cdef int w = img.shape[1]
cdef int c = img.shape[2]
cdef np.ndarray copy = np.empty([h, w, c], dtype=img.dtype)
for i in range(h):
for j in range(w):
for k in range(c):
copy[i, j, k] = img[i, j, k]
return copy