Beispiel #1
0
def Demosaic(imgs):
    demosaicedArray = []
    for img in imgs:
        img = mosaicing_CFA_Bayer(img)
        img = demosaicing_CFA_Bayer_Malvar2004(img)
        demosaicedArray.append(img)
    return demosaicedArray
Beispiel #2
0
    def bay2rgb(self, method=2):

        # print status
        self.sta.status_msg('Debayering', self.cfg.params[self.cfg.opt_prnt])
        self.sta.progress(None, self.cfg.params[self.cfg.opt_prnt])

        # Bayer to RGB conversion
        if method == 0:
            self._rgb_img = demosaicing_CFA_Bayer_bilinear(
                self._bay_img.astype(np.float32), self.cfg.lfpimg['bay'])
        elif method == 1:
            self._rgb_img = demosaicing_CFA_Bayer_Malvar2004(
                self._bay_img.astype(np.float32), self.cfg.lfpimg['bay'])
        else:
            self._rgb_img = demosaicing_CFA_Bayer_Menon2007(
                self._bay_img.astype(np.float32), self.cfg.lfpimg['bay'])

        # clip intensities above and below previous limits (removing dead and hot outliers yields much better contrast)
        self._rgb_img[
            self._rgb_img < self._bay_img.min()] = self._bay_img.min()
        self._rgb_img[
            self._rgb_img > self._bay_img.max()] = self._bay_img.max()

        # print "Progress: Done!"
        self.sta.progress(100, self.cfg.params[self.cfg.opt_prnt])

        return True
Beispiel #3
0
    def bay2rgb(self, method=2):

        # print status
        self.sta.status_msg('Debayering', self.cfg.params[self.cfg.opt_prnt])
        self.sta.progress(None, self.cfg.params[self.cfg.opt_prnt])

        # Bayer to RGB conversion
        if method == 0:
            self._rgb_img = demosaicing_CFA_Bayer_bilinear(
                self._bay_img, self.cfg.lfpimg['bay'])
        elif method == 1:
            self._rgb_img = demosaicing_CFA_Bayer_Malvar2004(
                self._bay_img, self.cfg.lfpimg['bay'])
        else:
            self._rgb_img = demosaicing_CFA_Bayer_Menon2007(
                self._bay_img, self.cfg.lfpimg['bay'])

        # normalize image
        min = np.percentile(self._rgb_img, 0.05)
        max = np.max(self.rgb_img)
        self._rgb_img = misc.Normalizer(self._rgb_img, min=min,
                                        max=max).type_norm()

        # update status message
        self.sta.progress(100, self.cfg.params[self.cfg.opt_prnt])

        return True
Beispiel #4
0
def get_demosaiced(img: ndarray,
                   pattern: str = 'GRBG',
                   method: str = 'bilinear') -> ndarray:
    """Get a demosaiced RGB image from a raw image.

    This function is a wrapper of the demosaicing functions supplied by the
    ``color_demosaicing`` package.

    Args:
        img: Input image, greyscale, of shape (x,y).

        pattern: Bayer filter pattern that the input image is modulated with.
            Patterns are: 'RGGB', 'BGGR', 'GRBG', 'GBRG'.

            Default: 'GRBG'

        method: Algorithm used to calculate the demosaiced image.\n
            * 'bilinear': Simple bilinear interpolation of color values
            * 'malvar2004': Algorithm introduced by Malvar et. al. [R3]_
            * 'menon2007': Algorithm introduced by Menon et. al. [R4]_,


    Returns:
        Demosaiced RGB-color image of shape (x,y,3) of
        dtype :class:`numpy.float64`.

    References:
        .. [R3]  H.S. Malvar,  Li-wei He, and  R. Cutler (2004).
           High-quality linear interpolation for demosaicing of
           Bayer-patterned color images.
           IEEE International Conference on Acoustics, Speech, and Signal
           Processing, Proceedings. (ICASSP '04).
           DOI: 10.1109/ICASSP.2004.1326587

        .. [R4]  D. Menon, S. Andriani, G. Calvagno (2007).
           Demosaicing With Directional Filtering and a posteriori Decision.
           IEEE Transactions on Image Processing (Volume: 16, Issue: 1)
           DOI: 10.1109/TIP.2006.884928

    """

    param_list = ["bilinear", "malvar2004", "menon2007"]

    # Do demosaicing with specified method
    if method not in param_list:
        raise ValueError(
            f"The specified method {method} is none of the supported "
            f"methods: {param_list}.")

    elif method == "bilinear":
        return demosaicing_CFA_Bayer_bilinear(img.astype(np.float64),
                                              pattern=pattern)

    elif method == "malvar2004":
        return demosaicing_CFA_Bayer_Malvar2004(img.astype(np.float64),
                                                pattern=pattern)

    elif method == "menon2007":
        return demosaicing_CFA_Bayer_Menon2007(img.astype(np.float64),
                                               pattern=pattern)
