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

Merge branch 'master' into 'master'

Master

See merge request !44
parents 8c66b214 9a83f939
No related branches found
No related tags found
1 merge request!44Master
This diff is collapsed.
### How to use ?
There are 2 functions that can be called in reconstruct.py :
f.cfa_reconstruction_1 that uses the method 1
and f.cfa_reconstruction that uses the method 2
They use the same arguments which are y , cfa and input_shape.
File added
import numpy as np
import matplotlib.pyplot as plt
from src.checks import check_cfa, check_rgb
from src.forward_model import CFA
import src.methods.Samanos_Thomas.version1 as v1
import src.methods.Samanos_Thomas.version2 as v2
from scipy import ndimage
def cfa_reconstruction(y: np.ndarray, cfa:str, input_shape: tuple) -> np.ndarray:
"""Performs demosaicking on y.
Args:
cfa (str): Name of the CFA. Can be bayer or quad_bayer.
Returns:
np.ndarray: Demosaicked image.
"""
if cfa == "bayer":
op = v2.bayer_reconstruction(y,input_shape)
elif cfa == "quad_bayer":
op = v2.quad_bayer_reconstruction(y,input_shape)
else:
op = np.zeros(input_shape)
return op
def cfa_reconstruction_1(y: np.ndarray, cfa:str, input_shape: tuple) -> np.ndarray:
"""Performs demosaicking on y.
Args:
cfa (str): Name of the CFA. Can be bayer or quad_bayer.
Returns:
np.ndarray: Demosaicked image.
"""
if cfa == "bayer":
op = v1.bayer_reconstruction(y,input_shape)
elif cfa == "quad_bayer":
op = v1.quad_bayer_reconstruction(y,input_shape)
else:
op = np.zeros(input_shape)
return op
\ No newline at end of file
"""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
import src.methods.Samanos_Thomas.function as f
from src.forward_model import CFA
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.
"""
input_shape = (y.shape[0], y.shape[1], 3)
Y = f.cfa_reconstruction(y, cfa,input_shape)
return Y
####
####
####
#### #### #### #############
#### ###### #### ##################
#### ######## #### ####################
#### ########## #### #### ########
#### ############ #### #### ####
#### #### ######## #### #### ####
#### #### ######## #### #### ####
#### #### ######## #### #### ####
#### #### ## ###### #### #### ######
#### #### #### ## #### #### ############
#### #### ###### #### #### ##########
#### #### ########## #### #### ########
#### #### ######## #### ####
#### #### ############ ####
#### #### ########## ####
#### #### ######## ####
#### #### ###### ####
# 2023
# Authors: Mauro Dalla Mura and Matthieu Muller
import numpy as np
from skimage.transform import resize
def upscale(img: np.ndarray, shape: tuple, pas: int) -> np.ndarray:
"""Upscales the acquisition to the shape of the desired reconstruction.
Args:
img (np.ndarray): mosaique image in smaller size.
shape (tuple): Shape of the output.
Returns:
np.ndarray: Upscaled image.
"""
return resize(img, shape, anti_aliasing=True)
def bayer_reconstruction(y : np.ndarray, input_shape : tuple) -> np.ndarray:
op_shape = (input_shape[0]//2,input_shape[1]//2,3)
op = np.zeros(op_shape)
conv = np.zeros((2,2,3))
liste = [(0,1,0),(0,0,1),(1,1,1),(1,0,2)]
for el in liste :
conv[el]=1
if el[2]==1:
conv[el]=1/2
for i in range (3):
op[:,:,i] = conv2d(y,conv[:,:,i], 2)
op1 = upscale(op,(input_shape[0],input_shape[1],3),2)
return op1
def quad_bayer_reconstruction(y : np.ndarray, input_shape : tuple) -> np.ndarray:
op_shape = (input_shape[0]//4,input_shape[1]//4,3)
op = np.zeros(op_shape)
conv = np.zeros((4,4,3))
liste = [(2,0,2),(3,0,2),(2,1,2),(3,1,2),(0,0,1),(1,0,1),(0,1,1),(1,1,1),(2,2,1),(2,3,1),(3,2,1),(3,3,1),(0,2,0),(0,3,0),(1,2,0),(1,3,0)]
for el in liste :
conv[el]=1/4
if el[2]==1:
conv[el]=1/8
for i in range (3):
op[:,:,i] = conv2d(y,conv[:,:,i], 4)
op1 = upscale(op,(input_shape[0],input_shape[1],3),4)
return op1
def conv2d(img : np.ndarray, conv : np.ndarray, pas :int):
y_shape = (img.shape[0], img.shape[1])
op = np.zeros((y_shape[0]//pas,y_shape[1]//pas))
for i in range(0,y_shape[0],pas):
for j in range(0,y_shape[1],pas):
op[i//pas,j//pas] = np.sum(img[i:i+pas,j:j+pas]*conv)
return op
import numpy as np
from scipy.signal import convolve2d
CONV2 = np.array([[1/4,1/2,1/4],[1/2,1,1/2],[1/4,1/2,1/4]])
def bayer_reconstruction(y : np.ndarray, input_shape : tuple) -> np.ndarray:
op_shape = (input_shape[0],input_shape[1],3)
op = np.zeros(op_shape)
conv = np.zeros((2,2,3))
liste = [(0,1,0),(0,0,1),(1,1,1),(1,0,2)]
for el in liste :
conv[el]=1
op = masq(y,conv, 2)
op1 = np.zeros_like(op)
for color in range(3):
op1[:,:,color] = convolve2d(op[:,:,color],CONV2,mode ="same")
if color == 1 :
op1[:,:,color] = op1[:,:,color]/2
return op1
CONV4 = np.array([[11/156,43/312,2/13,43/312,11/156],
[43/312,1/6,1/4,1/6,43/312],
[2/13,1/4,1/3,1/4,2/13],
[43/312,1/6,1/4,1/6,43/312],
[11/156,43/312,2/13,43/312,11/156]])
def quad_bayer_reconstruction(y : np.ndarray, input_shape : tuple) -> np.ndarray:
op_shape = (input_shape[0],input_shape[1],3)
op = np.zeros(op_shape)
conv = np.zeros((4,4,3))
liste = [(2,0,2),(3,0,2),(2,1,2),(3,1,2),(0,0,1),(1,0,1),(0,1,1),(1,1,1),(2,2,1),(2,3,1),(3,2,1),(3,3,1),(0,2,0),(0,3,0),(1,2,0),(1,3,0)]
for el in liste :
conv[el]=1
op = masq(y,conv, 4)
op1 = np.zeros_like(op)
for color in range(3):
op1[:,:,color] = convolve2d(op[:,:,color],CONV4,mode ="same")
if color == 1 :
op1[:,:,color] = op1[:,:,color]/2
return op1
def masq(img : np.ndarray,conv:np.ndarray, pas:int) ->np.ndarray:
masq = np.zeros((img.shape[0],img.shape[1],3))
po = np.zeros((img.shape[0],img.shape[1],3))
for color in range (3):
for i in range(0,po.shape[0],pas):
for j in range(0,po.shape[1],pas):
masq[i:i+pas,j:j+pas,color] = conv[:,:,color]
po[:,:,color] = masq[:,:,color] * img
return po
\ No newline at end of file
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