python(denoising): Image denoising
Signed-off-by: prescientmoon <git@moonythm.dev>
This commit is contained in:
parent
f1719e9375
commit
4fb27b6826
File diff suppressed because one or more lines are too long
|
@ -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 {
|
||||
|
|
BIN
python/denoising/images/rick.jpg
Normal file
BIN
python/denoising/images/rick.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 135 KiB |
BIN
python/denoising/images/rick_gray.jpg
Normal file
BIN
python/denoising/images/rick_gray.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 115 KiB |
BIN
python/denoising/images/rick_gray_noisy.jpg
Normal file
BIN
python/denoising/images/rick_gray_noisy.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.2 MiB |
BIN
python/denoising/images/sonic.jpg
Normal file
BIN
python/denoising/images/sonic.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 49 KiB |
BIN
python/denoising/images/sonic_gray.jpg
Normal file
BIN
python/denoising/images/sonic_gray.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 43 KiB |
BIN
python/denoising/images/sonic_gray_noisy.jpg
Normal file
BIN
python/denoising/images/sonic_gray_noisy.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 252 KiB |
46
python/denoising/modules/image_tools.py
Normal file
46
python/denoising/modules/image_tools.py
Normal 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()
|
|
@ -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))
|
||||
|
|
Loading…
Reference in a new issue