Beispiel #5
0
def debayer_image_array(data, algorithm='bilinear', pattern='GRBG'):
    """ Returns the RGB data after bilinear interpolation on the given array.
        ----------
        parameters  
        ----------
        data : The input array containing the image data. Array like of shape (rows,columns)
        algorithm : The algorithm to use for the debayering operation. 
        {'bilinear','malvar2004','menon2007'}
        ----------
        returns
        ----------
        numpy array of shape (rows,columns,3)
    """
    # Check to see if data is two dimensional
    try:
        assert len(data.shape) == 2, 'Shape is not 2 dimensional'
    except AssertionError:
        log_error_exit('Image data input to debayer is not 2 dimensional')

    if algorithm == 'bilinear':
        rgb_data = demosaicing_CFA_Bayer_bilinear(data, pattern)
    elif algorithm == 'malvar2004':
        rgb_data = demosaicing_CFA_Bayer_Malvar2004(data, pattern)
    elif algorithm == 'menon2007':
        rgb_data = demosaicing_CFA_Bayer_Menon2007(data, pattern)
    return rgb_data.astype(np.uint16)
Beispiel #6
0
def demosaic(img, pattern):
    '''
    Input:
        img: H*W numpy array, input RAW image.
        pattern: string, 4 different Bayer patterns (GRBG, RGGB, GBRG, BGGR)
    Output:
        output: H*W*3 numpy array, output de-mosaic image.
    '''
    #### Using Python colour_demosaicing library
    #### You can write your own version, too
    output = demosaicing_CFA_Bayer_Malvar2004(img, pattern)
    output = np.clip(output, 0, 1)

    return output
def x0_demosaic(f_bayer, bayer_pattern):
    """
    Applies a simple demosaicking step to

    :param f_bayer: Input bayer image
    :type f_bayer: np.ndarray
    :param bayer_pattern: Pattern of the f_bayer image
    :type bayer_pattern: String

    :returns: Demosaicked output
    :rtype: np.ndarray
    """
    x0 = np.clip(
        demosaicing_CFA_Bayer_Malvar2004(np.sum(f_bayer, axis=2),
                                         pattern=bayer_pattern), 0.0, 1.0)
    return x0
def demosaicking(image: np.ndarray,
                 method: str = "bilinear",
                 pattern: str = "RGGB") -> np.ndarray:
    """Returns the demosaicked image given a method.

    Parameters
    -------------------
    image: np.ndarray,
        The image to be demosaicked.
    method: str,
        Demosaicking method to be applied.
    pattern: str,
        Arrangement of the colour filters on the pixel array.
        Possible patterns are: {RGGB, BGGR, GRBG, GBRG}.

    Raises
    ------------------
    ValueError,
        If given method does not exist.

    Returns
    -------------------
    Returns the demosaicked image.
    """
    image_rgb = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
    image_cfa = mosaicing_CFA_Bayer(image_rgb, pattern=pattern) / 255

    if method == 'bilinear':
        image_demo = ((cctf_encoding(
            demosaicing_CFA_Bayer_bilinear(image_cfa, pattern=pattern))) *
                      255).astype(np.uint8)
    elif method == 'malvar':
        image_demo = ((cctf_encoding(
            demosaicing_CFA_Bayer_Malvar2004(image_cfa, pattern=pattern))) *
                      255).astype(np.uint8)
    elif method == 'menon':
        image_demo = ((cctf_encoding(
            demosaicing_CFA_Bayer_Menon2007(image_cfa, pattern=pattern))) *
                      255).astype(np.uint8)
    else:
        raise ValueError(
            'Given method \'{}\' does not belong to possible methods. '
            'Valid methods are: \'bilinear\', \'malvar\' and \'menon\'.'.
            format(method))

    return cv2.cvtColor(image_demo, cv2.COLOR_RGB2GRAY)
Beispiel #9
0
desc_list = ["Bilinear", "Malvar (2004)", "DDFAPD", "Learned"]

# Indexing:  Algorithm - Test Image - Row - Column - RGB channel
results = np.zeros((len(desc_list), dataset.shape[0], dataset.shape[1],
                    dataset.shape[2], dataset.shape[3]))

model.compile(optimizer='adam', loss='mean_squared_error')
model.load_weights("checkpoints/weights.100-0.00.hdf5")
model.summary()
results_nn = model.predict(dataset)

for i in range(dataset.shape[0]):
    results[0][i] = demosaicing_CFA_Bayer_bilinear(dataset_mosaiced[i],
                                                   pattern="RGGB")
    results[1][i] = demosaicing_CFA_Bayer_Malvar2004(dataset_mosaiced[i],
                                                     pattern="RGGB")
    results[2][i] = demosaicing_CFA_Bayer_DDFAPD(dataset_mosaiced[i],
                                                 pattern="RGGB")
    results[3][i] = results_nn[i]

psnrs = np.zeros((dataset.shape[0], len(desc_list)))

for alg_idx in range(len(desc_list)):
    for img_idx in range(dataset.shape[0]):
        psnrs[img_idx][alg_idx] = calc_cpsnr(dataset[img_idx],
                                             results[alg_idx][img_idx])

for img_idx in range(dataset.shape[0]):
    print(files_grabbed[img_idx] + ":")
    for alg_idx in range(len(desc_list)):
        print(desc_list[alg_idx] + ": " + str(psnrs[img_idx][alg_idx]) + " dB")