Ejemplo n.º 1
0
 def adjust_exp(img, exp):
     configs = [
                 ('PixelClip', {}),
                 ('ExposureAdjustment', {'nstops': exp}),
                 ('PixelClip', {}),
               ]
     return ImageDegradationPipeline(configs)(img)
Ejemplo n.º 2
0
def prep_for_vis_arr(img_arr, exposure=None):
    if exposure is not None:
        configs = [
                    ('PixelClip', {}),
                    ('ExposureAdjustment', {'nstops': exposure}),
                    ('PixelClip', {}),
                  ]
        exp_adj = ImageDegradationPipeline(configs)
        img_arr = [exp_adj(im) for im in img_arr]
    img_arr = [torch2numpy(im, 1.0 / 2.2).astype('uint8') for im in img_arr]
    return img_arr
Ejemplo n.º 3
0
    def _create_pipeline(exp_adjustment,
                         poisson_k,
                         read_noise_sigma,
                         chromatic_aberration,
                         motion_blur_dir,
                         jpeg_quality,
                         denoise_sigma_s,
                         denoise_sigma_r,
                         denoise_color_sigma_ratio,
                         unsharp_amount,
                         denoise_color_only,
                         demosaick,
                         denoise,
                         jpeg_compression,
                         use_motion_blur,
                         use_chromatic_aberration,
                         use_unsharp_mask,
                         exposure_correction,
                         quantize,
                         quantize_bits=8,
                         denoise_guide_transform=None,
                         denoise_n_iter=1,
                         demosaick_use_median=False,
                         demosaick_n_iter=0,
                         use_median_denoise=False,
                         median_before_bilateral=False,
                         denoise_median=None,
                         denoise_median_ratio=1.0,
                         denoise_median_n_iter=1,
                         demosaicked_input=True,
                         log_blackpts=0.004,
                         bilateral_class="DenoisingSKImageBilateralNonDifferentiable",
                         demosaick_class="AHDDemosaickingNonDifferentiable",
                         demosaick_ahd_delta=2.0,
                         demosaick_ahd_sobel_sz=3,
                         demosaick_ahd_avg_sz=3,
                         use_wavelet=False,
                         wavelet_family="db2",
                         wavelet_sigma=None,
                         wavelet_th_method="BayesShrink",
                         wavelet_levels=None,
                         motion_blur=None,
                         motionblur_th=None,
                         motionblur_boost=None,
                         motionblur_segment=1,
                         debug=False,
                         bayer_crop_phase=None,
                         saturation=None,
                         use_autolevel=False,
                         autolevel_max=1.5,
                         autolevel_blk=1,
                         autolevel_wht=99,
                         denoise_color_range_ratio=1,
                         wavelet_last=False,
                         wavelet_threshold=None,
                         wavelet_filter_chrom=True,
                         post_tonemap_class=None,
                         post_tonemap_amount=None,
                         pre_tonemap_class=None,
                         pre_tonemap_amount=None,
                         post_tonemap_class2=None,
                         post_tonemap_amount2=None,
                         repair_hotdead_pixel=False,
                         hot_px_th=0.2,
                         white_balance=False,
                         white_balance_temp=6504,
                         white_balance_tint=0,
                         use_tone_curve3zones=False,
                         tone_curve_highlight=0.0,
                         tone_curve_midtone=0.0,
                         tone_curve_shadow=0.0,
                         tone_curve_midshadow=None,
                         tone_curve_midhighlight=None,
                         unsharp_radius=4.0,
                         unsharp_threshold=3.0,
                         **kwargs):

        # Define image degradation pipeline
        # add motion blur and chromatic aberration
        configs_degrade = []
        # Random threshold
        if demosaicked_input:
            # These are features that only make sense to simulate in 
            # demosaicked input.
            if use_motion_blur:
                configs_degrade += [
                    ('MotionBlur', {'amt': motion_blur,
                                    'direction': motion_blur_dir,
                                    'kernel_sz': None,
                                    'dynrange_th': motionblur_th,
                                    'dynrange_boost': motionblur_boost,
                                    }
                     )
                ]
            if use_chromatic_aberration:
                configs_degrade += [
                    ('ChromaticAberration', {'scaling': chromatic_aberration}),
                ]

        configs_degrade.append(('ExposureAdjustment', {'nstops': exp_adjustment}))
        if demosaicked_input:
            if demosaick:
                configs_degrade += [
                    ('BayerMosaicking', {}),
                ]
                mosaick_pattern = 'bayer'
            else:
                mosaick_pattern = None
        else:
            mosaick_pattern = 'bayer'

        # Add artificial noise.
        configs_degrade += [
            ('PoissonNoise', {'sigma': poisson_k, 'mosaick_pattern': mosaick_pattern}),
            ('GaussianNoise', {'sigma': read_noise_sigma, 'mosaick_pattern': mosaick_pattern}),
        ]

        if quantize:
            configs_degrade += [
                ('PixelClip', {}),
                ('Quantize', {'nbits': quantize_bits}),
            ]
        if repair_hotdead_pixel:
            configs_degrade += [
                ("RepairHotDeadPixel", {"threshold": hot_px_th}),
            ]

        if demosaick:
            configs_degrade += [
                (demosaick_class, {'use_median_filter': demosaick_use_median,
                                   'n_iter': demosaick_n_iter,
                                   'delta': demosaick_ahd_delta,
                                   'sobel_sz': demosaick_ahd_sobel_sz,
                                   'avg_sz': demosaick_ahd_avg_sz,
                                   }),
                ('PixelClip', {}),
            ]
        if white_balance:
            configs_degrade += [
                ('WhiteBalanceTemperature', {"new_temp": white_balance_temp,
                                             "new_tint": white_balance_tint,
                                             }),
            ]
        if pre_tonemap_class is not None:
            kw = "gamma" if "Gamma" in pre_tonemap_class else "amount"
            configs_degrade += [
                (pre_tonemap_class, {kw: pre_tonemap_amount})
            ]
        if use_autolevel:
            configs_degrade.append(('AutoLevelNonDifferentiable', {'max_mult': autolevel_max,
                                                                   'blkpt': autolevel_blk,
                                                                   'whtpt': autolevel_wht,
                                                                   }))
        denoise_list = []
        if denoise:
            denoise_list.append([
                ('PixelClip', {}),
                (bilateral_class, {'sigma_s': denoise_sigma_s,
                                   'sigma_r': denoise_sigma_r,
                                   'color_sigma_ratio': denoise_color_sigma_ratio,
                                   'color_range_ratio': denoise_color_range_ratio,
                                   'filter_lum': not denoise_color_only,
                                   'n_iter': denoise_n_iter,
                                   'guide_transform': denoise_guide_transform,
                                   '_bp': log_blackpts,
                                   }),
                ('PixelClip', {}),
            ])

        if use_median_denoise:
            # TODO: Fix this.
            # Special value because our config can't specify list of list
            if denoise_median == -1:
                denoise_median = [[0, 1, 0], [1, 1, 1], [0, 1, 0]]
            if debug:
                print("Denoising with Median Filter")
            denoise_list.append([
                ('DenoisingMedianNonDifferentiable', {'neighbor_sz': denoise_median,
                                                      'color_sigma_ratio': denoise_median_ratio,
                                                      'n_iter': denoise_median_n_iter,
                                                      }),
            ])

        if median_before_bilateral:
            denoise_list = denoise_list[::-1]
        if use_wavelet:
            # always do wavelet first.
            wavelet_config = [
                ('PixelClip', {}),
                ("DenoisingWaveletNonDifferentiable", {'sigma_s': wavelet_th_method,
                                                       'sigma_r': wavelet_sigma,
                                                       'color_sigma_ratio': wavelet_family,
                                                       'filter_lum': True,
                                                       'n_iter': wavelet_levels,
                                                       'guide_transform': denoise_guide_transform,
                                                       '_bp': wavelet_threshold,
                                                       'filter_chrom': wavelet_filter_chrom,
                                                       }),
                ('PixelClip', {}),
            ]
            if wavelet_last:
                denoise_list.append(wavelet_config)
            else:
                denoise_list.insert(0, wavelet_config)

        for i in range(len(denoise_list)):
            configs_degrade += denoise_list[i]
        if post_tonemap_class is not None:
            kw = "gamma" if "Gamma" in post_tonemap_class else "amount"
            configs_degrade += [
                (post_tonemap_class, {kw: post_tonemap_amount})
            ]
        if post_tonemap_class2 is not None:
            kw = "gamma" if "Gamma" in post_tonemap_class2 else "amount"
            configs_degrade += [
                (post_tonemap_class2, {kw: post_tonemap_amount2})
            ]
        if use_tone_curve3zones:
            ctrl_val = [t for t in [tone_curve_shadow,
                                    tone_curve_midshadow,
                                    tone_curve_midtone,
                                    tone_curve_midhighlight,
                                    tone_curve_highlight] if t is not None]
            configs_degrade += [
                ('ToneCurveNZones', {'ctrl_val': ctrl_val,
                                     }),
                ('PixelClip', {}),
            ]

        if use_unsharp_mask:
            configs_degrade += [
                ('Unsharpen', {'amount': unsharp_amount,
                               'radius': unsharp_radius,
                               'threshold': unsharp_threshold}),
                ('PixelClip', {}),
            ]

        if saturation is not None:
            configs_degrade.append(('Saturation', {'value': saturation}))

        # things that happens after camera apply denoising, etc. 
        if jpeg_compression:
            configs_degrade += [
                ('sRGBGamma', {}),
                ('Quantize', {'nbits': 8}),
                ('PixelClip', {}),
                ('JPEGCompression', {"quality": jpeg_quality}),
                ('PixelClip', {}),
                ('UndosRGBGamma', {}),
                ('PixelClip', {}),
            ]
        else:
            if quantize:
                configs_degrade += [
                    ('Quantize', {'nbits': 8}),
                    ('PixelClip', {}),
                ]

        if exposure_correction:
            # Finally do exposure correction of weird jpeg-compressed image to get crappy images.
            configs_degrade.append(('ExposureAdjustment', {'nstops': -exp_adjustment}))
            target_pipeline = None
        else:
            configs_target = [
                ('ExposureAdjustment', {'nstops': exp_adjustment}),
                ('PixelClip', {}),
            ]
            target_pipeline = ImageDegradationPipeline(configs_target)

        configs_degrade.append(('PixelClip', {}))
        if debug:
            print('Final config:')
            print('\n'.join([str(c) for c in configs_degrade]))

        degrade_pipeline = ImageDegradationPipeline(configs_degrade)
        return degrade_pipeline, target_pipeline
            'nstops': -exp_adjustment
        }),
        ('PixelClip', {}),
        ('NaiveDemosaicking', {}),
        ('PixelClip', {}),
    ]
    configs_denoise = [
        ('DenoisingBilateral', {
            'sigma_s': 1.0,
            'sigma_r': 0.1
        }),
        ('PixelClip', {}),
        ('sRGBGamma', {}),
    ]

    pipeline_prepreprocess = ImageDegradationPipeline(configs_prepreprocess)
    pipeline_preprocess = ImageDegradationPipeline(configs_preprocess)
    pipeline_degrade = ImageDegradationPipeline(configs_degrade)
    pipeline_denoise = ImageDegradationPipeline(configs_denoise)

    demosaicked = pipeline_prepreprocess(raw_im)
    preprocessed = pipeline_preprocess(demosaicked)
    degraded = pipeline_degrade(preprocessed)
    denoised = pipeline_denoise(degraded)

    denoised_numpy = tensor2numpy(denoised)
    preprocessed_numpy = tensor2numpy(preprocessed)
    stacked = np.concatenate((denoised_numpy, preprocessed_numpy), axis=-1)
    patches = extract_patches_2d(stacked, (args.patch_sz, args.patch_sz),
                                 args.patch_per_image)
    degraded_patches, target_patches = np.split(patches, 2, axis=-1)