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
import numpy as np
from scipy.signal import convolve2d
from src.forward_model import CFA
def malvar(y: np.ndarray, op: CFA) -> np.ndarray:
"""
Malvar-He-Cutler demosaicing algorithm for Bayer pattern.
"""
# Convert the mosaicked image to the initial estimated channels.
z = op.adjoint(y)
# Define convolution kernels
kernel_G_at_RB = np.array([[0, 0, -1, 0, 0], [0, 0, 2, 0, 0], [-1, 2, 4, 2, -1], [0, 0, 2, 0, 0], [0, 0, -1, 0, 0]]) / 8
kernel_RB_at_G = np.array([[0, 0, 0.5, 0, 0], [0, -1, 0, -1, 0], [-1, 4, 5, 4, -1], [0, -1, 0, -1, 0], [0, 0, 0.5, 0, 0]]) / 8
kernel_RB_at_RB = np.array([[0, 0, -1.5, 0, 0], [0, 2, 0, 2, 0], [-1.5, 0, 6, 0, -1.5], [0, 2, 0, 2, 0], [0, 0, -1.5, 0, 0]]) / 8
# Interpolate each channel
R = z[:, :, 0]
G = z[:, :, 1]
B = z[:, :, 2]
mask = op.mask
R_m, G_m, B_m = mask[:, :, 0], mask[:, :, 1], mask[:, :, 2]
# Interpolate G at R and B locations
G = np.where(np.logical_or(R_m == 1, B_m == 1), convolve2d(y, kernel_G_at_RB, mode='same'), G)
# Interpolate R at G and B locations, B at R and G locations
R = np.where(np.logical_or(G_m == 1, B_m == 1), convolve2d(y, kernel_RB_at_G, mode='same'), R)
B = np.where(np.logical_or(R_m == 1, G_m == 1), convolve2d(y, kernel_RB_at_G, mode='same'), B)
# Interpolate R at B locations and B at R locations
R = np.where(B_m == 1, convolve2d(y, kernel_RB_at_RB, mode='same'), R)
B = np.where(R_m == 1, convolve2d(y, kernel_RB_at_RB, mode='same'), B)
# Combine channels
return np.clip(np.stack((R, G, B), axis=-1), 0, 1)
####
####
####
#### #### #### #############
#### ###### #### ##################
#### ######## #### ####################
#### ########## #### #### ########
#### ############ #### #### ####
#### #### ######## #### #### ####
#### #### ######## #### #### ####
#### #### ######## #### #### ####
#### #### ## ###### #### #### ######
#### #### #### ## #### #### ############
#### #### ###### #### #### ##########
#### #### ########## #### #### ########
#### #### ######## #### ####
#### #### ############ ####
#### #### ########## ####
#### #### ######## ####
#### #### ###### ####
# 2023
# Authors: Mauro Dalla Mura and Matthieu Muller