def run_pipeline_v2(image_or_path,
                    params=None,
                    metadata=None,
                    fix_orient=True):
    params_ = params.copy()
    if type(image_or_path) == str:
        image_path = image_or_path
        # raw image data
        raw_image = get_visible_raw_image(image_path)
        # metadata
        metadata = get_metadata(image_path)
    else:
        raw_image = image_or_path.copy()
        # must provide metadata
        if metadata is None:
            raise ValueError(
                "Must provide metadata when providing image data in first argument."
            )

    current_stage = 'raw'
    current_image = raw_image

    if params_['input_stage'] == current_stage:
        # linearization
        linearization_table = metadata['linearization_table']
        if linearization_table is not None:
            print('Linearization table found. Not handled.')
            # TODO

        current_image = normalize(current_image, metadata['black_level'],
                                  metadata['white_level'])
        params_['input_stage'] = 'normal'

    current_stage = 'normal'

    if params_['output_stage'] == current_stage:
        return current_image

    if params_['input_stage'] == current_stage:
        current_image = white_balance(current_image,
                                      metadata['as_shot_neutral'],
                                      metadata['cfa_pattern'])
        params_['input_stage'] = 'white_balance'

    current_stage = 'white_balance'

    if params_['output_stage'] == current_stage:
        return current_image

    if params_['input_stage'] == current_stage:
        current_image = demosaic(current_image,
                                 metadata['cfa_pattern'],
                                 output_channel_order='BGR',
                                 alg_type=params_['demosaic_type'])
        params_['input_stage'] = 'demosaic'

    current_stage = 'demosaic'

    if params_['output_stage'] == current_stage:
        return current_image

    if params_['input_stage'] == current_stage:
        current_image = apply_color_space_transform(current_image,
                                                    metadata['color_matrix_1'],
                                                    metadata['color_matrix_2'])
        params_['input_stage'] = 'xyz'

    current_stage = 'xyz'

    if params_['output_stage'] == current_stage:
        return current_image

    if params_['input_stage'] == current_stage:
        current_image = transform_xyz_to_srgb(current_image)
        params_['input_stage'] = 'srgb'

    current_stage = 'srgb'

    if fix_orient:
        # fix image orientation, if needed (after srgb stage, ok?)
        current_image = fix_orientation(current_image, metadata['orientation'])

    if params_['output_stage'] == current_stage:
        return current_image

    if params_['input_stage'] == current_stage:
        current_image = apply_gamma(current_image)
        params_['input_stage'] = 'gamma'

    current_stage = 'gamma'

    if params_['output_stage'] == current_stage:
        return current_image

    if params_['input_stage'] == current_stage:
        current_image = apply_tone_map(current_image)
        params_['input_stage'] = 'tone'

    current_stage = 'tone'

    if params_['output_stage'] == current_stage:
        return current_image

    # invalid input/output stage!
    raise ValueError(
        'Invalid input/output stage: input_stage = {}, output_stage = {}'.
        format(params_['input_stage'], params_['output_stage']))
Exemplo n.º 2
0
    'input_stage':
    'raw',  # options: 'raw', 'normal', 'white_balance', 'demosaic', 'xyz', 'srgb', 'gamma', 'tone'
    'output_stage':
    'tone',  # options: 'normal', 'white_balance', 'demosaic', 'xyz', 'srgb', 'gamma', 'tone'
    'save_as': 'png',  # options: 'jpg', 'png', 'tif', etc.
    'demosaic_type': 'EA',
    'save_dtype': np.uint8
}

# processing a directory
images_dir = '../data/'
image_paths = glob.glob(os.path.join(images_dir, '*.dng'))
for image_path in image_paths:

    # raw image data
    raw_image = get_visible_raw_image(image_path)

    # metadata
    metadata = get_metadata(image_path)

    # modify WB here
    metadata['as_shot_neutral'] = [1., 1., 1.]

    # render
    output_image = run_pipeline_v2(image_path, params)

    # save
    output_image_path = image_path.replace(
        '.dng', '_{}.'.format(params['output_stage']) + params['save_as'])
    max_val = 2**16 if params['save_dtype'] == np.uint16 else 255
    output_image = (output_image[..., ::-1] * max_val).astype(
def run_pipeline(image_path, params):
    # raw image data
    raw_image = get_visible_raw_image(image_path)

    # metadata
    metadata = get_metadata(image_path)

    # linearization
    linearization_table = metadata['linearization_table']
    if linearization_table is not None:
        print('Linearization table found. Not handled.')
        # TODO

    normalized_image = normalize(raw_image, metadata['black_level'],
                                 metadata['white_level'])

    if params['output_stage'] == 'normal':
        return normalized_image

    white_balanced_image = white_balance(normalized_image,
                                         metadata['as_shot_neutral'],
                                         metadata['cfa_pattern'])

    if params['output_stage'] == 'white_balance':
        return white_balanced_image

    demosaiced_image = demosaic(white_balanced_image,
                                metadata['cfa_pattern'],
                                output_channel_order='BGR',
                                alg_type=params['demosaic_type'])

    # fix image orientation, if needed
    demosaiced_image = fix_orientation(demosaiced_image,
                                       metadata['orientation'])

    if params['output_stage'] == 'demosaic':
        return demosaiced_image

    xyz_image = apply_color_space_transform(demosaiced_image,
                                            metadata['color_matrix_1'],
                                            metadata['color_matrix_2'])

    if params['output_stage'] == 'xyz':
        return xyz_image

    srgb_image = transform_xyz_to_srgb(xyz_image)

    if params['output_stage'] == 'srgb':
        return srgb_image

    gamma_corrected_image = apply_gamma(srgb_image)

    if params['output_stage'] == 'gamma':
        return gamma_corrected_image

    tone_mapped_image = apply_tone_map(gamma_corrected_image)
    if params['output_stage'] == 'tone':
        return tone_mapped_image

    output_image = None
    return output_image