Skip to content
Snippets Groups Projects
Commit c18c5d9c authored by Théo TABURET's avatar Théo TABURET
Browse files

Test .html exportation.

parent 5f4ea92e
No related branches found
No related tags found
No related merge requests found
This diff is collapsed.
%% Cell type:code id: tags:
``` python
### Includes
import numpy as np
import matplotlib.pyplot as plt
from math import pi
from IPython.display import HTML
```
%% Cell type:markdown id: tags:
# Analytical covariance matrix computation
This notebook explain the use of the NS module to compute the analytical covariance matrix for DCT coefficients from a bloc (or 3) of photo-sites samples.
%% Cell type:code id: tags:
``` python
fig, ax = plt.subplots(figsize=(5, 5))
for m in range(1, 64+1):
_k = (m-1)//8
_l = (m-1)%8
_M = np.fromfunction(lambda _i, _j: np.cos((pi/16)*_k*(2*_i+1))*np.cos((pi/16)*_l*(2*_j+1)), (8, 8), dtype=np.uint64)
ax = plt.subplot(8, 8, m)
ax = plt.imshow(_M, cmap = 'gray', clim = (-1, 1))
ax = plt.axis('off')
plt.show()
```
%% Output
%% Cell type:code id: tags:
``` python
HTML('''<script>
code_show=true;
function code_toggle() {
if (code_show){
$('div.input').hide();
} else {
$('div.input').show();
}
code_show = !code_show
}
$( document ).ready(code_toggle);
</script>
<form action="javascript:code_toggle()"><input type="submit" value="Click here to toggle on/off the raw code."></form>''')
```
%% Output
<IPython.core.display.HTML object>
%% Cell type:markdown id: tags:
## Computation pipeline :
<ol>
<li>Photo-sites selection</li>
<li>Demosaicking</li>
<li>Selection</li>
<li>Permutation</li>
<li>DCT transform</li>
<li>Transposition</li>
</ol>
%% Cell type:markdown id: tags:
### 1. Photo-sites selection
The samples comes from $n_{b}\times n_{b}$ neighboring blocks, each composed of $8\times8$ photo-sites with the surrounding border included since interpolated values inside a $8\times8$ block need photo-site values surrounding this block (see Figure below). The result of flattening according to the columns this matrix is a contiguous flattened array which is therefore the the vector of the photo-sites samples denoted $\mathbf{y}\in\mathbb{N}^{(8n_{b}+2)^{2}}$.
##### $\mathbf{Y}\in\mathbb{N}^{(8n_{b}+2) \times (8n_{b}+2)}$
%% Cell type:code id: tags:
``` python
s = 10
_fig, _ax = plt.subplots(figsize=(5, 5))
strs = ['R' if (i%2==0) else 'G' for i in range(s**2)]
for _m in range(1, s**2+1):
_k = (_m-1)//s
_l = (_m-1)%s
_str = ''
if(_k%2 == 0 and _l%2 == 1):
_str = '$B$'
_color = 'Blues'
elif(_k%2 == _l%2):
_str = '$G$'
_color = 'Greens'
else:
_str = '$R$'
_color = 'Reds'
if(_k==0 or _l==0 or _k == 9 or _l == 9):
_M = 0.15*np.ones((1, 1))
else:
_M = 0.5*np.ones((1, 1))
_ax = plt.subplot(s, s, _m)
_ax = plt.imshow(_M, cmap = _color, clim = (0, 1))
_ax = plt.axis('on')
_ax = plt.xticks([])
_ax = plt.yticks([])
_ax = plt.text(0, 0, _str, fontsize=10, ha='center', va='center', color='black', fontweight='bold')
#plt.tight_layout()
plt.show()
```
%% Output
%% Cell type:markdown id: tags:
##### To $\mathbf{y}\in\mathbb{N}^{(8n_{b}+2)^{2}}$
%% Cell type:code id: tags:
``` python
s = 10
_fig, _ax = plt.subplots(figsize=(5, 5))
strs = ['R' if (i%2==0) else 'G' for i in range(s**2)]
for _m in range(1, s**2+1):
_k = (_m-1)//s
_l = (_m-1)%s
_str = ''
if(_k%2 == 0 and _l%2 == 1):
_str = '$B$'
_color = 'Blues'
elif(_k%2 == _l%2):
_str = '$G$'
_color = 'Greens'
else:
_str = '$R$'
_color = 'Reds'
if(_k==0 or _l==0 or _k == 9 or _l == 9):
_M = 0.15*np.ones((1, 1))
else:
_M = 0.5*np.ones((1, 1))
_ax = plt.subplot(s, s, _m)
_ax = plt.imshow(_M, cmap = _color, clim = (0, 1))
_ax = plt.axis('on')
_ax = plt.xticks([])
_ax = plt.yticks([])
_ax = plt.text(0, 0, _str, fontsize=10, ha='center', va='center', color='black', fontweight='bold')
#plt.tight_layout()
plt.show()
```
%% Output
%% Cell type:markdown id: tags:
### 2. Demosaicking and luminance averaging
In our case, this means that to perform color reconstruction and luminance averaging, we can define the demosaicking and averaging matrix $\mathbf{M_{L}}$ as:
$\mathbf{y_{L}}=\underbrace{(0.2126\cdot\mathbf{M_{R}}+0.7152\cdot\mathbf{M_{G}}+0.0722\cdot\mathbf{M_{B}})}_{\mathbf{M_{L}}}\cdot\mathbf{y}.$
With $\mathbf{y_{L}}\in\mathbb{R}^{(8n_{b}+2)^{2}}.$
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
### 3. Selection
As stated above, the surrounding edges of $n_{b}\times n_{b}$ blocks of samples have been included in order to take into account the convolution window during demosaicking. Once the demosaicking operations have been carried out, they can now be discarded. Let's denote $\mathbf{L}$ the $(8n_{b}+2)\times(8n_{b}+2)$ photo-sites matrix with its outer border, and $\mathbf{L_{S}}$ without as depicted in Figure below. The matrix $\mathbf{M_{S}}\in\mathbb{R}^{(8n_{b})^{2}\times(8n_{b}+2)^{2}}$ can the be defined as :$\mathbf{y_{S}=\mathbf{\mathbf{M_{S}}}\cdot}\mathbf{y_{L}}\Leftrightarrow\mathrm{vec_{R}}(\mathbf{L_{S}})=\mathbf{\mathbf{M_{S}}}\cdot\mathrm{vec_{R}}(\mathbf{L})$With $\mathbf{y_{S}}\in\mathbb{R}^{(8n_{b})^{2}}$.
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
### 4. Permutation
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
### 5. DCT transform
%% Cell type:markdown id: tags:
For a $8\times8$ block in the spatial domain, $\mathbf{X_{s}}$, its 2D-DCT block version written here as $\mathbf{X_{DCT}}$ can be expressed by the following matrix multiplication :
$$\mathbf{X_{DCT}}=\mathrm{DCT}(\mathbf{X_{S}})=\mathbf{A}\cdot\mathbf{X_{S}}\cdot\mathbf{A}^{t}=\mathbf{A}\cdot(\mathbf{A}\cdot\mathbf{X}_{\mathbf{S}}^{t})^{\boldsymbol{t}}$$
With :
$$\mathbf{A}=\left[\begin{array}{cccccccc}
a & a & a & a & a & a & a & a\\
b & d & e & g & -g & -e & -d & -b\\
c & f & -f & -c & -c & -f & f & c\\
d & -g & -b & -e & e & b & g & -d\\
a & -a & -a & a & a & -a & -a & a\\
e & -b & g & d & -d & -g & b & -e\\
f & -c & c & -f & -f & c & -c & f\\
g & -e & d & -b & b & -d & e & -g
\end{array}\right]$$,and : $$\left[\begin{array}{c}
a\\
b\\
c\\
d\\
e\\
f\\
g
\end{array}\right]=\frac{1}{2}\left[\begin{array}{c}
\cos(\frac{\pi}{4})\\
\cos(\frac{\pi}{16})\\
\cos(\frac{\pi}{8})\\
\cos(\frac{3\pi}{16})\\
\cos(\frac{5\pi}{16})\\
\cos(\frac{3\pi}{8})\\
\cos(\frac{7\pi}{16})
\end{array}\right].$$
It should be observed that the multiplication by $\mathbf{A}$ and $\mathbf{A}^{t}$ is due to the fact that the DCT transform is separable and processes the columns and rows independently. In order to compute the covariance matrix of the spatial signal $\mathbf{X_{S}}$, we use vector notation by transforming the matrix $\mathbf{X_{S}}\in\mathbb{R}^{8\times8}$ into a vector $\mathbf{x_{S}}\in\mathbb{R}^{64}$ by concatenating the columns. As a result, the $8\times8$ matrix $\mathbf{A}$ is transformed into a $64\times64$ matrix $\mathbf{A_{v}}$ given by :$$\mathbf{A_{v}}=\left[\begin{array}{cccc}
\mathbf{A} & 0 & \ldots & 0\\
0 & \mathbf{A} & 0 & \vdots\\
\vdots & 0 & \ddots & 0\\
0 & \cdots & 0 & \mathbf{A}
\end{array}\right]=\sum\limits _{n=0}^{8-1}\oplus\mathbf{A}.$$
%% Cell type:markdown id: tags:
### 6. Transposition
%% Cell type:markdown id: tags:
We also define a transpose operator $\mathbf{T}\in\mathbb{R}^{64\times64}$ such as $\mathrm{vec_{c}}(\mathbf{X_{S}^{t}})=\mathbf{T}\cdot\mathrm{vec_{c}}(\mathbf{X_{S}})=\mathbf{T}\cdot\mathbf{x_{S}}$, with :
$$\mathbf{T}=\left[\begin{array}{ccccccccc}
1 & 0 & \ldots\\
0 & \ldots & \ldots & 1 & 0 & \ldots\\
& & & & & & 1 & 0 & \ldots\\
\vdots & \vdots & \vdots & \vdots & \vdots\\
0 & 1 & \ldots\\
& & & 0 & 1 & \ldots\\
& & & & & & 0 & 1 & \ldots\\
\vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots
\end{array}\right]\in\mathbb{R}^{64\times64}$$
And more simply
$$\mathbf{T}=(\delta_{8i+j,\:8j+i})_{\substack{0\leq i<8\\
0\leq j<8
}
}$$
%% Cell type:markdown id: tags:
## Result for I.I.D samples
%% Cell type:code id: tags:
``` python
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment