Skip to content
Snippets Groups Projects
Commit b6835f47 authored by oussamarhouch's avatar oussamarhouch
Browse files

new commit

parent 4fe1abf0
No related branches found
No related tags found
1 merge request!21Master
This diff is collapsed.
import numpy as np
def clip_values(img: np.ndarray) -> np.ndarray:
"""Clip values of an image between 0 and 1
Args:
img (np.ndarray): image to clip
Returns:
np.ndarray: clipped image
"""
img[img < 0] = 0
img[img > 1] = 1
return img
import numpy as np
def find_direct_neighbors(neighbors: list) -> list:
"""Finds a pixel's direct neighbors"""
Dx = (neighbors[3] - neighbors[5]) / 2
Dy = (neighbors[1] - neighbors[7]) / 2
Dxx = (neighbors[2] - neighbors[6]) / (2 * np.sqrt(2))
Dyy = (neighbors[0] - neighbors[8]) / (2 * np.sqrt(2))
return [Dx, Dy, Dxx, Dyy]
\ No newline at end of file
import numpy as np
def find_neighbors(img: np.ndarray, channel: int, i: int, j: int, N: int, M: int) -> list:
"""Finds a pixel's neighbors on a channel"""
return np.array([img[(i + di) % N, (j + dj) % M, channel] for di in [-1, 0, 1] for dj in [-1, 0, 1]])
import numpy as np
from .find_neighbors import find_neighbors
from .find_direct_neighbors import find_direct_neighbors
def find_weights(img: np.ndarray, direct_neighbors: list, channel: int, i: int, j: int, N: int, M: int) -> list:
[Dx, Dy, Dxx, Dyy] = direct_neighbors
E = []
c = 1
for k in [-1, 0, 1]:
for l in [-1, 0, 1]:
n = find_neighbors(img, channel, i + k, j + l, N, M)
dd = find_direct_neighbors(n)
if c == 1 or c == 9:
E.append(1/np.sqrt(1 + Dyy * 2 + dd[3] * 2))
elif c == 3 or c == 7:
E.append(1/np.sqrt(1 + Dxx * 2 + dd[2] * 2))
elif c == 2 or c == 8:
E.append(1/np.sqrt(1 + Dy * 2 + dd[1] * 2))
elif c == 4 or c == 6:
E.append(1/np.sqrt(1 + Dx * 2 + dd[0] * 2))
c += 1
return E
def interpolate(neighbors: list, weights: list) -> float:
return (weights[1] * neighbors[1] + weights[3] * neighbors[3] + weights[4] * neighbors[5] + weights[6] * neighbors[7]) / (weights[1] + weights[1] + weights[4] + weights[6])
\ No newline at end of file
def interpolate_neighboring(neighbors_0: list, neighbors_1: list, weights: list) -> float:
return neighbors_1[4] * (((weights[0] * neighbors_0[0]) / neighbors_1[0]) + ((weights[2] * neighbors_0[2]) / neighbors_1[2]) + ((weights[5] * neighbors_0[6]) / neighbors_1[6]) + ((weights[7] * neighbors_0[8]) / neighbors_1[8])) / (weights[0] + weights[2] + weights[5] + weights[7])
\ No newline at end of file
import numpy as np
from .find_neighbors import find_neighbors
from .find_direct_neighbors import find_direct_neighbors
from .find_weights import find_weights
from .interpolate import interpolate
from .interpolate_neighboring import interpolate_neighboring
from .clip_values import clip_values
def process_blue_channel(img: np.ndarray, N: int, M: int) -> np.ndarray:
for i in range(0, N, 2):
for j in range(1, M, 2):
neighbors_0 = find_neighbors(img, 2, i, j, N, M)
neighbors_1 = find_neighbors(img, 1, i, j, N, M)
direct_neighbors = find_direct_neighbors(neighbors_1)
weights = find_weights(img, direct_neighbors, 1, i, j, N, M)
img[i, j, 2] = interpolate_neighboring(neighbors_0, neighbors_1, weights)
for i in range(N):
for j in range(M):
if(img[i, j, 2] == 0):
neighbors = find_neighbors(img, 2, i, j, N, M)
direct_neighbors = find_direct_neighbors(neighbors)
weights = find_weights(img, direct_neighbors, 2, i, j, N, M)
img[i, j, 2] = interpolate(neighbors, weights)
img = clip_values(img)
return img
\ No newline at end of file
import numpy as np
from .find_neighbors import find_neighbors
from .find_direct_neighbors import find_direct_neighbors
from .find_weights import find_weights
from .interpolate import interpolate
from .clip_values import clip_values
def process_green_channel(img: np.ndarray, N: int, M: int) -> np.ndarray:
"""Process the green channel of an image
Args:
img (np.ndarray): image to process
N (int): height of the image
M (int): width of the image
Returns:
np.ndarray: processed image
"""
for i in range(N):
for j in range(M):
if(img[i, j, 1] == 0):
neighbors = find_neighbors(img, 1, i, j, N, M)
direct_neighbors = find_direct_neighbors(neighbors)
weights = find_weights(img, direct_neighbors, 1, i, j, N, M)
img[i, j, 1] = interpolate(neighbors, weights)
img = clip_values(img)
return img
\ No newline at end of file
import numpy as np
from .find_neighbors import find_neighbors
from .find_direct_neighbors import find_direct_neighbors
from .find_weights import find_weights
from .interpolate import interpolate
from .interpolate_neighboring import interpolate_neighboring
from .clip_values import clip_values
def process_red_channel(img: np.ndarray, N: int, M: int) -> np.ndarray:
"""Process the red channel of an image
Args:
img (np.ndarray): image to process
N (int): height of the image
M (int): width of the image
Returns:
np.ndarray: processed image
"""
for i in range(1, N, 2):
for j in range(0, M, 2):
neighbors_0 = find_neighbors(img, 0, i, j, N, M)
neighbors_1 = find_neighbors(img, 1, i, j, N, M)
direct_neighbors = find_direct_neighbors(neighbors_1)
weights = find_weights(img, direct_neighbors, 1, i, j, N, M)
img[i, j, 0] = interpolate_neighboring(neighbors_0, neighbors_1, weights)
for i in range(N):
for j in range(M):
if(img[i, j, 0] == 0):
neighbors = find_neighbors(img, 0, i, j, N, M)
direct_neighbors = find_direct_neighbors(neighbors)
weights = find_weights(img, direct_neighbors, 0, i, j, N, M)
img[i, j, 0] = interpolate(neighbors, weights)
img = clip_values(img)
return img
\ 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
from src.forward_model import CFA
from src.methods.RHOUCH_Oussama.process_green_channel import process_green_channel
from src.methods.RHOUCH_Oussama.process_red_channel import process_red_channel
from src.methods.RHOUCH_Oussama.process_blue_channel import process_blue_channel
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.
input_shape = (y.shape[0], y.shape[1], 3)
op = CFA(cfa, input_shape)
img_restored = op.adjoint(y)
N, M = img_restored.shape[:2]
img_restored = process_green_channel(img_restored, N, M)
img_restored = process_red_channel(img_restored, N, M)
img_restored = process_blue_channel(img_restored, N, M)
return img_restored
####
####
####
#### #### #### #############
#### ###### #### ##################
#### ######## #### ####################
#### ########## #### #### ########
#### ############ #### #### ####
#### #### ######## #### #### ####
#### #### ######## #### #### ####
#### #### ######## #### #### ####
#### #### ## ###### #### #### ######
#### #### #### ## #### #### ############
#### #### ###### #### #### ##########
#### #### ########## #### #### ########
#### #### ######## #### ####
#### #### ############ ####
#### #### ########## ####
#### #### ######## ####
#### #### ###### ####
# 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