1
Fork 0

python(denoising): Image denoising

Signed-off-by: prescientmoon <git@moonythm.dev>
This commit is contained in:
Matei Adriel 2023-03-29 22:12:50 +02:00 committed by prescientmoon
parent f1719e9375
commit 4fb27b6826
Signed by: prescientmoon
SSH key fingerprint: SHA256:UUF9JT2s8Xfyv76b8ZuVL7XrmimH4o49p4b+iexbVH4
12 changed files with 295 additions and 36 deletions

File diff suppressed because one or more lines are too long

View file

@ -9,7 +9,7 @@
(system:
let
pkgs = nixpkgs.legacyPackages.${system};
pyDeps = p: with p; [ numpy scipy matplotlib ];
pyDeps = p: with p; [ numpy scipy matplotlib pillow ];
in
rec {
devShell = pkgs.mkShell {

Binary file not shown.

After

Width:  |  Height:  |  Size: 135 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 115 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 252 KiB

View file

@ -0,0 +1,46 @@
"""Project 2 tools for generating a noisy image"""
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image # PIL is an image processing module
def create_noisy_image(path="/content/drive/My Drive/", file="sonic.jpg"):
"""Transform color image to a noisy grayscale image.
Plot and save the transformed images.
The file sonic.jpg must be located in your google drive and the drive must have been
"mounted" with:
from google.colab import drive
drive.mount("/content/drive")
"""
image = Image.open(path + file)
# Transform RGB image to grayscale
image = image.convert("L")
# get the filename without the extension
# (note: the pathlib module offers better ways to do this!)
name = file.removesuffix(".jpg")
image.save(path + name + "_gray.jpg")
# set images to be plotted in gray scale
plt.gray()
plt.imshow(image)
plt.title("Original image")
plt.show()
# add noise to grayscale image
sigma = 2.0
img_arr = np.array(image)
img_arr_noisy = np.clip(
img_arr + sigma * np.random.normal(0, 100, img_arr.shape), 0, 255
).astype(np.uint8)
plt.title("Noisy image")
plt.imshow(img_arr_noisy)
# create Image object from numpy array and save to disk
image = Image.fromarray(img_arr_noisy, mode="L")
image.save(path + name + "_gray_noisy.jpg")
plt.show()

View file

@ -43,23 +43,7 @@ def to_csr(a, c, e):
"""
assert_valid_tridiagonal(a, c, e)
n = len(c)
values = np.zeros(n * 3 + 1)
values[::3] = a
values[1::3] = c
values[2::3] = e
col_indices = np.zeros_like(values)
col_indices[1::3] = np.arange(1, n + 1)
col_indices[2::3] = np.arange(0, n)
col_indices[3::3] = np.arange(1, n + 1)
index_ptr = np.zeros(n + 2)
index_ptr[1:n+1] = np.arange(2, n * 3 + 2, 3)
index_ptr[n+1] = n * 3 + 1
return sp.sparse.csr_array((values, col_indices, index_ptr))
return sp.sparse.diags((a, c, e), offsets=(0, 1, -1), format="csr")
def to_array(a, c, e):
"""
@ -230,6 +214,12 @@ def scale(s, m):
return result
def transpose(a, c, e):
"""
Computes the transpose of a tridiagonal matrix.
"""
return create(a, e, c)
# Small sanity check for the above code
def main():
a, c, e = create(3*np.ones(4), 2*np.ones(3), 3*np.ones(3))