Skip to content

Commit

Permalink
Format "colour_demosaicing" package.
Browse files Browse the repository at this point in the history
  • Loading branch information
KelSolaar committed Jul 4, 2017
1 parent 9731a11 commit 61e0151
Show file tree
Hide file tree
Showing 14 changed files with 169 additions and 227 deletions.
15 changes: 7 additions & 8 deletions colour_demosaicing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Colour - Demosaicing
====================
Expand Down Expand Up @@ -29,18 +28,18 @@
__all__ = []
__all__ += bayer.__all__

RESOURCES_DIRECTORY = os.path.join(
os.path.dirname(__file__), 'resources')
RESOURCES_DIRECTORY = os.path.join(os.path.dirname(__file__), 'resources')
EXAMPLES_RESOURCES_DIRECTORY = os.path.join(
RESOURCES_DIRECTORY, 'colour-demosaicing-examples-dataset')
TESTS_RESOURCES_DIRECTORY = os.path.join(
RESOURCES_DIRECTORY, 'colour-demosaicing-tests-dataset')
TESTS_RESOURCES_DIRECTORY = os.path.join(RESOURCES_DIRECTORY,
'colour-demosaicing-tests-dataset')

__application_name__ = 'Colour - Demosaicing'

__major_version__ = '0'
__minor_version__ = '1'
__change_version__ = '2'
__version__ = '.'.join((__major_version__,
__minor_version__,
__change_version__))
__version__ = '.'.join(
(__major_version__,
__minor_version__,
__change_version__)) # yapf: disable
5 changes: 2 additions & 3 deletions colour_demosaicing/bayer/demosaicing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@

from .bilinear import demosaicing_CFA_Bayer_bilinear
from .malvar2004 import demosaicing_CFA_Bayer_Malvar2004
from .menon2007 import (
demosaicing_CFA_Bayer_DDFAPD,
demosaicing_CFA_Bayer_Menon2007)
from .menon2007 import (demosaicing_CFA_Bayer_DDFAPD,
demosaicing_CFA_Bayer_Menon2007)

__all__ = []
__all__ += ['demosaicing_CFA_Bayer_bilinear']
Expand Down
15 changes: 8 additions & 7 deletions colour_demosaicing/bayer/demosaicing/bilinear.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Bilinear Bayer CFA Demosaicing
==============================
Expand Down Expand Up @@ -61,8 +60,9 @@ def demosaicing_CFA_Bayer_bilinear(CFA, pattern='RGGB'):
Examples
--------
>>> CFA = np.array([[0.30980393, 0.36078432, 0.30588236, 0.3764706],
... [0.35686275, 0.39607844, 0.36078432, 0.40000001]])
>>> CFA = np.array(
... [[0.30980393, 0.36078432, 0.30588236, 0.3764706],
... [0.35686275, 0.39607844, 0.36078432, 0.40000001]])
>>> demosaicing_CFA_Bayer_bilinear(CFA)
array([[[ 0.69705884, 0.17941177, 0.09901961],
[ 0.46176472, 0.4509804 , 0.19803922],
Expand All @@ -73,8 +73,9 @@ def demosaicing_CFA_Bayer_bilinear(CFA, pattern='RGGB'):
[ 0.15392157, 0.26960785, 0.59411766],
[ 0.15294118, 0.4509804 , 0.59705884],
[ 0.07647059, 0.18431373, 0.90000002]]])
>>> CFA = np.array([[0.3764706, 0.360784320, 0.40784314, 0.3764706],
... [0.35686275, 0.30980393, 0.36078432, 0.29803923]])
>>> CFA = np.array(
... [[0.3764706, 0.360784320, 0.40784314, 0.3764706],
... [0.35686275, 0.30980393, 0.36078432, 0.29803923]])
>>> demosaicing_CFA_Bayer_bilinear(CFA, 'BGGR')
array([[[ 0.07745098, 0.17941177, 0.84705885],
[ 0.15490197, 0.4509804 , 0.5882353 ],
Expand All @@ -93,12 +94,12 @@ def demosaicing_CFA_Bayer_bilinear(CFA, pattern='RGGB'):
H_G = np.asarray(
[[0, 1, 0],
[1, 4, 1],
[0, 1, 0]]) / 4
[0, 1, 0]]) / 4 # yapf: disable

H_RB = np.asarray(
[[1, 2, 1],
[2, 4, 2],
[1, 2, 1]]) / 4
[1, 2, 1]]) / 4 # yapf: disable

R = convolve(CFA * R_m, H_RB)
G = convolve(CFA * G_m, H_G)
Expand Down
17 changes: 9 additions & 8 deletions colour_demosaicing/bayer/demosaicing/malvar2004.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Malvar (2004) Bayer CFA Demosaicing
===================================
Expand Down Expand Up @@ -64,8 +63,9 @@ def demosaicing_CFA_Bayer_Malvar2004(CFA, pattern='RGGB'):
Examples
--------
>>> CFA = np.array([[0.30980393, 0.36078432, 0.30588236, 0.3764706],
... [0.35686275, 0.39607844, 0.36078432, 0.40000001]])
>>> CFA = np.array(
... [[0.30980393, 0.36078432, 0.30588236, 0.3764706],
... [0.35686275, 0.39607844, 0.36078432, 0.40000001]])
>>> demosaicing_CFA_Bayer_Malvar2004(CFA)
array([[[ 0.30980393, 0.31666668, 0.32941177],
[ 0.33039216, 0.36078432, 0.38112746],
Expand All @@ -76,8 +76,9 @@ def demosaicing_CFA_Bayer_Malvar2004(CFA, pattern='RGGB'):
[ 0.35318628, 0.38186275, 0.39607844],
[ 0.3379902 , 0.36078432, 0.3754902 ],
[ 0.37769609, 0.39558825, 0.40000001]]])
>>> CFA = np.array([[0.3764706, 0.360784320, 0.40784314, 0.3764706],
... [0.35686275, 0.30980393, 0.36078432, 0.29803923]])
>>> CFA = np.array(
... [[0.3764706, 0.360784320, 0.40784314, 0.3764706],
... [0.35686275, 0.30980393, 0.36078432, 0.29803923]])
>>> demosaicing_CFA_Bayer_Malvar2004(CFA, 'BGGR')
array([[[ 0.35539217, 0.37058825, 0.3764706 ],
[ 0.34264707, 0.36078432, 0.37450981],
Expand All @@ -98,14 +99,14 @@ def demosaicing_CFA_Bayer_Malvar2004(CFA, pattern='RGGB'):
[0, 0, 2, 0, 0],
[-1, 2, 4, 2, -1],
[0, 0, 2, 0, 0],
[0, 0, -1, 0, 0]]) / 8
[0, 0, -1, 0, 0]]) / 8 # yapf: disable

Rg_RB_Bg_BR = np.asarray(
[[0, 0, 0.5, 0, 0],
[0, -1, 0, -1, 0],
[-1, 4, 5, 4, - 1],
[0, -1, 0, -1, 0],
[0, 0, 0.5, 0, 0]]) / 8
[0, 0, 0.5, 0, 0]]) / 8 # yapf: disable

Rg_BR_Bg_RB = np.transpose(Rg_RB_Bg_BR)

Expand All @@ -114,7 +115,7 @@ def demosaicing_CFA_Bayer_Malvar2004(CFA, pattern='RGGB'):
[0, 2, 0, 2, 0],
[-1.5, 0, 6, 0, -1.5],
[0, 2, 0, 2, 0],
[0, 0, -1.5, 0, 0]]) / 8
[0, 0, -1.5, 0, 0]]) / 8 # yapf: disable

R = CFA * R_m
G = CFA * G_m
Expand Down
130 changes: 64 additions & 66 deletions colour_demosaicing/bayer/demosaicing/menon2007.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-


