diff --git a/src/methods/dai/dai.pdf b/src/methods/dai/dai.pdf new file mode 100644 index 0000000000000000000000000000000000000000..bf7796c360943d14294386904302f2a355374af8 Binary files /dev/null and b/src/methods/dai/dai.pdf differ diff --git a/src/methods/dai/function.py b/src/methods/dai/function.py new file mode 100644 index 0000000000000000000000000000000000000000..6f9c70fd88cde7c118179c48683cb1052c9a5b3d --- /dev/null +++ b/src/methods/dai/function.py @@ -0,0 +1,65 @@ +import numpy as np +from scipy.signal import convolve2d + +from src.forward_model import CFA + + +def naive_interpolation(op: CFA, y: np.ndarray) -> np.ndarray: + """Performs a simple interpolation of the lost pixels. + + Args: + op (CFA): CFA operator. + y (np.ndarray): Mosaicked image. + + Returns: + np.ndarray: Demosaicked image. + """ + + + res = np.empty(op.input_shape) + z = op.adjoint(y) + + 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,y): + + z = op.adjoint(y) + y_hat = naive_interpolation(op,y) + res = np.empty(op.input_shape) + for l in range(3): + res[:,:,0]+=z[:,:,l]+convolve2d(z[:,:,0]-y_hat[:,:,l]*op.mask[:,:,0], ker_bayer_red_blue, mode='same')*op.mask[:,:,l] + res[:,:,1]+=z[:,:,l]+convolve2d(z[:,:,1]-y_hat[:,:,l]*op.mask[:,:,1], ker_bayer_green, mode='same')*op.mask[:,:,l] + res[:,:,2]+=z[:,:,l]+convolve2d(z[:,:,2]-y_hat[:,:,l]*op.mask[:,:,2], ker_bayer_red_blue, mode='same')*op.mask[:,:,l] + + return res + +def quad_bayer_to_bayer(y_quad): + y_bayer=np.copy(y_quad) + + for i in range(1,y_quad.shape[0],4): + temp = np.copy(y_bayer[i,:]) + y_bayer[i,:]=y_bayer[i+1,:] + y_bayer[i+1,:]=temp + + for j in range(1,y_quad.shape[1],4): + temp = np.copy(y_bayer[:,j]) + y_bayer[:,j]=y_bayer[:,j+1] + y_bayer[:,j+1]=temp + + + + return y_bayer + + + + +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 \ No newline at end of file diff --git a/src/methods/dai/reconstruct.py b/src/methods/dai/reconstruct.py new file mode 100644 index 0000000000000000000000000000000000000000..54456ca8b5d29a40c4ad19ca78db7a4d08fabd69 --- /dev/null +++ b/src/methods/dai/reconstruct.py @@ -0,0 +1,58 @@ +"""The main file for the baseline reconstruction. +This file should NOT be modified. +""" + + +import numpy as np + +from src.forward_model import CFA +from src.methods.dai.function import Spectral_difference, quad_bayer_to_bayer + + +def run_reconstruction(y: np.ndarray, cfa: str) -> np.ndarray: + """Performs demosaicking on y. + + Args: + y (np.ndarray): Mosaicked image to be reconstructed. + cfa (str): Name of the CFA. Can be bayer or quad_bayer. + + Returns: + np.ndarray: Demosaicked image. + """ + input_shape = (y.shape[0], y.shape[1], 3) + op = CFA(cfa, input_shape) + + if op.cfa == 'bayer': + res = Spectral_difference(op, y) + + else: + op.mask = quad_bayer_to_bayer(op.mask) + res = Spectral_difference(CFA('bayer',input_shape),quad_bayer_to_bayer(y)) + + return res + + +####a +#### +#### + +#### #### #### ############# +#### ###### #### ################## +#### ######## #### #################### +#### ########## #### #### ######## +#### ############ #### #### #### +#### #### ######## #### #### #### +#### #### ######## #### #### #### +#### #### ######## #### #### #### +#### #### ## ###### #### #### ###### +#### #### #### ## #### #### ############ +#### #### ###### #### #### ########## +#### #### ########## #### #### ######## +#### #### ######## #### #### +#### #### ############ #### +#### #### ########## #### +#### #### ######## #### +#### #### ###### #### + +# 2023 +# Authors: Mauro Dalla Mura and Matthieu Muller