Skip to content
Snippets Groups Projects
Commit d6e1c838 authored by Erwan Dzik's avatar Erwan Dzik
Browse files

Update src/methods/dzik_erwan/__init__.py,...

Update src/methods/dzik_erwan/__init__.py, src/methods/dzik_erwan/functions.py, src/methods/dzik_erwan/reconstruct.py, src/methods/dzik_erwan/dzik_erwan.pdf, src/methods/dzik_erwan/output/bayer/reconstructed_img1.png, src/methods/dzik_erwan/output/bayer/reconstructed_img4.png, src/methods/dzik_erwan/output/bayer/reconstructed_img2.png, src/methods/dzik_erwan/output/bayer/reconstructed_img3.png, src/methods/dzik_erwan/output/quad_bayer/reconstructed_img1.png, src/methods/dzik_erwan/output/quad_bayer/reconstructed_img3.png, src/methods/dzik_erwan/output/quad_bayer/reconstructed_img4.png, src/methods/dzik_erwan/output/quad_bayer/reconstructed_img2.png
parent 25942cba
No related branches found
No related tags found
No related merge requests found
Showing
with 176 additions and 0 deletions
File added
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.],
])
src/methods/dzik_erwan/output/bayer/reconstructed_img1.png

1.6 MiB

src/methods/dzik_erwan/output/bayer/reconstructed_img2.png

1.97 MiB

src/methods/dzik_erwan/output/bayer/reconstructed_img3.png

1.84 MiB

src/methods/dzik_erwan/output/bayer/reconstructed_img4.png

1.96 MiB

src/methods/dzik_erwan/output/quad_bayer/reconstructed_img1.png

1.64 MiB

src/methods/dzik_erwan/output/quad_bayer/reconstructed_img2.png

1.97 MiB

src/methods/dzik_erwan/output/quad_bayer/reconstructed_img3.png

1.86 MiB

src/methods/dzik_erwan/output/quad_bayer/reconstructed_img4.png

1.96 MiB

"""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
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