# -*- coding: utf-8 -*-
"""
Bayer CFA Masks
===============
*Bayer* CFA (Colour Filter Array) masks generation.
"""
from __future__ import division, unicode_literals
import numpy as np
__author__ = 'Colour Developers'
__copyright__ = 'Copyright (C) 2015-2019 - Colour Developers'
__license__ = 'New BSD License - http://opensource.org/licenses/BSD-3-Clause'
__maintainer__ = 'Colour Developers'
__email__ = 'colour-science@googlegroups.com'
__status__ = 'Production'
__all__ = ['masks_CFA_Bayer']
[docs]def masks_CFA_Bayer(shape, pattern='RGGB'):
"""
Returns the *Bayer* CFA red, green and blue masks for given pattern.
Parameters
----------
shape : array_like
Dimensions of the *Bayer* CFA.
pattern : unicode, optional
**{'RGGB', 'BGGR', 'GRBG', 'GBRG'}**,
Arrangement of the colour filters on the pixel array.
Returns
-------
tuple
*Bayer* CFA red, green and blue masks.
Examples
--------
>>> from pprint import pprint
>>> shape = (3, 3)
>>> pprint(masks_CFA_Bayer(shape))
(array([[ True, False, True],
[False, False, False],
[ True, False, True]], dtype=bool),
array([[False, True, False],
[ True, False, True],
[False, True, False]], dtype=bool),
array([[False, False, False],
[False, True, False],
[False, False, False]], dtype=bool))
>>> pprint(masks_CFA_Bayer(shape, 'BGGR'))
(array([[False, False, False],
[False, True, False],
[False, False, False]], dtype=bool),
array([[False, True, False],
[ True, False, True],
[False, True, False]], dtype=bool),
array([[ True, False, True],
[False, False, False],
[ True, False, True]], dtype=bool))
"""
pattern = pattern.upper()
channels = dict((channel, np.zeros(shape)) for channel in 'RGB')
for channel, (y, x) in zip(pattern, [(0, 0), (0, 1), (1, 0), (1, 1)]):
channels[channel][y::2, x::2] = 1
return tuple(channels[c].astype(bool) for c in 'RGB')