diff --git a/src/methods/dzik_erwan/__init__.py b/src/methods/dzik_erwan/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/src/methods/dzik_erwan/dzik_erwan.pdf b/src/methods/dzik_erwan/dzik_erwan.pdf new file mode 100644 index 0000000000000000000000000000000000000000..9ebc663f65a7f20f29850d5099eb013c53e7c7e3 Binary files /dev/null and b/src/methods/dzik_erwan/dzik_erwan.pdf differ diff --git a/src/methods/dzik_erwan/functions.py b/src/methods/dzik_erwan/functions.py new file mode 100644 index 0000000000000000000000000000000000000000..5f2b583f66dfb579a802d55678b6b7bdfdb25df9 --- /dev/null +++ b/src/methods/dzik_erwan/functions.py @@ -0,0 +1,115 @@ +import numpy as np +from scipy.ndimage import convolve + +from src.forward_model import CFA + +def malvar2004(y,op): + "Code inspiered by KelSolaar's GitHub repo:" + "https://github.com/colour-science/colour-demosaicing/tree/master" + + z = op.adjoint(y) + mask = op.mask + + R = z[:,:,0] + G = z[:,:,1] + B = z[:,:,2] + + R_m, G_m, B_m = mask[:,:,0], mask[:,:,1], mask[:,:,2] + + + G = np.where(np.logical_or(R_m == 1, B_m == 1), convolve(y, GR_GB), G) + + + #Logical array that verify the conditon of the different filters + # Red rows. + R_r = np.transpose(np.any(R_m == 1, axis=1)[None]) * np.ones(R.shape) + # Red columns. + R_c = np.any(R_m == 1, axis=0)[None] * np.ones(R.shape) + # Blue rows. + B_r = np.transpose(np.any(B_m == 1, axis=1)[None]) * np.ones(B.shape) + # Blue columns + B_c = np.any(B_m == 1, axis=0)[None] * np.ones(B.shape) + + #Results of the convolutions + RBg_RBBR = convolve(y, Rg_RB_Bg_BR) + RBg_BRRB = convolve(y, Rg_BR_Bg_RB) + RBgr_BBRR = convolve(y, Rb_BB_Br_RR) + + + R = np.where(np.logical_and(R_r == 1, B_c == 1), RBg_RBBR, R) + R = np.where(np.logical_and(B_r == 1, R_c == 1), RBg_BRRB, R) + + B = np.where(np.logical_and(B_r == 1, R_c == 1), RBg_RBBR, B) + B = np.where(np.logical_and(R_r == 1, B_c == 1), RBg_BRRB, B) + + R = np.where(np.logical_and(B_r == 1, B_c == 1), RBgr_BBRR, R) + B = np.where(np.logical_and(R_r == 1, R_c == 1), RBgr_BBRR, B) + + + res = np.clip(np.stack((R,G,B), axis = 2), 0, 1) + + + return res + +def quad_bayer_to_bayer(cfa_quad_bayer): + "Quad to Bayer by swapping method" + + cfa_bayer = np.flip(np.copy(cfa_quad_bayer), 1) + h,w = cfa_bayer.shape + for col in range(1, w, 4): + temp = np.copy(cfa_bayer[:, col]) + cfa_bayer[:, col] = cfa_bayer[:, col + 1] + cfa_bayer[:, col + 1] = temp + del temp + + + for row in range(1, h, 4): + temp = np.copy(cfa_bayer[row, :]) + cfa_bayer[row, :] = cfa_bayer[row + 1, :] + cfa_bayer[row + 1, :] = temp + del temp + + for row in range(1, h, 4): + for col in range(2, w, 4): + g1 = cfa_bayer[row, col] + cfa_bayer[row, col] = cfa_bayer[row + 1, col -1] + cfa_bayer[row + 1, col -1] = g1 + + return np.flip(cfa_bayer,1) + +# G at R locations or G at B locations +GR_GB = (1/8) * np.asarray([ + [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.], +]) + +#R at green in R row, B column or B at green in B row, R colunm +Rg_RB_Bg_BR = (1/8) * np.asarray([ + [0., 0., .5, 0., 0.], + [0., -1., 0., -1., 0.], + [-1., 4., 5., 4., -1.], + [0., -1., 0., -1., 0.], + [0., 0., .5, 0., 0.], +]) + +# R at green in B row, R column or B at green in R row, B column +Rg_BR_Bg_RB = (1/8) * np.asarray([ + [0., 0., -1, 0., 0.], + [0., -1., 4., -1., 0.], + [.5, 0., 5., 0., .5], + [0., -1., 4., -1., 0.], + [0., 0., -1, 0., 0.], +]) + +#R at blue in B row, B column or B at red in R row, R column +Rb_BB_Br_RR = (1/8) * np.asarray([ + [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.], +]) + diff --git a/src/methods/dzik_erwan/output/bayer/reconstructed_img1.png b/src/methods/dzik_erwan/output/bayer/reconstructed_img1.png new file mode 100644 index 0000000000000000000000000000000000000000..70bcf9850172cca07a3803d162d8be773401bcfd Binary files /dev/null and b/src/methods/dzik_erwan/output/bayer/reconstructed_img1.png differ diff --git a/src/methods/dzik_erwan/output/bayer/reconstructed_img2.png b/src/methods/dzik_erwan/output/bayer/reconstructed_img2.png new file mode 100644 index 0000000000000000000000000000000000000000..34dad77117335bc01820ba6cf5193d768a7a8e5a Binary files /dev/null and b/src/methods/dzik_erwan/output/bayer/reconstructed_img2.png differ diff --git a/src/methods/dzik_erwan/output/bayer/reconstructed_img3.png b/src/methods/dzik_erwan/output/bayer/reconstructed_img3.png new file mode 100644 index 0000000000000000000000000000000000000000..cd60f98dc4d26719f4ad045e544456a08e5c7e34 Binary files /dev/null and b/src/methods/dzik_erwan/output/bayer/reconstructed_img3.png differ diff --git a/src/methods/dzik_erwan/output/bayer/reconstructed_img4.png b/src/methods/dzik_erwan/output/bayer/reconstructed_img4.png new file mode 100644 index 0000000000000000000000000000000000000000..d53b58e54e7107acdabca5bd79ac63dcfdbee91c Binary files /dev/null and b/src/methods/dzik_erwan/output/bayer/reconstructed_img4.png differ diff --git a/src/methods/dzik_erwan/output/quad_bayer/reconstructed_img1.png b/src/methods/dzik_erwan/output/quad_bayer/reconstructed_img1.png new file mode 100644 index 0000000000000000000000000000000000000000..c1d5b5edf95eb980230c3a738ff450eb3421cec4 Binary files /dev/null and b/src/methods/dzik_erwan/output/quad_bayer/reconstructed_img1.png differ diff --git a/src/methods/dzik_erwan/output/quad_bayer/reconstructed_img2.png b/src/methods/dzik_erwan/output/quad_bayer/reconstructed_img2.png new file mode 100644 index 0000000000000000000000000000000000000000..198dadf0dff75016e907e6cacdc577bc42e18adf Binary files /dev/null and b/src/methods/dzik_erwan/output/quad_bayer/reconstructed_img2.png differ diff --git a/src/methods/dzik_erwan/output/quad_bayer/reconstructed_img3.png b/src/methods/dzik_erwan/output/quad_bayer/reconstructed_img3.png new file mode 100644 index 0000000000000000000000000000000000000000..6cf5fbe7367fda38774a405a3f8ba034348accc5 Binary files /dev/null and b/src/methods/dzik_erwan/output/quad_bayer/reconstructed_img3.png differ diff --git a/src/methods/dzik_erwan/output/quad_bayer/reconstructed_img4.png b/src/methods/dzik_erwan/output/quad_bayer/reconstructed_img4.png new file mode 100644 index 0000000000000000000000000000000000000000..03063cd753bbd6e1ce9355ceb592340496aadba6 Binary files /dev/null and b/src/methods/dzik_erwan/output/quad_bayer/reconstructed_img4.png differ diff --git a/src/methods/dzik_erwan/reconstruct.py b/src/methods/dzik_erwan/reconstruct.py new file mode 100644 index 0000000000000000000000000000000000000000..424b01c5e921152902d9e02ecc80beb1ac4adeea --- /dev/null +++ b/src/methods/dzik_erwan/reconstruct.py @@ -0,0 +1,61 @@ +"""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.forward_model import CFA +from src.methods.dzik_erwan.functions import malvar2004, 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. + """ + # Performing the reconstruction. + # TODO + input_shape = (y.shape[0], y.shape[1], 3) + op = CFA(cfa, input_shape) + + if cfa == 'bayer': + res = malvar2004(y,op) + else: + bayer_equivalent = quad_bayer_to_bayer(y) + res = malvar2004(bayer_equivalent, CFA("bayer", input_shape)) + + return res + + +#### +#### +#### + +#### #### #### ############# +#### ###### #### ################## +#### ######## #### #################### +#### ########## #### #### ######## +#### ############ #### #### #### +#### #### ######## #### #### #### +#### #### ######## #### #### #### +#### #### ######## #### #### #### +#### #### ## ###### #### #### ###### +#### #### #### ## #### #### ############ +#### #### ###### #### #### ########## +#### #### ########## #### #### ######## +#### #### ######## #### #### +#### #### ############ #### +#### #### ########## #### +#### #### ######## #### +#### #### ###### #### + +# 2023 +# Authors: Mauro Dalla Mura and Matthieu Muller