-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageEnhancement.py
84 lines (69 loc) · 2.66 KB
/
ImageEnhancement.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
import numpy as np
import keras as kr
from constants import BIAS_VAL
def load_trained_model(path):
"""
Load a pre trained model. 'base_model' parameter for 'restore_image' function
:param path: path to the model on the computer
:return: a trained model
"""
return kr.models.load_model(path)
def copy_restored_patch(model, origin, patch_shape, i, j):
patch = origin[i:i + patch_shape[0], j:j + patch_shape[1]]
patch = patch[..., np.newaxis]
res = model.predict(patch[np.newaxis, ...])[0].copy()
return res.reshape(res.shape[0:2])
def restore_image_v2(corrupted_image, base_model):
im_shape = corrupted_image.shape
patch_shape = base_model.input_shape[1:3]
restored_image = np.zeros(im_shape)
for i in range(0, im_shape[0]-patch_shape[0], patch_shape[0]):
for j in range(0, im_shape[1]-patch_shape[1], patch_shape[1]):
restored_image[i:i+patch_shape[0], j:j+patch_shape[1]] = copy_restored_patch(
base_model,
corrupted_image,
patch_shape,
i,
j)
i = im_shape[0] - patch_shape[0]
for j in range(0, im_shape[1] - patch_shape[1], patch_shape[1]):
restored_image[i:i + patch_shape[0], j:j + patch_shape[1]] = copy_restored_patch(
base_model,
corrupted_image,
patch_shape,
i,
j)
j = im_shape[1] - patch_shape[1]
for i in range(0, im_shape[0] - patch_shape[0], patch_shape[0]):
restored_image[i:i + patch_shape[0], j:j + patch_shape[1]] = copy_restored_patch(
base_model,
corrupted_image,
patch_shape,
i,
j)
j = im_shape[1] - patch_shape[1]
i = im_shape[0] - patch_shape[0]
restored_image[i:i + patch_shape[0], j:j + patch_shape[1]] = copy_restored_patch(
base_model,
corrupted_image,
patch_shape,
i,
j)
return restored_image
def restore_image(corrupted_image, base_model):
"""
given a trained model "base_model", and noised / blurred image, will return an improved image.
:param corrupted_image: numpy ndarray
:param base_model: trained model
:return: ndarray representing an image
"""
corrupted_image = corrupted_image[..., np.newaxis]
image_shape = corrupted_image.shape
a = kr.layers.Input(shape=image_shape)
b = base_model(a)
new_model = kr.Model(inputs=a, outputs=b)
corrupted_image = corrupted_image - BIAS_VAL
clean_image = new_model.predict(corrupted_image[np.newaxis, ...])[0]
clean_image = clean_image + BIAS_VAL
clean_image = np.clip(clean_image, 0, 1)
return clean_image[:, :, 0]