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

Merge branch 'master' into 'master'

Master

See merge request !6
parents 19918e3e fabf109c
No related branches found
No related tags found
No related merge requests found
This diff is collapsed.
This diff is collapsed.
File added
import numpy as np
def find_Knearest_neighbors(z, chan, i, j, N, M):
"""Finds a pixel's neighbors on a channel"""
return np.array([z[(i+di)%N, (j+dj)%M, chan] for di in range(-1, 2) for dj in range(-1, 2)])
def calculate_directional_gradients(neighbors):
"""Gives the directional derivative"""
P1, P2, P3, P4, P5, P6, P7, P8, P9 = neighbors
Dx, Dy = (P4 - P6)/2, (P2 - P8)/2
Dxd, Dyd = (P3 - P7)/(2*np.sqrt(2)), (P1 - P9)/(2*np.sqrt(2))
return [Dx, Dy, Dxd, Dyd]
def calculate_adaptive_weights(z, neigh, dir_deriv,chan,i,j,N,M):
[Dx,Dy,Dxd,Dyd] = dir_deriv
[P1,P2,P3,P4,P5,P6,P7,P8,P9] = neigh
E = []
c = 1
for k in range (-1,2):
for k in range (-1,2):
n = find_Knearest_neighbors(z,chan,i+k,j+k,N,M)
dd = calculate_directional_gradients(n)
if c == 1 or c == 9:
E.append(1/np.sqrt(1 + Dyd**2 + dd[3]**2))
elif c == 2 or c == 8:
E.append(1/np.sqrt(1 + Dy**2 + dd[1]**2))
elif c == 3 or c == 7:
E.append(1/np.sqrt(1 + Dxd**2 + dd[2]**2))
elif c == 4 or c == 6:
E.append(1/np.sqrt(1 + Dx**2 + dd[0]**2))
c += 1
return E
def interpolate_pixel(neigh,weights):
"""This function performs interpolation for a single pixel by calculating a weighted average of its neighboring pixels"""
[P1,P2,P3,P4,P5,P6,P7,P8,P9] = neigh
[E1,E2,E3,E4,E6,E7,E8,E9] = weights
num5 = E2*P2 + E4*P4 + E6*P6 + E8*P8
den5 = E2 + E4 + E6 + E8
I5 = num5/den5
return I5
def interpolate_RedBlue(neighbors, neighbors_G, weights):
"""This function specifically interpolates a pixel in the red or blue channels"""
[P1,P2,P3,P4,P5,P6,P7,P8,P9] = neighbors
[G1,G2,G3,G4,G5,G6,G7,G8,G9] = neighbors_G
[E1,E2,E3,E4,E6,E7,E8,E9] = weights
num5 = ((E1*P1)/G1) + ((E3*P3)/G3) + ((E7*P7)/G7) + ((E9*P9)/G9)
den5 = E1 + E3 + E7 + E9
I5 = G5 * num5/den5
return I5
import numpy as np
from src.forward_model import CFA
from src.methods.ELAMRANI_Mouna.functions import *
def run_reconstruction(y: np.ndarray, cfa: str) -> np.ndarray:
cfa_name = 'bayer' # bayer or quad_bayer
input_shape = (y.shape[0], y.shape[1], 3)
op = CFA(cfa_name, input_shape)
img_res = op.adjoint(y)
N = img_res[:,:,0].shape[0]
M = img_res[:,:,0].shape[1]
def interpolate_channel(img_res, channel, first_pass, N, M):
for i in range(N):
for j in range(M):
if first_pass and ((channel == 0 and i % 2 == 1 and j % 2 == 0) or
(channel == 2 and i % 2 == 0 and j % 2 == 1)):
neighbors = find_Knearest_neighbors(img_res, channel, i, j, N, M)
neighbors_G = find_Knearest_neighbors(img_res, 1, i, j, N, M)
dir_deriv = calculate_directional_gradients(neighbors_G)
weights = calculate_adaptive_weights(img_res, neighbors_G, dir_deriv, 1, i, j, N, M)
img_res[i, j, channel] = interpolate_RedBlue(neighbors, neighbors_G, weights)
elif not first_pass and img_res[i, j, channel] == 0:
neighbors = find_Knearest_neighbors(img_res, channel, i, j, N, M)
dir_deriv = calculate_directional_gradients(neighbors)
weights = calculate_adaptive_weights(img_res, neighbors, dir_deriv, channel, i, j, N, M)
img_res[i, j, channel] = interpolate_pixel(neighbors, weights)
return img_res
# Interpolation pour chaque canal
img_res = interpolate_channel(img_res, 1, False, N, M) # Interpolation du canal vert
img_res = interpolate_channel(img_res, 0, True, N, M) # Première interpolation du canal rouge
img_res = interpolate_channel(img_res, 0, False, N, M) # Seconde interpolation du canal rouge
img_res = interpolate_channel(img_res, 2, True, N, M) # Première interpolation du canal bleu
img_res = interpolate_channel(img_res, 2, False, N, M) # Seconde interpolation du canal bleu
img_res[img_res > 1] = 1
img_res[img_res < 0] = 0
return img_res
"""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
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)
return np.zeros(op.input_shape)
####
####
####
#### #### #### #############
#### ###### #### ##################
#### ######## #### ####################
#### ########## #### #### ########
#### ############ #### #### ####
#### #### ######## #### #### ####
#### #### ######## #### #### ####
#### #### ######## #### #### ####
#### #### ## ###### #### #### ######
#### #### #### ## #### #### ############
#### #### ###### #### #### ##########
#### #### ########## #### #### ########
#### #### ######## #### ####
#### #### ############ ####
#### #### ########## ####
#### #### ######## ####
#### #### ###### ####
# 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