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

last commit

parent b6835f47
No related branches found
No related tags found
1 merge request!21Master
Showing
with 211 additions and 288 deletions
This diff is collapsed.
File added
...@@ -9,7 +9,7 @@ def clip_values(img: np.ndarray) -> np.ndarray: ...@@ -9,7 +9,7 @@ def clip_values(img: np.ndarray) -> np.ndarray:
Returns: Returns:
np.ndarray: clipped image np.ndarray: clipped image
""" """
# Clip values between 0 and 1
img[img < 0] = 0 img[img < 0] = 0
img[img > 1] = 1 img[img > 1] = 1
return img return img
import numpy as np import numpy as np
def find_direct_neighbors(neighbors: list) -> list: def find_direct_neighbors(neighbors: list) -> list:
"""Finds a pixel's direct neighbors""" """
Dx = (neighbors[3] - neighbors[5]) / 2 Calculate the gradients in horizontal, vertical, and diagonal directions for a pixel
Dy = (neighbors[1] - neighbors[7]) / 2 based on its neighboring pixels.
Dxx = (neighbors[2] - neighbors[6]) / (2 * np.sqrt(2))
Dyy = (neighbors[0] - neighbors[8]) / (2 * np.sqrt(2)) Args:
neighbors (list): The neighbors of a pixel in a 3x3 grid. The order of neighbors
is assumed to be [top-left, top, top-right, right, center, left,
bottom-left, bottom, bottom-right].
Returns:
list: The gradients [horizontal, vertical, diagonal-x, diagonal-y].
"""
# Horizontal gradient: Difference between the right and left neighbors
horizontal_gradient = (neighbors[3] - neighbors[5]) / 2
# Vertical gradient: Difference between the top and bottom neighbors
vertical_gradient = (neighbors[1] - neighbors[7]) / 2
# Diagonal gradient along x-axis: Difference between top-right and bottom-left neighbors
diagonal_gradient_x = (neighbors[2] - neighbors[6]) / (2 * np.sqrt(2))
# Diagonal gradient along y-axis: Difference between top-left and bottom-right neighbors
diagonal_gradient_y = (neighbors[0] - neighbors[8]) / (2 * np.sqrt(2))
return [Dx, Dy, Dxx, Dyy] return [horizontal_gradient, vertical_gradient, diagonal_gradient_x, diagonal_gradient_y]
\ No newline at end of file
import numpy as np import numpy as np
def find_neighbors(img: np.ndarray, channel: int, i: int, j: int, N: int, M: int) -> list: 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]]) Retrieve the 3x3 neighborhood of a pixel in a specified channel of an image.
The function wraps around the image edges to handle border pixels.
Args:
img (np.ndarray): The image to process.
channel (int): The index of the channel to process.
i (int): The row index of the pixel.
j (int): The column index of the pixel.
N (int): Height of the image.
M (int): Width of the image.
Returns:
np.ndarray: An array of neighbor pixel values in the specified channel.
"""
neighbors = []
for di in [-1, 0, 1]:
for dj in [-1, 0, 1]:
neighbor_i = (i + di) % N
neighbor_j = (j + dj) % M
neighbors.append(img[neighbor_i, neighbor_j, channel])
return np.array(neighbors)
...@@ -3,6 +3,22 @@ from .find_neighbors import find_neighbors ...@@ -3,6 +3,22 @@ from .find_neighbors import find_neighbors
from .find_direct_neighbors import find_direct_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: def find_weights(img: np.ndarray, direct_neighbors: list, channel: int, i: int, j: int, N: int, M: int) -> list:
"""
Find the weights of the neighbors of a pixel in the image.
Args:
img (np.ndarray): The image to process.
direct_neighbors (list): The list of direct neighbors of the pixel.
channel (int): The index of the channel to process.
i (int): The row index of the pixel.
j (int): The column index of the pixel.
N (int): Height of the image.
M (int): Width of the image.
Returns:
list: The list of weights of the neighbors of the pixel.
"""
[Dx, Dy, Dxx, Dyy] = direct_neighbors [Dx, Dy, Dxx, Dyy] = direct_neighbors
E = [] E = []
c = 1 c = 1
...@@ -11,14 +27,20 @@ def find_weights(img: np.ndarray, direct_neighbors: list, channel: int, i: int, ...@@ -11,14 +27,20 @@ def find_weights(img: np.ndarray, direct_neighbors: list, channel: int, i: int,
for l in [-1, 0, 1]: for l in [-1, 0, 1]:
n = find_neighbors(img, channel, i + k, j + l, N, M) n = find_neighbors(img, channel, i + k, j + l, N, M)
dd = find_direct_neighbors(n) dd = find_direct_neighbors(n)
if c == 1 or c == 9:
E.append(1/np.sqrt(1 + Dyy * 2 + dd[3] * 2)) sqrt_arguments = {
elif c == 3 or c == 7: 1: 1 + Dyy * 2 + dd[3] * 2,
E.append(1/np.sqrt(1 + Dxx * 2 + dd[2] * 2)) 3: 1 + Dxx * 2 + dd[2] * 2,
elif c == 2 or c == 8: 2: 1 + Dy * 2 + dd[1] * 2,
E.append(1/np.sqrt(1 + Dy * 2 + dd[1] * 2)) 4: 1 + Dx * 2 + dd[0] * 2
elif c == 4 or c == 6: }
E.append(1/np.sqrt(1 + Dx * 2 + dd[0] * 2))
value = sqrt_arguments.get(c, 1)
if value < 0:
E.append(0)
else:
E.append(1 / np.sqrt(value))
c += 1 c += 1
return E return E
def interpolate(neighbors: list, weights: list) -> float: def interpolate(neighbors: list, weights: list) -> float:
"""
Interpolate the pixel value.
Args:
neighbors (list): The neighbors of the pixel.
weights (list): The weights of the direct neighbors.
Returns:
float: The interpolated value.
"""
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]) 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: def interpolate_neighboring(neighbors_0: list, neighbors_1: list, weights: list) -> float:
"""
Interpolate the neighboring pixels.
Args:
neighbors_0 (list): The neighbors of the pixel in the channel to process.
neighbors_1 (list): The neighbors of the pixel in the green channel.
weights (list): The weights of the direct neighbors.
Returns:
float: The interpolated value.
"""
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]) 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
...@@ -5,24 +5,24 @@ from .find_weights import find_weights ...@@ -5,24 +5,24 @@ from .find_weights import find_weights
from .interpolate import interpolate from .interpolate import interpolate
from .interpolate_neighboring import interpolate_neighboring from .interpolate_neighboring import interpolate_neighboring
from .clip_values import clip_values from .clip_values import clip_values
from .process_channel import process_channel
from .process_interpolation import process_interpolation
def process_blue_channel(img: np.ndarray, N: int, M: int) -> np.ndarray: 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): Process the blue channel of an image
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): Args:
for j in range(M): img (np.ndarray): image to process
if(img[i, j, 2] == 0): N (int): height of the image
neighbors = find_neighbors(img, 2, i, j, N, M) M (int): width of the image
direct_neighbors = find_direct_neighbors(neighbors)
weights = find_weights(img, direct_neighbors, 2, i, j, N, M) Returns:
img[i, j, 2] = interpolate(neighbors, weights) np.ndarray: processed blue channel of the image
"""
img = process_interpolation(img, 2, N, M)
img = process_channel(img, 2, N, M)
img = clip_values(img) img = clip_values(img)
return 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
def process_channel(img: np.ndarray, channel_index: int, N: int, M: int) -> np.ndarray:
"""
Generic function to process a specific channel in the image.
Args:
img (np.ndarray): The image to process.
channel_index (int): The index of the channel to process.
N (int): Height of the image.
M (int): Width of the image.
Returns:
np.ndarray: The processed image.
"""
for i in range(N):
for j in range(M):
if(img[i, j, channel_index] == 0):
neighbors = find_neighbors(img, channel_index, i, j, N, M)
direct_neighbors = find_direct_neighbors(neighbors)
weights = find_weights(img, direct_neighbors, channel_index, i, j, N, M)
img[i, j, channel_index] = interpolate(neighbors, weights)
return img
import numpy as np 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 from .clip_values import clip_values
from .process_channel import process_channel
def process_green_channel(img: np.ndarray, N: int, M: int) -> np.ndarray: def process_green_channel(img: np.ndarray, N: int, M: int) -> np.ndarray:
"""Process the green channel of an image """Process the green channel of an image
...@@ -14,17 +11,10 @@ def process_green_channel(img: np.ndarray, N: int, M: int) -> np.ndarray: ...@@ -14,17 +11,10 @@ def process_green_channel(img: np.ndarray, N: int, M: int) -> np.ndarray:
M (int): width of the image M (int): width of the image
Returns: Returns:
np.ndarray: processed image np.ndarray: processed green channel of the image
""" """
for i in range(N): img = process_channel(img, 1, N, M)
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) img = clip_values(img)
return 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_neighboring import interpolate_neighboring
def process_interpolation(img, channel_index, N, M):
"""
Process specific interpolation for a given channel.
Args:
img (np.ndarray): The image to process.
channel_index (int): The index of the channel to process (0 for red, 2 for blue).
N (int): Height of the image.
M (int): Width of the image.
Returns:
np.ndarray: The image with the specified channel processed.
"""
start_i = 1 if channel_index == 0 else 0 # Start from 1 for red and 0 for blue
start_j = 0 if channel_index == 0 else 1 # Start from 0 for red and 1 for blue
for i in range(start_i, N, 2):
for j in range(start_j, M, 2):
neighbors_0 = find_neighbors(img, channel_index, 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, channel_index] = interpolate_neighboring(neighbors_0, neighbors_1, weights)
return img
import numpy as np 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 from .clip_values import clip_values
from .process_channel import process_channel
from .process_interpolation import process_interpolation
def process_red_channel(img: np.ndarray, N: int, M: int) -> np.ndarray: def process_red_channel(img: np.ndarray, N: int, M: int) -> np.ndarray:
"""Process the red channel of an image """Process the red channel of an image
...@@ -17,22 +14,9 @@ def process_red_channel(img: np.ndarray, N: int, M: int) -> np.ndarray: ...@@ -17,22 +14,9 @@ def process_red_channel(img: np.ndarray, N: int, M: int) -> np.ndarray:
Returns: Returns:
np.ndarray: processed image 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): img = process_interpolation(img, 0, N, M)
for j in range(M): img = process_channel(img, 0, N, 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) img = clip_values(img)
return img return img
\ No newline at end of file
...@@ -21,7 +21,6 @@ def run_reconstruction(y: np.ndarray, cfa: str) -> np.ndarray: ...@@ -21,7 +21,6 @@ def run_reconstruction(y: np.ndarray, cfa: str) -> np.ndarray:
Returns: Returns:
np.ndarray: Demosaicked image. np.ndarray: Demosaicked image.
""" """
# Performing the reconstruction. # Performing the reconstruction.
input_shape = (y.shape[0], y.shape[1], 3) input_shape = (y.shape[0], y.shape[1], 3)
op = CFA(cfa, input_shape) op = CFA(cfa, input_shape)
...@@ -36,8 +35,6 @@ def run_reconstruction(y: np.ndarray, cfa: str) -> np.ndarray: ...@@ -36,8 +35,6 @@ def run_reconstruction(y: np.ndarray, cfa: str) -> np.ndarray:
return img_restored return img_restored
#### ####
#### ####
#### ####
......
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