From 62ce1f90beaf398e571aca17f0bf9be10f6da96a Mon Sep 17 00:00:00 2001 From: lahmare <elmehdilahmar@grenoble-inp.org> Date: Thu, 1 Feb 2024 00:13:23 +0100 Subject: [PATCH] Update --- src/methods/Elmehdi_lahmar/malvar.py | 66 +++++++++++++++++++++++ src/methods/Elmehdi_lahmar/reconstruct.py | 58 ++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 src/methods/Elmehdi_lahmar/malvar.py create mode 100644 src/methods/Elmehdi_lahmar/reconstruct.py diff --git a/src/methods/Elmehdi_lahmar/malvar.py b/src/methods/Elmehdi_lahmar/malvar.py new file mode 100644 index 0000000..b087120 --- /dev/null +++ b/src/methods/Elmehdi_lahmar/malvar.py @@ -0,0 +1,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 \ No newline at end of file diff --git a/src/methods/Elmehdi_lahmar/reconstruct.py b/src/methods/Elmehdi_lahmar/reconstruct.py new file mode 100644 index 0000000..7a771ee --- /dev/null +++ b/src/methods/Elmehdi_lahmar/reconstruct.py @@ -0,0 +1,58 @@ +"""The main file for the reconstruction. +This file should NOT be modified except the body of the 'run_reconstruction' function. +Students can call their functions (declared in others files of src/methods/your_name). +""" + + +import numpy as np +from src.methods.Elmehdi_lahmar.malvar import malvar +from src.forward_model import CFA + +def run_reconstruction(y: np.ndarray, cfa: str) -> np.ndarray: + """ + Run the demosaicing process using Malvar-He-Cutler algorithm for Bayer pattern. + + Args: + y (np.ndarray): The mosaicked image to be reconstructed. + cfa (str): Name of the CFA, expected to be 'bayer'. + + Returns: + np.ndarray: The demosaicked image. + """ + if cfa != 'bayer': + raise ValueError("Malvar-He-Cutler demosaicing only supports Bayer CFA pattern.") + + input_shape = (y.shape[0], y.shape[1], 3) + op = CFA(cfa, input_shape) + + return malvar(y, op) + + + + + + +#### +#### +#### + +#### #### #### ############# +#### ###### #### ################## +#### ######## #### #################### +#### ########## #### #### ######## +#### ############ #### #### #### +#### #### ######## #### #### #### +#### #### ######## #### #### #### +#### #### ######## #### #### #### +#### #### ## ###### #### #### ###### +#### #### #### ## #### #### ############ +#### #### ###### #### #### ########## +#### #### ########## #### #### ######## +#### #### ######## #### #### +#### #### ############ #### +#### #### ########## #### +#### #### ######## #### +#### #### ###### #### + +# 2023 +# Authors: Mauro Dalla Mura and Matthieu Muller -- GitLab