Newer
Older
"""A file containing some useful functions for the project.
This file should NOT be modified.
"""
from skimage.metrics import peak_signal_noise_ratio, structural_similarity
from skimage.io import imread, imsave
import numpy as np
from src.checks import check_data_range, check_rgb, check_shape, check_path, check_png
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
def normalise_image(img: np.ndarray) -> np.ndarray:
"""Normalise the values of img in the interval [0, 1].
Args:
img (np.ndarray): Image to normalise.
Returns:
np.ndarray: Normalised image.
"""
return (img - np.min(img)) / np.ptp(img)
def load_image(file_path: str) -> np.ndarray:
"""Loads the image located in file_path.
Args:
file_path (str): Path of the file containing the image. Must end by '.png'.
Returns:
np.ndarray: The loaded image.
"""
check_path(file_path)
check_png(file_path)
return normalise_image(imread(file_path))
def save_image(file_path: str, img: np.ndarray) -> None:
"""Saves the image located in file_path.
Args:
file_path (str): Path of the file in which the image will be saved. Must end by '.png'.
img (np.ndarray): Image to save.
"""
check_path(file_path.split('/')[-2])
check_png(file_path)
imsave(file_path, (img * 255).astype(np.uint8))
def psnr(img1: np.ndarray, img2: np.ndarray) -> float:
"""Computes the PSNR between img1 and img2 after some sanity checks.
img1 and img2 must:
- have the same shape;
- be in range [0, 1].
Args:
img1 (np.ndarray): First image.
img2 (np.ndarray): Second image.
Returns:
float: PSNR between img1 and img2.
"""
check_shape(img1, img2)
check_data_range(img1)
return peak_signal_noise_ratio(img1, img2, data_range=1)
def ssim(img1: np.ndarray, img2: np.ndarray) -> float:
"""Computes the SSIM between img1 and img2 after some sanity checks.
img1 and img2 must:
- have the same shape;
- be in range [0, 1];
- be 3 dimensional array with 3 channels.
Args:
img1 (np.ndarray): First image.
img2 (np.ndarray): Second image.
Returns:
float: SSIM between img1 and img2.
"""
check_shape(img1, img2)
check_data_range(img1)
check_rgb(img1)
return structural_similarity(img1, img2, data_range=1, channel_axis=2)
####
####
####
#### #### #### #############
#### ###### #### ##################
#### ######## #### ####################
#### ########## #### #### ########
#### ############ #### #### ####
#### #### ######## #### #### ####
#### #### ######## #### #### ####
#### #### ######## #### #### ####
#### #### ## ###### #### #### ######
#### #### #### ## #### #### ############
#### #### ###### #### #### ##########
#### #### ########## #### #### ########
#### #### ######## #### ####
#### #### ############ ####
#### #### ########## ####
#### #### ######## ####
#### #### ###### ####
# 2023
# Authors: Mauro Dalla Mura and Matthieu Muller