Newer
Older
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
import numpy as np
from scipy.signal import convolve2d
from src.forward_model import CFA
def bilinear_interpolation(op: CFA, z: np.ndarray) -> np.ndarray:
"""Perform bilinear interpolation for demosaicing
Args:
op (CFA): CFA operator.
z (np.ndarray): Adjoint image.
Returns:
np.ndarray: Interpolated image.
"""
# Bi-linear interpolation
ker_bayer_red_blue = np.array([[1, 2, 1], [2, 4, 2], [1, 2, 1]]) / 4
ker_bayer_green = np.array([[0, 1, 0], [1, 4, 1], [0, 1, 0]]) / 4
res = np.empty(op.input_shape)
res[:, :, 0] = convolve2d(z[:, :, 0], ker_bayer_red_blue, mode='same')
res[:, :, 1] = convolve2d(z[:, :, 1], ker_bayer_green, mode='same')
res[:, :, 2] = convolve2d(z[:, :, 2], ker_bayer_red_blue, mode='same')
return res
def spectral_difference(op: CFA, z: np.ndarray, res: np.ndarray) -> np.ndarray:
"""Perform spectral difference method for demosaicing
Args:
op (CFA): CFA operator.
z (np.ndarray): Adjoint image.
res (np.ndarray): Interpolated image.
Returns:
np.ndarray: Demosaicked image.
"""
ker_bayer_red_blue = np.array([[1, 2, 1], [2, 4, 2], [1, 2, 1]]) / 4
ker_bayer_green = np.array([[0, 1, 0], [1, 4, 1], [0, 1, 0]]) / 4
# Computation of spectral differences
delta_red_green_quad = z[:, :, 0] - np.multiply(res[:, :, 1],op.mask[:,:,0])
delta_red_blue_quad = z[:, :, 0] - np.multiply(res[:, :, 2],op.mask[:,:,0])
delta_blue_green_quad = z[:, :, 2] - np.multiply(res[:, :, 1],op.mask[:,:,2])
delta_blue_red_quad = z[:, :, 2] - np.multiply(res[:, :, 0],op.mask[:,:,2])
delta_green_red_quad = z[:, :, 1] - np.multiply(res[:, :, 0],op.mask[:,:,1])
delta_green_blue_quad = z[:, :, 1] - np.multiply(res[:, :, 2],op.mask[:,:,1])
# Estimation
res_sd = np.empty(op.input_shape)
res_sd[:,:,0] = res[:, :, 1] + convolve2d(delta_red_green_quad,ker_bayer_red_blue,mode='same') + res[:, :, 2] + convolve2d(delta_red_blue_quad,ker_bayer_red_blue,mode='same')
res_sd[:,:,2] = res[:, :, 1] + convolve2d(delta_blue_green_quad,ker_bayer_red_blue,mode='same') + res[:, :, 0] + convolve2d(delta_blue_red_quad,ker_bayer_red_blue,mode='same')
res_sd[:,:,1] = res[:, :, 0] + convolve2d(delta_green_red_quad,ker_bayer_green,mode='same') + res[:, :, 2] + convolve2d(delta_green_blue_quad,ker_bayer_green,mode='same')
return res_sd
def normalization(res_sd:np.ndarray)-> np.ndarray:
"""Perform a min-max normalization
Args:
res_sd (np.ndarray): Demosaicked image.
Returns:
np.ndarray: Normalized image.
"""
res = (res_sd - np.min(res_sd)) / (np.max(res_sd) - np.min(res_sd))
return res
def quad_bayer_to_bayer_pattern(op: CFA):
"""Quad to Bayer pattern conversion by swapping method
Args:
op (CFA): CFA operator.
Returns:
CFA: Bayer pettern.
"""
if op.cfa == 'quad_bayer':
for j in range(1, op.mask.shape[1], 4):
op.mask[:,j], op.mask[:,j+1] = op.mask[:,j+1].copy(), op.mask[:,j].copy()
for i in range(1, op.mask.shape[0], 4):
op.mask[i, :], op.mask[i+1,:] = op.mask[i+1,:].copy(), op.mask[i,:].copy()
for i in range(1, op.mask.shape[0], 4):
for j in range(1, op.mask.shape[1], 4):
op.mask[i,j], op.mask[i+1,j+1] = op.mask[i+1,j+1].copy(), op.mask[i,j].copy()
return 0
def quad_bayer_to_bayer_image(y:np.array):
"""Quad to Bayer conversion of a mosaicked image by swapping method
Args:
y (np.array): Mosaicked image.
"""
for j in range(1, y.shape[1], 4):
y[:,j], y[:,j+1] = y[:,j+1].copy(), y[:,j].copy()
for i in range(1, y.shape[0], 4):
y[i, :], y[i+1,:] = y[i+1,:].copy(), y[i,:].copy()
for i in range(1, y.shape[0], 4):
for j in range(1, y.shape[1], 4):
y[i,j], y[i+1,j+1] = y[i+1,j+1].copy(), y[i,j].copy()
return 0