Skip to content
Snippets Groups Projects
Commit c4d69f3d authored by Matthieu Muller's avatar Matthieu Muller
Browse files

Post merge changes

parent 1f4b66e2
No related branches found
No related tags found
No related merge requests found
source diff could not be displayed: it is too large. Options to address this: view the blob.
%% Cell type:markdown id: tags:
## Main notebook for experimenting with the algorithms
Here is a simple notebook to test your code. You can modify it as you please.
Remember to restart the jupyter kernel each time you modify a file.
%% Cell type:code id: tags:
``` python
%%capture
%pip install -r requirements.txt
```
%% Cell type:code id: tags:
``` python
import matplotlib.pyplot as plt
from src.utils import load_image, save_image, psnr, ssim
from src.forward_model import CFA
from src.methods.RHOUCH_Oussama.reconstruct import run_reconstruction
```
%% Cell type:markdown id: tags:
### Load the input image
%% Cell type:code id: tags:
``` python
image_path = 'images/img_4.png'
img = load_image(image_path)
```
%% Cell type:code id: tags:
``` python
# Shows some information on the image
plt.imshow(img)
plt.show()
print(f'Shape of the image: {img.shape}.')
```
%% Cell type:markdown id: tags:
### Definition of the forward model
To setup the forward operator we just need to instanciate the `CFA` class. This class needs two arguments: `cfa_name` being the kind of pattern (bayer or kodak), and `input_shape` the shape of the inputs of the operator.
This operation is linear and can be represented by a matrix $A$ but no direct access to this matrix is given (one can create it if needed). However the method `direct` allows to perform $A$'s operation. Likewise the method `adjoint` will perform the operation of $A^T$.
For example let $X \in \mathbb R^{M \times N \times 3}$ the input RGB image in natural shape. Then we got $x \in \mathbb R^{3MN}$ (vectorized version of $X$) and $A \in \mathbb R^{MN \times 3MN}$, leading to:
\begin{equation*}
y = Ax \in \mathbb R^{MN} \quad \text{and} \quad z = A^Ty \in \mathbb R^{3MN}
\end{equation*}
However thanks to `direct` and `adjoint` there is no need to work with vectorized images, except if it is interesting to create the matrix $A$ explicitly.
%% Cell type:code id: tags:
``` python
cfa_name = 'bayer' # bayer or quad_bayer
op = CFA(cfa_name, img.shape)
```
%% Cell type:code id: tags:
``` python
# Shows the mask
plt.imshow(op.mask[:10, :10])
plt.show()
print(f'Shape of the mask: {op.mask.shape}.')
```
%% Cell type:code id: tags:
``` python
# Applies the mask to the image
y = op.direct(img)
```
%% Cell type:code id: tags:
``` python
# Applies the adjoint operation to y
z = op.adjoint(y)
```
%% Cell type:code id: tags:
``` python
fig, axs = plt.subplots(2, 3, figsize=(15, 10))
axs[0, 0].imshow(img)
axs[0, 0].set_title('Input image')
axs[0, 1].imshow(y, cmap='gray')
axs[0, 1].set_title('Output image')
axs[0, 2].imshow(z)
axs[0, 2].set_title('Adjoint image')
axs[1, 0].imshow(img[800:864, 450:514])
axs[1, 0].set_title('Zoomed input image')
axs[1, 1].imshow(y[800:864, 450:514], cmap='gray')
axs[1, 1].set_title('Zoomed output image')
axs[1, 2].imshow(z[800:864, 450:514])
axs[1, 2].set_title('Zoomed adjoint image')
plt.show()
```
%% Cell type:markdown id: tags:
### Run the reconstruction
Here the goal is to reconstruct the image `img` using only `y` and `op` (using `img` is forbidden).
To run the reconstruction we simply call the function `run_reconstruction`. This function takes in argument the image to reconstruct and the kind of CFA used (bayer or kodak). All the parameters related to the reconstruction itself must be written inside `run_reconstruction`.
%% Cell type:code id: tags:
``` python
res = run_reconstruction(y, cfa_name)
```
%% Cell type:code id: tags:
``` python
# Prints some information on the reconstruction
print(f'Size of the reconstruction: {res.shape}.')
```
%% Cell type:markdown id: tags:
### Quantitative and qualitative results
%% Cell type:code id: tags:
``` python
fig, axs = plt.subplots(1, 2, figsize=(15, 15))
axs[0].imshow(img)
axs[0].set_title('Original image')
axs[1].imshow(res)
axs[1].set_title('Reconstructed image')
plt.show()
```
%% Cell type:code id: tags:
``` python
# Computes some metrics
print(f'PSNR: {psnr(img, res):.2f}')
print(f'SSIM: {ssim(img, res):.4f}')
```
%% Cell type:code id: tags:
``` python
reconstructed_path = 'output/reconstructed_image.png'
save_image(reconstructed_path, res)
```
%% Cell type:code id: tags:
``` python
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment