diff --git a/src/methods/chaari_mohamed/fonctions.ipynb b/src/methods/chaari_mohamed/fonctions.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..c94e976282662746f97d5eed762f16acbf3c824b --- /dev/null +++ b/src/methods/chaari_mohamed/fonctions.ipynb @@ -0,0 +1,142 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 24, + "id": "ec6da321", + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "from scipy.signal import convolve2d\n", + "from src.forward_model import CFA\n", + "\n", + "def bilinear_demosaicing(op: CFA, y: np.ndarray) -> np.ndarray:\n", + " \"\"\"\n", + " Bilinear demosaicing method.\n", + "\n", + " Args:\n", + " op (CFA): CFA operator.\n", + " y (np.ndarray): Mosaicked image.\n", + "\n", + " Returns:\n", + " np.ndarray: Demosaicked image.\n", + " \"\"\"\n", + " # Copie des valeurs directement connues pour chaque canal\n", + " red = y[:, :, 0]\n", + " green = y[:, :, 1]\n", + " blue = y[:, :, 2]\n", + "\n", + " # Création des masques pour chaque couleur selon le motif CFA\n", + " mask_red = (op.mask == 0) # Supposons que 0 correspond au rouge dans le masque\n", + " mask_green = (op.mask == 1) # Supposons que 1 correspond au vert\n", + " mask_blue = (op.mask == 2) # Supposons que 2 correspond au bleu\n", + "\n", + " # Interpolation bilinéaire pour le rouge et le bleu\n", + " # Note: np.multiply multiplie les éléments correspondants des tableaux, c'est pourquoi nous utilisons np.multiply au lieu de *\n", + " red_interp = convolve2d(np.multiply(red, mask_red), [[1/4, 1/2, 1/4], [1/2, 1, 1/2], [1/4, 1/2, 1/4]], mode='same')\n", + " blue_interp = convolve2d(np.multiply(blue, mask_blue), [[1/4, 1/2, 1/4], [1/2, 1, 1/2], [1/4, 1/2, 1/4]], mode='same')\n", + "\n", + " # Interpolation bilinéaire pour le vert\n", + " # Pour le vert, nous utilisons un autre noyau car il y a plus de pixels verts\n", + " green_interp = convolve2d(np.multiply(green, mask_green), [[0, 1/4, 0], [1/4, 1, 1/4], [0, 1/4, 0]], mode='same')\n", + "\n", + " # Création de l'image interpolée\n", + " demosaicked_image = np.stack((red_interp, green_interp, blue_interp), axis=-1)\n", + "\n", + " # Correction des valeurs interpolées: on réapplique les valeurs connues pour éviter le flou\n", + " demosaicked_image[:, :, 0][mask_red] = red[mask_red]\n", + " demosaicked_image[:, :, 1][mask_green] = green[mask_green]\n", + " demosaicked_image[:, :, 2][mask_blue] = blue[mask_blue]\n", + "\n", + " # Clip pour s'assurer que toutes les valeurs sont dans la plage [0, 1]\n", + " demosaicked_image = np.clip(demosaicked_image, 0, 1)\n", + "\n", + " return demosaicked_image\n" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "cf379598", + "metadata": {}, + "outputs": [], + "source": [ + "def quad_bayer_demosaicing(op: CFA, y: np.ndarray) -> np.ndarray:\n", + " \"\"\"\n", + " Demosaicing method for Quad Bayer CFA pattern.\n", + "\n", + " Args:\n", + " op (CFA): CFA operator.\n", + " y (np.ndarray): Mosaicked image.\n", + "\n", + " Returns:\n", + " np.ndarray: Demosaicked image.\n", + " \"\"\"\n", + " \n", + " # Interpolation bilinéaire pour chaque canal\n", + " red_interp = convolve2d(np.multiply(y[:, :, 0], op.mask == 0), [[1/4, 1/2, 1/4], [1/2, 1, 1/2], [1/4, 1/2, 1/4]], mode='same')\n", + " green_interp = convolve2d(np.multiply(y[:, :, 1], op.mask == 1), [[0, 1/4, 0], [1/4, 1, 1/4], [0, 1/4, 0]], mode='same')\n", + " blue_interp = convolve2d(np.multiply(y[:, :, 2], op.mask == 2), [[1/4, 1/2, 1/4], [1/2, 1, 1/2], [1/4, 1/2, 1/4]], mode='same')\n", + "\n", + " # Assemblage de l'image interpolée\n", + " demosaicked_image = np.stack((red_interp, green_interp, blue_interp), axis=-1)\n", + "\n", + " # Réapplication des valeurs connues\n", + " demosaicked_image[:, :, 0][op.mask == 0] = y[:, :, 0][op.mask == 0]\n", + " demosaicked_image[:, :, 1][op.mask == 1] = y[:, :, 1][op.mask == 1]\n", + " demosaicked_image[:, :, 2][op.mask == 2] = y[:, :, 2][op.mask == 2]\n", + "\n", + " # Clip des valeurs pour les maintenir dans la plage appropriée\n", + " demosaicked_image = np.clip(demosaicked_image, 0, 1)\n", + "\n", + " return demosaicked_image\n" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "3eec062c", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a6630396", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ef4e59c8", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.16" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/src/methods/chaari_mohamed/reconstruct.py b/src/methods/chaari_mohamed/reconstruct.py new file mode 100644 index 0000000000000000000000000000000000000000..6f202941e0ece681f2a70397380409d476ae3e4c --- /dev/null +++ b/src/methods/chaari_mohamed/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.chaari_mohamed.fonctions import bilinear_demosaicing,quad_bayer_demosaicing + + +def run_reconstruction(y: np.ndarray, cfa: str) -> np.ndarray: + """ + Performs demosaicking on y based on the CFA pattern. + + Args: + y (np.ndarray): Mosaicked image to be reconstructed. + cfa (str): Name of the CFA pattern. 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 cfa == 'bayer': + res = bilinear_demosaicing(op, y) + elif cfa == 'quad_bayer': + res = quad_bayer_demosaicing(op, y) + else: + raise ValueError("Unsupported CFA pattern. Supported patterns are 'bayer' and 'quad_bayer'.") + + return res + + +#### +#### +#### + +#### #### #### ############# +#### ###### #### ################## +#### ######## #### #################### +#### ########## #### #### ######## +#### ############ #### #### #### +#### #### ######## #### #### #### +#### #### ######## #### #### #### +#### #### ######## #### #### #### +#### #### ## ###### #### #### ###### +#### #### #### ## #### #### ############ +#### #### ###### #### #### ########## +#### #### ########## #### #### ######## +#### #### ######## #### #### +#### #### ############ #### +#### #### ########## #### +#### #### ######## #### +#### #### ###### #### + +# 2023 +# Authors: Mauro Dalla Mura and Matthieu Muller diff --git a/src/methods/chaari_mohamed/report_mohamed chaari.pdf b/src/methods/chaari_mohamed/report_mohamed chaari.pdf new file mode 100644 index 0000000000000000000000000000000000000000..2bbb6c993006d3c6baed65154b9dc580bfa77a5a Binary files /dev/null and b/src/methods/chaari_mohamed/report_mohamed chaari.pdf differ