Exemple #1
0
def mosaic(im, pattern='RGGB'):
    """
    im should have rgb as 012 channels. And have shapes as (height, width, 3)
    Returns an image of the bayer filter pattern.
    """
    mosaiced = mosaicing_CFA_Bayer(im, pattern=pattern)
    return mosaiced
Exemple #2
0
 def _mosaic(self, img):
     cfa = np.zeros(img.shape, np.uint8)
     mosaic = mosaicing_CFA_Bayer(img)
     for i in range(3):
         cfa[:, :, i] = mosaic
     #cfa[:, :, 0] = mosaic
     return cfa
Exemple #3
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
Exemple #4
0
def mosaic(img, pattern):
    '''
    Input:
        img: H*W*3 numpy array, input image.
        pattern: string, 4 different Bayer patterns (GRBG, RGGB, GBRG, BGGR)
    Output:
        output: H*W numpy array, output image after mosaic.
    '''
    ########################################################################
    # TODO:                                                                #
    #   1. Create the H*W output numpy array.                              #
    #   2. Discard other two channels from input 3-channel image according #
    #      to given Bayer pattern.                                         #
    #                                                                      #
    #   e.g. If Bayer pattern now is BGGR, for the upper left pixel from   #
    #        each four-pixel square, we should discard R and G channel     #
    #        and keep B channel of input image.                            #
    #        (since upper left pixel is B in BGGR bayer pattern)           #
    ########################################################################

    output = mosaicing_CFA_Bayer(img, pattern)

    ########################################################################
    #                                                                      #
    #                           End of your code                           #
    #                                                                      #
    ########################################################################

    return output
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)
def mosaic(img):
    return mosaicing_CFA_Bayer(img, pattern=PATTERN)
print("Reading images...")
dataset = np.zeros(
    (len(files_grabbed), nn_m.image_shape[0], nn_m.image_shape[1], 3))

idx = 0
for file in files_grabbed:
    dataset[idx] = ndimage.imread(file, mode='RGB').astype(
        np.float32) / 255.0  # Normalize images 0-1
    idx += 1

# Create a RGGB-mosaiced version of the input dataset to compare against more traditional demosaicing
dataset_mosaiced = np.zeros(
    (dataset.shape[0], dataset.shape[1], dataset.shape[2]))

for i in range(dataset.shape[0]):
    dataset_mosaiced[i] = mosaicing_CFA_Bayer(dataset[i], pattern="RGGB")

