forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrichardson_lucy.py
241 lines (191 loc) · 8.3 KB
/
richardson_lucy.py
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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
"""
https://en.wikipedia.org/wiki/Motion_blur
https://en.wikipedia.org/wiki/Image_noise
https://en.wikipedia.org/wiki/Richardson-Lucy_deconvolution
"""
import imageio.v2 as imageio
import numpy as np
from numpy.fft import fft2, ifft2, ifftshift
def root_mean_square_error(original: np.ndarray, reference: np.ndarray) -> float:
"""Simple implementation of Root Mean Squared Error
for two N dimensional numpy arrays.
Examples:
>>> root_mean_square_error(np.array([1, 2, 3]), np.array([1, 2, 3]))
0.0
>>> root_mean_square_error(np.array([1, 2, 3]), np.array([2, 2, 2]))
0.816496580927726
>>> root_mean_square_error(np.array([1, 2, 3]), np.array([6, 4, 2]))
3.1622776601683795
"""
return np.sqrt(((original - reference) ** 2).mean())
def pad_to_size(image: np.ndarray, reference: np.ndarray):
"""Pad an image to have final shape equal to reference image."""
p, q = (size for size in reference.shape)
difference = (
(p - image.shape[0]) // 2,
(q - image.shape[1]) // 2 + (q - image.shape[1]) % 2,
)
return np.pad(image, difference, mode="constant")
def normalize_image(
image: np.ndarray, cap: float = 255.0, data_type: np.dtype = np.uint8
) -> np.ndarray:
"""
Normalizes image in Numpy 2D array format, between ranges 0-cap,
as to fit uint8 type.
Args:
image: 2D numpy array representing image as matrix, with values in any range
cap: Maximum cap amount for normalization
data_type: numpy data type to set output variable to
Returns:
return 2D numpy array of type uint8, corresponding to limited range matrix
Examples:
>>> normalize_image(np.array([[1, 2, 3], [4, 5, 10]]),
... cap=1.0, data_type=np.float64)
array([[0. , 0.11111111, 0.22222222],
[0.33333333, 0.44444444, 1. ]])
>>> normalize_image(np.array([[4, 4, 3], [1, 7, 2]]))
array([[127, 127, 85],
[ 0, 255, 42]], dtype=uint8)
"""
normalized = (image - np.min(image)) / (np.max(image) - np.min(image)) * cap
return normalized.astype(data_type)
def gaussian_noise(size: tuple, mean=0, std=0.05):
"""Creates normal distribution array with given size to use as noise.
Args:
size: Size of the desired output image
mean: Mean to use within the Gaussian Function
std: Standard deviation to use within the Gaussian Function
Returns:
Matrix with given size, containing generated gaussian values.
Example:
>>> np.random.seed(0)
>>> gaussian_noise((5, 5))
array([[ 22.49166741, 5.10200441, 12.4789093 , 28.57138829,
23.81136437],
[-12.46029297, 12.11362732, -1.92980441, -1.31604036,
5.2351309 ],
[ 1.83655553, 18.54198721, 9.703231 , 1.55135646,
5.65925622],
[ 4.25434767, 19.04950818, -2.61576786, 3.9916132 ,
-10.88972068],
[-32.55062015, 8.33363709, 11.02156154, -9.46260401,
28.93937146]])
"""
noise = np.multiply(np.random.normal(mean, std, size), 255)
return noise
def gaussian_filter(k: int = 5, sigma: float = 1.0) -> np.ndarray:
"""Generates a matrix with weights corresponding to centered gaussian distribution.
Args:
k: Lateral size of the kernel.
sigma: Standard deviation to be used when generating distribution
Returns:
np.ndarray: [k x k] sized kernel to be used as filter
Examples:
>>> gaussian_filter()
array([[0.00296902, 0.01330621, 0.02193823, 0.01330621, 0.00296902],
[0.01330621, 0.0596343 , 0.09832033, 0.0596343 , 0.01330621],
[0.02193823, 0.09832033, 0.16210282, 0.09832033, 0.02193823],
[0.01330621, 0.0596343 , 0.09832033, 0.0596343 , 0.01330621],
[0.00296902, 0.01330621, 0.02193823, 0.01330621, 0.00296902]])
"""
arx = np.arange((-k // 2) + 1.0, (k // 2) + 1.0)
x, y = np.meshgrid(arx, arx)
filt = np.exp(-(1 / 2) * (np.square(x) + np.square(y)) / np.square(sigma))
return filt / np.sum(filt)
def convolve(matrix: np.ndarray, kernel: np.ndarray) -> np.ndarray:
"""
Convolves a given kernel around a matrix through the frequency domain,
using Fourier transformations.
Args:
matrix: Numpy array containing values to be convolved
kernel: Kernel (with all dimensions smaller than those of the matrix)
with weights to apply to each pixel.
Returns:
np.ndarray: Final equally shaped matrix with convoluted pixels.
Examples:
>>> matrix = np.array([[-1.45436567, 0.04575852],
... [-0.18718385, 1.53277921]])
>>> kernel = np.array([[1, 0], [0, 1]])
>>> convolve(matrix, kernel)
array([[ 0.07841354, -0.14142533],
[-0.14142533, 0.07841354]])
"""
if kernel.shape[0] > matrix.shape[1] or kernel.shape[1] > matrix.shape[1]:
return matrix
kernel = pad_to_size(kernel, matrix)
kernel_f = fft2(kernel)
matrix_f = fft2(matrix)
return ifftshift(ifft2(np.multiply(matrix_f, kernel_f))).real
def get_motion_psf(shape: tuple, angle: float, num_pixel_dist: int = 20) -> np.ndarray:
"""
Generate an array with given shape corresponding to
Point Spread Function for the desired angle.
Args:
shape: The shape of the image.
angle: The angle of the motion blur. Should be in degrees. [0, 360)
num_pixel_dist: The distance of the motion blur. [0, infinity)
Remember that the distance is measured in pixels.
Greater values will be more blurry
Returns:
np.ndarray: The point-spread array associated with the motion blur.
Examples:
>>> shape = (3, 3)
>>> angle = 15
>>> get_motion_psf(shape, angle, num_pixel_dist=3)
array([[0. , 0.33333333, 0. ],
[0. , 0.33333333, 0. ],
[0.33333333, 0. , 0. ]])
"""
psf = np.zeros(shape)
center = np.array([shape[0] - 1, shape[1] - 1]) // 2
radians = angle / 180 * np.pi
phase = np.array([np.cos(radians), np.sin(radians)])
for i in range(num_pixel_dist):
offset_x = int(center[0] - np.round_(i * phase[0]))
offset_y = int(center[1] - np.round_(i * phase[1]))
psf[offset_x, offset_y] = 1
psf /= psf.sum()
return psf
def richardson_lucy(
degraded: np.ndarray, function_kernel: np.ndarray, steps: int
) -> np.ndarray:
"""
Richardson-Lucy method to restore an image
affected by known motion blur function, as well as arbitrary steps
to perform during iterative image restoration.
Args:
degraded: Observed image, considered to be degraded - to be restored
function_kernel: Supposed function used to blur original image
steps: Iterative steps to take for restoring image
Returns:
np.ndarray: Restored image after method application
Examples:
>>> np.random.seed(0)
>>> shape = (3, 3)
>>> degraded = gaussian_noise(shape)
>>> kernel = np.identity(2)
>>> richardson_lucy(degraded, kernel, steps=4)
array([[1.16272775e+02, 0.00000000e+00, 7.58091075e+01],
[2.40582123e+01, 2.09006034e+01, 0.00000000e+00],
[0.00000000e+00, 1.52620152e-02, 1.31734948e+00]])
"""
estimated_img = np.full(shape=degraded.shape, fill_value=1, dtype="float64")
for i in range(steps):
dividend = convolve(estimated_img, function_kernel)
quotient = np.divide(degraded, dividend, where=dividend != 0)
estimated_img = np.multiply(
estimated_img, convolve(quotient, np.flip(function_kernel))
)
estimated_img = np.clip(estimated_img, 0, 255)
return estimated_img
def main():
input_file = str(input()).rstrip()
degraded = imageio.imread(input_file)
angle = int(input())
steps = int(input())
# The Richardson-Lucy iterative method is used to restore an image
# degraded by motion blur and noise.
restored = richardson_lucy(degraded, get_motion_psf(degraded.shape, angle), steps)
print(root_mean_square_error(degraded, restored))
if __name__ == "__main__":
main()