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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
}