-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnisetropic_lib_solver.py
More file actions
72 lines (62 loc) · 2.46 KB
/
Anisetropic_lib_solver.py
File metadata and controls
72 lines (62 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import numpy as np
import cv2
import matplotlib.pyplot as plt
from medpy.filter.smoothing import anisotropic_diffusion
from skimage.measure import compare_ssim as ssim
import os
img = np.random.uniform(size=(32,32))
im = cv2.imread("images/Apple.jpg")
im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)# Changing the order of the color channel to RGB to get
#a red apple rather than a blue one
xs = im.copy()
def prepare_to_plot(xs):
"""
:param xs: input array of the image
:return: Optimized array within the range of 0 to 255
"""
ranged = np.amax(xs) - np.amin(xs)
return ((xs + np.amin(xs)) / ranged * 255).astype(int)
def add_noise(xs):
"""
A method used to add noise to an image
:param xs: input array of the original image to be added noise to
:return: An array corresponding to a noisy image
"""
noise = np.random.randint(-30, 30, xs.shape)
xs = xs + noise
return prepare_to_plot(xs)
def filter(xs):
"""
Method to filter noise out of an image.
:param xs: an array corresponding to a noisy image
:return: An array corresponding to a cleaner image
"""
img_filtered = anisotropic_diffusion(xs, gamma = 0.1, kappa = 40, niter = 20, option=1)
return prepare_to_plot(img_filtered)
def plot_images(im, noisy_im):
"""
Method used to display a panel of images before and after noise removal
:param xs: An array corresponding to a clean image to be imported
:return: A panel of the original image, image with noise added, and an image with the noise filtered
"""
xs = im.copy()
noisy_image = noisy_im.copy()
filtered_image = filter(noisy_image)
original_image = prepare_to_plot(xs)
#ssim calculates the structural similarity index between the noisy and the filtered image. The less the similarity index, the cleaner the filtered image
ssim_noise = ssim(noisy_image, filtered_image,
multichannel=True,data_range=filtered_image.max()
- filtered_image.min())
print(ssim_noise)
plt.subplot(1, 3, 1)
plt.imshow( original_image)
plt.xlabel("Original Image")
plt.subplot(1, 3, 2)
plt.imshow(noisy_image)
plt.xlabel("Original Image + Noise")
plt.subplot(1, 3, 3)
plt.imshow(filtered_image)
plt.xlabel("Filtered Image")
plt.savefig(os.path.join('Results', 'at_lib_solver.png'),format='png')
noisy_im = add_noise(xs)
plot_images(xs, noisy_im)