"""
DDFAPD - Menon (2007) Bayer CFA Demosaicing
===========================================
Expand Down Expand Up @@ -31,9 +29,10 @@
__email__ = 'colour-science@googlegroups.com'
__status__ = 'Production'

__all__ = ['demosaicing_CFA_Bayer_Menon2007',
'demosaicing_CFA_Bayer_DDFAPD',
'refining_step_Menon2007']
__all__ = [
'demosaicing_CFA_Bayer_Menon2007', 'demosaicing_CFA_Bayer_DDFAPD',
'refining_step_Menon2007'
]


def _cnv_h(x, y):
Expand Down Expand Up @@ -83,8 +82,9 @@ def demosaicing_CFA_Bayer_Menon2007(CFA, pattern='RGGB', refining_step=True):
Examples
--------
>>> CFA = np.array([[ 0.30980393, 0.36078432, 0.30588236, 0.3764706 ],
... [ 0.35686275, 0.39607844, 0.36078432, 0.40000001]])
>>> CFA = np.array(
... [[ 0.30980393, 0.36078432, 0.30588236, 0.3764706 ],
... [ 0.35686275, 0.39607844, 0.36078432, 0.40000001]])
>>> demosaicing_CFA_Bayer_Menon2007(CFA)
array([[[ 0.30980393, 0.35686275, 0.39215687],
[ 0.30980393, 0.36078432, 0.39607844],
Expand All @@ -95,8 +95,9 @@ def demosaicing_CFA_Bayer_Menon2007(CFA, pattern='RGGB', refining_step=True):
[ 0.30980393, 0.36078432, 0.39607844],
[ 0.30588236, 0.36078432, 0.39019609],
[ 0.32156864, 0.3764706 , 0.40000001]]])
>>> CFA = np.array([[ 0.3764706 , 0.36078432, 0.40784314, 0.3764706 ],
... [ 0.35686275, 0.30980393, 0.36078432, 0.29803923]])
>>> CFA = np.array(
... [[ 0.3764706 , 0.36078432, 0.40784314, 0.3764706 ],
... [ 0.35686275, 0.30980393, 0.36078432, 0.29803923]])
>>> demosaicing_CFA_Bayer_Menon2007(CFA, 'BGGR')
array([[[ 0.30588236, 0.35686275, 0.3764706 ],
[ 0.30980393, 0.36078432, 0.39411766],
Expand Down Expand Up @@ -128,17 +129,17 @@ def demosaicing_CFA_Bayer_Menon2007(CFA, pattern='RGGB', refining_step=True):
C_V = np.where(R_m == 1, R - G_V, 0)
C_V = np.where(B_m == 1, B - G_V, C_V)

D_H = np.abs(C_H -
np.pad(C_H, ((0, 0), (0, 2)), mode=str('reflect'))[:, 2:])
D_V = np.abs(C_V -
np.pad(C_V, ((0, 2), (0, 0)), mode=str('reflect'))[2:, :])
D_H = np.abs(C_H - np.pad(C_H, ((0, 0), (0, 2)),
mode=str('reflect'))[:, 2:])
D_V = np.abs(C_V - np.pad(C_V, ((0, 2), (0, 0)),
mode=str('reflect'))[2:, :])

k = np.array(
[[0, 0, 1, 0, 1],
[0, 0, 0, 1, 0],
[0, 0, 3, 0, 3],
[0, 0, 0, 1, 0],
[0, 0, 1, 0, 1]])
[0, 0, 1, 0, 1]]) # yapf: disable

d_H = convolve(D_H, k, mode='constant')
d_V = convolve(D_V, np.transpose(k), mode='constant')
Expand All @@ -154,33 +155,31 @@ def demosaicing_CFA_Bayer_Menon2007(CFA, pattern='RGGB', refining_step=True):

k_b = np.array([0.5, 0, 0.5])

R = np.where(np.logical_and(G_m == 1, R_r == 1),
G + _cnv_h(R, k_b) - _cnv_h(G, k_b),
R)
R = np.where(
np.logical_and(G_m == 1, R_r == 1),
G + _cnv_h(R, k_b) - _cnv_h(G, k_b), R)

R = np.where(np.logical_and(G_m == 1, B_r == 1) == 1,
G + _cnv_v(R, k_b) - _cnv_v(G, k_b),
R)
R = np.where(
np.logical_and(G_m == 1, B_r == 1) == 1,
G + _cnv_v(R, k_b) - _cnv_v(G, k_b), R)

B = np.where(np.logical_and(G_m == 1, B_r == 1),
G + _cnv_h(B, k_b) - _cnv_h(G, k_b),
B)
B = np.where(
np.logical_and(G_m == 1, B_r == 1),
G + _cnv_h(B, k_b) - _cnv_h(G, k_b), B)

B = np.where(np.logical_and(G_m == 1, R_r == 1) == 1,
G + _cnv_v(B, k_b) - _cnv_v(G, k_b),
B)
B = np.where(
np.logical_and(G_m == 1, R_r == 1) == 1,
G + _cnv_v(B, k_b) - _cnv_v(G, k_b), B)

R = np.where(np.logical_and(B_r == 1, B_m == 1),
np.where(M == 1,
B + _cnv_h(R, k_b) - _cnv_h(B, k_b),
B + _cnv_v(R, k_b) - _cnv_v(B, k_b)),
R)
R = np.where(
np.logical_and(B_r == 1, B_m == 1),
np.where(M == 1, B + _cnv_h(R, k_b) - _cnv_h(B, k_b),
B + _cnv_v(R, k_b) - _cnv_v(B, k_b)), R)

B = np.where(np.logical_and(R_r == 1, R_m == 1),
np.where(M == 1,
R + _cnv_h(B, k_b) - _cnv_h(R, k_b),
R + _cnv_v(B, k_b) - _cnv_v(R, k_b)),
B)
B = np.where(
np.logical_and(R_r == 1, R_m == 1),
np.where(M == 1, R + _cnv_h(B, k_b) - _cnv_h(R, k_b),
R + _cnv_v(B, k_b) - _cnv_v(R, k_b)), B)

RGB = tstack((R, G, B))

Expand Down Expand Up @@ -213,24 +212,27 @@ def refining_step_Menon2007(RGB, RGB_m, M):
Examples
--------
>>> RGB = np.array([[[0.30588236, 0.35686275, 0.3764706],
... [0.30980393, 0.36078432, 0.39411766],
... [0.29607844, 0.36078432, 0.40784314],
... [0.29803923, 0.37647060, 0.42352942]],
... [[0.30588236, 0.35686275, 0.3764706],
... [0.30980393, 0.36078432, 0.39411766],
... [0.29607844, 0.36078432, 0.40784314],
... [0.29803923, 0.37647060, 0.42352942]]])
>>> RGB_m = np.array([[[0, 0, 1],
... [0, 1, 0],
... [0, 0, 1],
... [0, 1, 0]],
... [[0, 1, 0],
... [1, 0, 0],
... [0, 1, 0],
... [1, 0, 0]]])
>>> M = np.array([[0, 1, 0, 1],
... [1, 0, 1, 0]])
>>> RGB = np.array(
... [[[0.30588236, 0.35686275, 0.3764706],
... [0.30980393, 0.36078432, 0.39411766],
... [0.29607844, 0.36078432, 0.40784314],
... [0.29803923, 0.37647060, 0.42352942]],
... [[0.30588236, 0.35686275, 0.3764706],
... [0.30980393, 0.36078432, 0.39411766],
... [0.29607844, 0.36078432, 0.40784314],
... [0.29803923, 0.37647060, 0.42352942]]])
>>> RGB_m = np.array(
... [[[0, 0, 1],
... [0, 1, 0],
... [0, 0, 1],
... [0, 1, 0]],
... [[0, 1, 0],
... [1, 0, 0],
... [0, 1, 0],
... [1, 0, 0]]])
>>> M = np.array(
... [[0, 1, 0, 1],
... [1, 0, 1, 0]])
>>> refining_step_Menon2007(RGB, RGB_m, M)
array([[[ 0.30588236, 0.35686275, 0.3764706 ],
[ 0.30980393, 0.36078432, 0.39411765],
Expand Down Expand Up @@ -276,22 +278,18 @@ def refining_step_Menon2007(RGB, RGB_m, M):

k_b = np.array([0.5, 0, 0.5])

R_G_m = np.where(np.logical_and(G_m == 1, B_r == 1),
_cnv_v(R_G, k_b),
R_G_m)
R_G_m = np.where(
np.logical_and(G_m == 1, B_r == 1), _cnv_v(R_G, k_b), R_G_m)
R = np.where(np.logical_and(G_m == 1, B_r == 1), G + R_G_m, R)
R_G_m = np.where(np.logical_and(G_m == 1, B_c == 1),
_cnv_h(R_G, k_b),
R_G_m)
R_G_m = np.where(
np.logical_and(G_m == 1, B_c == 1), _cnv_h(R_G, k_b), R_G_m)
R = np.where(np.logical_and(G_m == 1, B_c == 1), G + R_G_m, R)

B_G_m = np.where(np.logical_and(G_m == 1, R_r == 1),
_cnv_v(B_G, k_b),
B_G_m)
B_G_m = np.where(
np.logical_and(G_m == 1, R_r == 1), _cnv_v(B_G, k_b), B_G_m)
B = np.where(np.logical_and(G_m == 1, R_r == 1), G + B_G_m, B)
B_G_m = np.where(np.logical_and(G_m == 1, R_c == 1),
_cnv_h(B_G, k_b),
B_G_m)
B_G_m = np.where(
np.logical_and(G_m == 1, R_c == 1), _cnv_h(B_G, k_b), B_G_m)
B = np.where(np.logical_and(G_m == 1, R_c == 1), G + B_G_m, B)

# Updating of the red (blue) component in the blue (red) locations.
Expand Down
1 change: 0 additions & 1 deletion colour_demosaicing/bayer/masks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Bayer CFA Masks
===============
Expand Down
1 change: 0 additions & 1 deletion colour_demosaicing/bayer/mosaicing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Bayer CFA Mosaicing
===================
Expand Down

0 comments on commit 61e0151

Please sign in to comment.