# ==================== Evaluation ====================

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]):
    def synthesize_noise(self,
                         img,
                         max_s=25 / 255,
                         max_c=25 / 255,
                         min_s=0,
                         min_c=0):
        channel = img.shape[2]  # W H C: rgb
        if self.sigma_s is None:
            np.random.seed(seed=None)
            sigma_s = np.random.uniform(min_s, max_s, (1, 1, channel))
        else:
            sigma_s = self.sigma_s
        if self.sigma_c is None:
            np.random.seed(seed=None)
            sigma_c = np.random.uniform(min_c, max_c, (1, 1, channel))
        else:
            sigma_c = self.sigma_c
        if self.crf_index is None:
            np.random.seed(seed=None)
            crf_index = random.randint(0, 200)
        else:
            crf_index = self.crf_index
        if self.pattern is None:
            np.random.seed(seed=None)
            pattern = random.randint(0, 5)
        else:
            pattern = self.pattern

        I = self.data_I_B['I'][crf_index, :].tolist()
        B = self.data_I_B['B'][crf_index, :].tolist()
        invI = self.data_invI_invB['invI'][crf_index, :].tolist()
        invB = self.data_invI_invB['invB'][crf_index, :].tolist()

        # x-->L
        temp_x = self.ICRF_Map(img, invI, invB)

        # adding noise
        noise_s_map = np.tile(sigma_s,
                              (temp_x.shape[0], temp_x.shape[1], 1)) * temp_x
        if self.mode == 'Test':
            np.random.seed(seed=0)  # for reproducibility
            noise_s = np.random.normal(0, 1, temp_x.shape) * noise_s_map
        else:
            np.random.seed(seed=None)
            noise_s = np.random.normal(0, 1, temp_x.shape) * noise_s_map

        noise_c_map = np.tile(sigma_c, (temp_x.shape[0], temp_x.shape[1], 1))
        if self.mode == 'Test':
            np.random.seed(seed=0)  # for reproducibility
            noise_c = np.random.normal(0, 1, temp_x.shape) * noise_c_map
        else:
            np.random.seed(seed=None)
            noise_c = np.random.normal(0, 1, temp_x.shape) * noise_c_map

        temp_n = temp_x + noise_s + noise_c
        noise_map = np.sqrt(noise_s_map + noise_c_map)

        if self.corr:
            # L-->x
            temp_x = self.CRF_Map(temp_x, I, B)

            # add Mosai
            if pattern == 0:
                B_b_x = mosaicing_CFA_Bayer(temp_x, 'GBRG')
            elif pattern == 1:
                B_b_x = mosaicing_CFA_Bayer(temp_x, 'GRBG')
            elif pattern == 2:
                B_b_x = mosaicing_CFA_Bayer(temp_x, 'BGGR')
            elif pattern == 3:
                B_b_x = mosaicing_CFA_Bayer(temp_x, 'RGGB')
            else:
                B_b_x = temp_x
            temp_x = B_b_x

            # DeMosai
            if pattern == 0:
                lin_rgb_x = demosaicing_CFA_Bayer_bilinear(temp_x, 'GBRG')
            elif pattern == 1:
                lin_rgb_x = demosaicing_CFA_Bayer_bilinear(temp_x, 'GRBG')
            elif pattern == 2:
                lin_rgb_x = demosaicing_CFA_Bayer_bilinear(temp_x, 'BGGR')
            elif pattern == 3:
                lin_rgb_x = demosaicing_CFA_Bayer_bilinear(temp_x, 'RGGB')
            else:
                lin_rgb_x = temp_x
            temp_x = lin_rgb_x

        # L-->x
        temp_n = self.CRF_Map(temp_n, I, B)

        # add Mosai
        if pattern == 0:
            B_b_n = mosaicing_CFA_Bayer(temp_n, 'GBRG')
        elif pattern == 1:
            B_b_n = mosaicing_CFA_Bayer(temp_n, 'GRBG')
        elif pattern == 2:
            B_b_n = mosaicing_CFA_Bayer(temp_n, 'BGGR')
        elif pattern == 3:
            B_b_n = mosaicing_CFA_Bayer(temp_n, 'RGGB')
        else:
            B_b_n = temp_n
        temp_n = B_b_n

        # DeMosai
        if pattern == 0:
            lin_rgb_n = demosaicing_CFA_Bayer_bilinear(temp_n, 'GBRG')
        elif pattern == 1:
            lin_rgb_n = demosaicing_CFA_Bayer_bilinear(temp_n, 'GRBG')
        elif pattern == 2:
            lin_rgb_n = demosaicing_CFA_Bayer_bilinear(temp_n, 'BGGR')
        elif pattern == 3:
            lin_rgb_n = demosaicing_CFA_Bayer_bilinear(temp_n, 'RGGB')
        else:
            lin_rgb_n = temp_n
        temp_n = lin_rgb_n

        if self.corr:
            y = temp_n - temp_x + img
        else:
            y = temp_n
        return y, noise_map
def generateDemosaicWithGaussianPoissonNoiseSTD(img, std):
    gausspois_out = generateGaussianRandomVarNoiseChannelSpecificSTD(img, std)
    tmp_mosaic = mosaicing_CFA_Bayer(gausspois_out)
    tmp_demosaic = demosaicing_CFA_Bayer_bilinear(tmp_mosaic)
    return np.clip(tmp_demosaic, 0, 1)