def main():
    """
	workflow idea:
		clean sensor real good
		take really fine grained 'mapping' of weird bands once. shitton of images, long processing

		each night:
			take 1 set of flats close to light frames
				determine mean flat -> fine grained flats map
					- get an image of dust spots, focus differences, etc

				for each image:	
					get fine grained flat map frame
						multiply fine grained flat * nightly flat map = real flat
						calibrate light frame using flat



	"""

    bias_folder = 'K:/orion_135mm_bothnights/bias'

    if 1:
        flats_folder = 'F:/2020/2020-04-06/blue_sky_flats'
    elif 0:
        flats_folder = 'F:/2020/2020-04-07/flats_600mm'
    else:
        flats_folder = 'F:/Pictures/Lightroom/2020/2020-03-01/135mm_computer_screen_flats'

    master_bias = load_dark(bias_folder)

    flat_images = load_flats_from_subfolders(flats_folder, master_bias)
    if 1:
        plot_flats_banding_progression(flats_folder, master_bias)
        exit(0)
Exemplo n.º 2
0
def test_get_flat():
    # folder = 'F:/2020/2020-04-09/shirt_dusk_flats_progression'
    folder = 'F:/2020/2020-04-11/135mm_sky_flats_progression'
    bias_folder = 'K:/orion_135mm_bothnights/bias'
    bias_img = load_dark(bias_folder)
    downsize_factor = 4

    test_img_path = 'F:/Pictures/Lightroom/2020/2020-02-18/orion_135mm/DSC03637.ARW'

    test_img = histogram_gap.load_raw_image(test_img_path, bias_img)

    test_img = np.transpose(test_img, (2, 0, 1))

    flat = get_subimg_matched_flat(None, test_img, folder, bias_img)

    for channel_index in range(4):
        print('testing channel ', channel_index)
        channel_flat = flat[channel_index]
        test_img_channel = test_img[channel_index]

        plt.imshow(channel_flat)
        plt.title('channel flat')
        plt.show()

        calibrated_img = test_img_channel / channel_flat

        plt.imshow(test_img_channel)
        plt.title('test img')
        plt.show()

        plt.imshow(calibrated_img)
        plt.title('calibrated test img')
        plt.show()

        # plt.hist(calibrated_img.flatten(), bins = 1000)
        # plt.show()

        benchmark_flat = full_flat_sequence.get_flat_matching_brightness_histogram_match(
            folder,
            test_img_channel,
            channel_index,
            bias_img=bias_img,
            half_images_to_average=5)

        plt.imshow(benchmark_flat)
        plt.title('benchmark')
        plt.show()

        benchmark_ratio = channel_flat / benchmark_flat
        plt.title('difference v benchmark')
        plt.imshow(benchmark_ratio)
        plt.show()
Exemplo n.º 3
0
def test_get_flat_faster():
    folder = 'F:/2020/2020-04-11/135mm_sky_flats_progression'
    bias_folder = 'K:/orion_135mm_bothnights/bias'
    bias_img = load_dark(bias_folder)
    downsize_factor = 4

    test_img_path = 'F:/Pictures/Lightroom/2020/2020-02-18/orion_135mm/DSC03637.ARW'

    test_img = histogram_gap.load_raw_image(test_img_path, bias_img)

    test_img = np.transpose(test_img, (2, 0, 1))

    # resize = 4
    # test_img = test_img[0:1, :test_img.shape[1]//(resize*downsize_factor) * downsize_factor, :test_img.shape[2]//(resize*downsize_factor) * downsize_factor]
    # test_img = test_img[0:1]
    print('test_img: ', test_img.shape)

    # for channel_index in range(test_img.shape[0]):
    # 	channel_progression, channel_means = load_channel_progression_brightness_filtered(folder, bias_img, downsize_factor, channel_index, num_images_to_avg=5)

    load_all_channels(folder, bias_img, downsize_factor)

    start = datetime.datetime.now()
    flat_new = get_subimg_matched_flat2(None, test_img, folder, bias_img)
    print('new time: ', (datetime.datetime.now() - start))

    if 1:
        start = datetime.datetime.now()
        flat_reference = get_subimg_matched_flat(None, test_img, folder,
                                                 bias_img)
        print('reference time: ', (datetime.datetime.now() - start))

        if np.all(flat_new == flat_reference):
            print('ALL GOOD!')

        else:
            print('***not equal***')
            avg_diff = np.mean(
                np.abs(flat_new - flat_reference) / flat_reference)
            print('average relative difference: ', avg_diff)
            ratio = (flat_new / flat_reference)[0]
            plt.imshow(ratio)
            plt.show()
Exemplo n.º 4
0
def test():

    flats_progression_folder = 'F:/2020/2020-04-11/135mm_sky_flats_progression'

    # test_folder = 'F:/Pictures/Lightroom/2020/2020-02-18/blue_sky_flats/4'
    test_folder = 'F:/2020/2020-04-11/flats_135mm_tshirtwall'
    bias_folder = 'K:/orion_135mm_bothnights/bias'

    bias_img = load_dark(bias_folder)

    for img_fn in os.listdir(test_folder):
        img = histogram_gap.load_raw_image(os.path.join(test_folder, img_fn),
                                           master_dark=bias_img)
        img = np.transpose(img, (2, 0, 1))

        flat_img = subimg_full_flat_sequence.get_subimg_matched_flat2(
            None, img, flats_progression_folder, bias_img, downsize_factor=4)

        calibrated_test_img = img / flat_img
        # calibrated_test_img = img

        for channel in range(4):
            calibrated_channel = calibrated_test_img[channel]

            calibrated_channel = remove_gradient(calibrated_channel,
                                                 quadratic=True)
            calibrated_channel /= np.mean(calibrated_channel)

            overall_shape = gaussian_filter(calibrated_channel, sigma=50)

            calibrated_channel -= overall_shape

            std_dev = np.std(calibrated_channel)
            print(std_dev)
            plt.subplot(2, 1, 1)
            plt.imshow(np.clip(calibrated_channel, -std_dev, std_dev))
            plt.title(str(std_dev))

            plt.subplot(2, 1, 2)
            plt.imshow(overall_shape)

            plt.show()
def test():
    bias_folder = 'K:/orion_135mm_bothnights/bias'
    master_bias = load_dark(bias_folder)

    flats_progression_folder = 'F:/2020/2020-04-09/shirt_dusk_flats_progression'

    test_img_path = 'F:/Pictures/Lightroom/2020/2020-02-29/orion_600mm/DSC05669.ARW'
    show_relative_flats(test_img_path, master_bias)

    test_img = histogram_gap.load_raw_image(test_img_path, None)

    for channel in range(test_img.shape[-1]):
        matching_flat = get_flat_matching_brightness(flats_progression_folder,
                                                     test_img[:, :, channel],
                                                     channel,
                                                     half_images_to_average=3)

        plt.subplot(2, 2, 1)
        display_image(test_img[:, :, channel])
        plt.title('test img')

        plt.subplot(2, 2, 2)
        display_image(matching_flat)
        plt.title('matching flat')

        ratios = (test_img[:, :, channel] / matching_flat).flatten()

        plt.subplot(2, 2, 3)
        bin_range = 0.1
        bins = np.linspace(1 - bin_range, 1 + bin_range, 1000)
        plt.hist(ratios, bins=bins)
        plt.yscale('log', nonposy='clip')
        plt.grid(True)

        plt.show()

    print(np.mean(test_img, axis=(0, 1)))
def test2():
    bias_folder = 'K:/orion_135mm_bothnights/bias'
    master_bias = load_dark(bias_folder)

    # flats_progression_folder = 'F:/2020/2020-04-09/shirt_dusk_flats_progression'
    flats_progression_folder = 'F:/2020/2020-04-11/135mm_sky_flats_progression'

    test_folder = 'F:/2020/2020-04-10/casseiopeia_pano_135mm/3'
    n_test_images = 3

    test_filenames = list(
        map(lambda s2: os.path.join(test_folder, s2),
            filter(lambda s: s.endswith('.ARW'), os.listdir(test_folder))))

    for i in range(0, len(test_filenames), n_test_images):
        fns = test_filenames[i:i + n_test_images]

        test_imgs = [
            histogram_gap.load_raw_image(fn, master_bias) for fn in fns
        ]

        mean_test_img = np.mean(test_imgs, axis=0)

        for channel in range(mean_test_img.shape[-1]):
            test_img_channel = mean_test_img[:, :, channel]

            # matching_flat = get_flat_matching_brightness(flats_progression_folder, test_img_channel, channel, half_images_to_average=7)
            matching_flat = get_flat_matching_brightness_histogram_match(
                flats_progression_folder,
                test_img_channel,
                channel,
                half_images_to_average=7)

            corrected_test_img = test_img_channel / matching_flat

            ratio_range = 0.05
            ratios = corrected_test_img[np.where(
                np.abs(corrected_test_img - 1) < 0.05)].flatten()

            def display_image(img, z=1):
                disp_image = img.copy()
                disp_image = remove_gradient(disp_image)
                disp_image = gaussian_filter(disp_image,
                                             mode='nearest',
                                             sigma=5)

                low = np.percentile(disp_image, z)
                high = np.percentile(disp_image, 100 - z)
                disp_image = np.clip(disp_image, low, high)
                plt.imshow(disp_image)

            plt.subplot(2, 2, 1)
            display_image(test_img_channel)
            plt.title('raw test img')

            plt.subplot(2, 2, 2)
            display_image(matching_flat)
            plt.title('matching flat')

            plt.subplot(2, 2, 3)
            display_image(corrected_test_img)
            plt.title('corrected test img')

            plt.subplot(2, 2, 4)
            plt.hist(ratios, bins=1001)
            plt.grid(True)
            plt.title('flat : bright ratio img')

            plt.show()
def main():
    """
	workflow:
calc bias frame	 - sigma mean
	remove histogram gaps
	folder input
	cache

calc dark frame - sigma mean
	remove histogram gaps
	folder input
	cache

calc series of flat frames - sigma mean
	remove histogram gaps
	subtract bias
	folder input
	cache

for each light frame:
	remove histogram gaps
	subtract dark frame
	for each channel:
		pick optimal weighting of flat frames
		divide by flat

	save image as tif

 

-> pixinsight
	debayer
	register
	integrate
	"""
    if 1:
        lights_in_folder = 'K:/orion_135mm_bothnights/lights_in'

        darks_folder = 'K:/orion_135mm_bothnights/darks'
        bias_folder = 'K:/orion_135mm_bothnights/bias'

        output_folder = 'K:/orion_135mm_bothnights2'
        # flats_folder = 'F:/2020/2020-04-06/blue_sky_flats'
        flats_folder = 'F:/Pictures/Lightroom/2020/2020-02-18/blue_sky_flats'

        flats_progression_folder = 'F:/2020/2020-04-09/shirt_dusk_flats_progression'
    elif 0:
        lights_in_folder = 'F:/Pictures/Lightroom/2020/2020-02-19/flame_horsehead_600mm'

        darks_folder = 'F:/Pictures/Lightroom/2020/2020-02-19/darks'
        bias_folder = 'F:/Pictures/Lightroom/2020/2020-02-19/bias'

        flats_folder = 'F:/Pictures/Lightroom/2020/2020-02-20/flats'
        # flats_folder = 'F:/2020/2020-04-07/flats_600mm'
        flats_progression_folder = 'F:/2020/2020-04-09/shirt_dusk_flats_progression'

        calibrated_lights_out_folder = 'K:/flame_horsehead_600mm/lights_out_flat_sequence'
    elif 0:
        lights_in_folder = 'F:/Pictures/Lightroom/2020/2020-02-29/orion_600mm'

        darks_folder = 'F:/Pictures/Lightroom/2020/2020-02-19/darks'
        bias_folder = 'F:/2020/2020-04-17/bias_iso800'
        flats_progression_folder = 'F:/2020/2020-04-09/shirt_dusk_flats_progression'

        flats_folder = 'F:/Pictures/Lightroom/2020/2020-02-29/flats'
        # flats_folder = 'F:/2020/2020-04-07/flats_600mm'

        calibrated_lights_out_folder = 'K:/orion_600mm/lights_out'
    elif 0:
        bias_folder = 'F:/Pictures/Lightroom/2020/2020-02-19/bias'
        darks_folder = 'F:/Pictures/Lightroom/2020/2020-02-19/darks'
        # flats_progression_folder = 'F:/2020/2020-04-09/shirt_dusk_flats_progression'
        flats_progression_folder = 'F:/2020/2020-04-11/135mm_sky_flats_progression'

        # flats_folder = 'F:/2020/2020-04-06/blue_sky_flats'
        flats_folder = 'F:/2020/2020-04-11/flats_135mm_tshirtwall'

        # lights_in_folder = 'F:/2020/2020-04-10/casseiopeia_pano_135mm/1'
        # calibrated_lights_out_folder = 'K:/casseiopeia_pano/lights_1'

        # lights_in_folder = 'F:/2020/2020-04-10/casseiopeia_pano_135mm/2'
        # calibrated_lights_out_folder = 'K:/casseiopeia_pano/lights_2'

        lights_in_folder = 'F:/2020/2020-04-10/casseiopeia_pano_135mm/1'
        # calibrated_lights_out_folder = 'K:/casseiopeia_pano/lights_3'
        calibrated_lights_out_folder = 'K:/casseiopeia_pano/lights_1_blurredflats'
    elif 0:
        bias_folder = 'F:/Pictures/Lightroom/2020/2020-02-19/bias'
        darks_folder = 'F:/Pictures/Lightroom/2020/2020-02-19/darks'

        flats_folder = 'F:/2020/2020-04-16/rho_ophiuchi/flats'
        lights_in_folder = 'F:/2020/2020-04-16/rho_ophiuchi/lights'
        flats_progression_folder = 'F:/2020/2020-04-11/135mm_sky_flats_progression'

        calibrated_lights_out_folder = 'K:/rho_oph/lights'
    elif 0:
        bias_folder = 'F:/2020/2020-04-17/bias_iso800'
        darks_folder = 'F:/2020/2020-04-17/darks_iso800_2mins'

        flats_folder = 'F:/2020/2020-04-17/flats_135mmf4_v2'
        lights_in_folder = 'F:/2020/2020-04-17/coma_cluster_135mmf4'
        flats_progression_folder = 'F:/2020/2020-04-11/135mm_sky_flats_progression'

        calibrated_lights_out_folder = 'K:/coma_135mm_f4/lights'
    elif 0:

        lights_in_folder = 'F:/2020/2020-04-15/leo_triplet_600mm'

        darks_folder = 'F:/Pictures/Lightroom/2020/2020-02-19/darks'
        bias_folder = 'F:/2020/2020-04-17/bias_iso800'
        flats_progression_folder = 'F:/2020/2020-04-09/shirt_dusk_flats_progression'

        flats_folder = 'F:/2020/2020-04-15/flats'

        calibrated_lights_out_folder = 'K:/leo_triplet_600mm/lights_out'
    elif 0:

        lights_in_folder = 'F:/Pictures/Lightroom/2020_2018/2018-02-01/andromeda_600mm'

        darks_folder = 'F:/Pictures/Lightroom/2020/2020-02-19/darks'
        bias_folder = 'F:/2020/2020-04-17/bias_iso800'
        flats_progression_folder = 'F:/2020/2020-04-09/shirt_dusk_flats_progression'

        flats_folder = 'F:/Pictures/Lightroom/2020_2018/2018-02-01/flats_600mm'

        calibrated_lights_out_folder = 'K:/andromeda_600mm/lights_out'
    elif 0:
        darks_folder = 'F:/Pictures/Lightroom/2020/2020-03-07/darks_135mm_30s_iso100'
        bias_folder = 'F:/Pictures/Lightroom/2020/2020-03-07/bias_135mm_iso100'
        flats_progression_folder = 'F:/2020/2020-04-11/135mm_sky_flats_progression'
        flats_folder = 'F:/2020/2020-04-12/flats_135mm_clothsky'

        lights_in_folder = 'F:/2020/2020-04-12/coma_cluster_135mm'
        calibrated_lights_out_folder = 'K:/coma_cluster_135mm/lights_out'
    elif 0:
        lights_in_folder = 'F:/Pictures/Lightroom/2020/2020-02-20/pleiades'

        darks_folder = 'F:/Pictures/Lightroom/2020/2020-02-19/darks'
        bias_folder = 'F:/2020/2020-04-17/bias_iso800'
        flats_progression_folder = 'F:/2020/2020-04-09/shirt_dusk_flats_progression'

        flats_folder = 'F:/Pictures/Lightroom/2020/2020-02-20/pleiades_flats'

        calibrated_lights_out_folder = 'Z:/astro_processing/pleiades/lights_pleiades_flats'
    elif 1:
        darks_folder = 'K:/orion_135mm_bothnights/darks'
        bias_folder = 'K:/orion_135mm_bothnights/bias'
        flats_progression_folder = 'F:/2020/2020-04-11/135mm_sky_flats_progression'
        flats_folder = 'F:/2020/2020-04-22/24mm_flats'

        # lights_in_folder = 'F:/2020/2020-04-11/rice_lake_nightscape/tracked'
        # calibrated_lights_out_folder = 'K:/rice_lake/tracked_lights'

        lights_in_folder = 'F:/2020/2020-04-11/rice_lake_nightscape/untracked'
        calibrated_lights_out_folder = 'K:/rice_lake/untracked_lights'

    if not os.path.exists(output_folder): os.mkdir(output_folder)
    calibrated_lights_out_folder = os.path.join(output_folder, 'calibrated')
    if not os.path.exists(calibrated_lights_out_folder):
        os.mkdir(calibrated_lights_out_folder)

    #Todo: make smarter mean. also cache
    master_dark = load_dark(darks_folder)
    master_bias = load_dark(bias_folder)

    flat_images, flat_subfolders = load_flats_from_subfolders(
        flats_folder, master_bias)

    flat_images_rgb = np.transpose(flat_images, (0, 3, 1, 2))
    print(flat_images.shape, flat_images_rgb.shape)

    filenames = list(
        filter(lambda s: s.endswith('.ARW'), os.listdir(lights_in_folder)))[:7]
    print('number of lights: ', len(filenames))

    for filename in tqdm.tqdm(filenames):
        full_path = os.path.join(lights_in_folder, filename)
        output_path = os.path.join(calibrated_lights_out_folder,
                                   filename.rstrip('.ARW') + '.tif')
        # if os.path.exists(output_path): continue

        img_minus_dark = histogram_gap.load_raw_image(full_path, master_dark)
        img_minus_dark_rgb = np.transpose(img_minus_dark, (2, 0, 1))

        if 0:
            calibrated_flat_img_rgb, exposure_index = get_exposure_matched_flat(
                flat_images_rgb, img_minus_dark_rgb)
        elif 0:
            calibrated_flat_img_rgb = full_flat_sequence.get_flat_and_bandingfix_flat(
                flat_images_rgb, img_minus_dark_rgb, flats_progression_folder,
                master_bias)
        elif 0:
            calibrated_flat_img_rgb = subimg_full_flat_sequence.get_subimg_matched_flat2(
                flat_images_rgb, img_minus_dark_rgb, flats_progression_folder,
                master_bias)
        elif 1:
            calibrated_flat_img_rgb = subimg_full_flat_sequence.get_flat_and_subimg_matched_flat(
                flat_images_rgb, img_minus_dark_rgb, flats_progression_folder,
                master_bias)
        else:
            calibrated_flat_img_rgb = 1

        if calibrated_flat_img_rgb is None:
            print("***Failed to find matching flat")
            continue

        calibrated_img_rgb = img_minus_dark_rgb / calibrated_flat_img_rgb
        calibrated_img_rgb = np.clip(calibrated_img_rgb, 0, 1)
        # calibrated_img_rgb = img_minus_dark_rgb

        # print('calibrated rgb: ', calibrated_img_rgb.shape)
        calibrated_img = flatten_channel_image(calibrated_img_rgb)

        tiff.imwrite(output_path, calibrated_img.astype('float32'))

        # exit(0)

    pi_script_path = pixinsight_preprocess.create_pixinsight_preprocess_script(
        output_folder, calibrated_lights_out_folder, filenames)
    cmd = '"C:/Program Files/PixInsight/bin/PixInsight.exe" --run=%s' % pi_script_path

    os.system(cmd)
Exemplo n.º 8
0
def test1():
    # folder = 'F:/2020/2020-04-09/shirt_dusk_flats_progression'
    folder = 'F:/2020/2020-04-11/135mm_sky_flats_progression'
    bias_folder = 'K:/orion_135mm_bothnights/bias'
    bias_img = load_dark(bias_folder)
    downsize_factor = 4
    channel_index = 0

    channel = load_channel_progression(folder, bias_img, downsize_factor,
                                       channel_index)

    image_means = np.mean(channel, axis=(1, 2))
    # print(image_means.shape)

    x = 1280
    y = 1000
    xs = []
    ys = []
    ys2 = []

    if 0:
        plt.imshow(channel[500, :, :])
        plt.show()

    if 0:
        plt.subplot(2, 1, 1)
        plt.plot(image_means)
        plt.grid(True)

        means_relative_diffs = image_means[:-1] / image_means[1:]

        plt.subplot(2, 1, 2)
        plt.plot(means_relative_diffs)
        plt.grid(True)
        plt.show()

    patch_size = 40

    xs = np.arange(0, channel.shape[0])

    ys1 = channel[:, (1000 - patch_size) //
                  downsize_factor:(1000 + patch_size) // downsize_factor,
                  (1280 - patch_size) // downsize_factor:(1280 + patch_size) //
                  downsize_factor]
    ys2 = channel[:, (100 - patch_size) //
                  downsize_factor:(100 + patch_size) // downsize_factor,
                  (100 - patch_size) // downsize_factor:(100 + patch_size) //
                  downsize_factor]
    print(ys1.shape)

    ys1 = np.mean(ys1, axis=(1, 2))
    ys2 = np.mean(ys2, axis=(1, 2))

    ys1 /= image_means
    ys2 /= image_means

    # #TODO: better mean?
    # channel_mean = np.mean(channel_downsized)

    # img_ratio = channel_downsized / channel_mean

    # xs.append(i)
    # ys.append(img_ratio[y,x])
    # ys2.append(img_ratio[50,50])

    # ys = np.array(ys)
    # ys2 = np.array(ys2)

    for ys, label in [(ys1, '1280,1000'), (ys2, '100, 100')]:
        ys_normalized = ys / np.mean(ys)
        # plt.plot(xs, ys_normalized, '--')
        n_filt = 5
        filt = np.ones(n_filt) / n_filt
        ys_filtered = scipy.signal.filtfilt(filt, [1], ys_normalized)

        plt.plot(xs, ys_filtered, label=label)

    # plt.plot(xs, ys1 / np.mean(ys1))
    # plt.plot(xs, ys2 / np.mean(ys2))
    plt.legend()
    plt.grid(True)
    plt.show()
Exemplo n.º 9
0
def test_flatten_channel():
    # folder = 'F:/2020/2020-04-09/shirt_dusk_flats_progression'
    folder = 'F:/2020/2020-04-11/135mm_sky_flats_progression'
    bias_folder = 'K:/orion_135mm_bothnights/bias'
    bias_img = load_dark(bias_folder)
    downsize_factor = 4
    channel_index = 0

    test_img_path = 'F:/Pictures/Lightroom/2020/2020-02-18/orion_135mm/DSC03637.ARW'

    test_img = histogram_gap.load_raw_image(test_img_path, bias_img)
    test_img_channel = test_img[:, :, channel_index]

    #todo: spatial and temporal filtering
    # channel_progression = load_channel_progression(folder, bias_img, downsize_factor, channel_index)
    # channel_progression, image_means = load_channel_progression_brightness_filtered(folder, bias_img, downsize_factor, channel_index, num_images_to_avg=5)
    channel_progression, image_means = load_channel_progression_brightness_filtered_spatialfiltered(
        folder, bias_img, downsize_factor, channel_index, num_images_to_avg=5)

    channel_flat = np.zeros_like(test_img_channel)

    print(test_img_channel.shape, channel_progression.shape,
          channel_flat.shape)

    for yi in tqdm.tqdm(range(test_img_channel.shape[0])):
        if 0:
            for xi in range(test_img_channel.shape[1]):
                input_pixel = test_img_channel[yi, xi]

                progression = channel_progression[:, yi // downsize_factor,
                                                  xi // downsize_factor]

                index = np.argmin(np.abs(progression - input_pixel))

                if 0:
                    print(progression.shape, index)
                    plt.plot(np.arange(0, len(progression)), progression)
                    plt.scatter([index], [input_pixel], 'r')
                    plt.grid(True)
                    plt.show()

                normalized_progression = progression / image_means
                output = normalized_progression[index]  #* image_means[index]

                # output = image_means[index] /
                # output = progression[index] / (np.mean(progression) * image_means[index])
                # print(progression[index], np.mean(progression), image_means[index])
                # print(output)

                channel_flat[yi, xi] = output
        else:
            # input_pixels = test_img_channel[yi]

            input_pixels = test_img_channel[yi]
            input_pixels = input_pixels.reshape(
                (input_pixels.shape[0] // downsize_factor,
                 downsize_factor)).mean(1)

            progressions = channel_progression[:, yi // downsize_factor, :]
            indices = np.argmin(np.abs(progressions - input_pixels), axis=0)
            # print('indices: ', indices.shape)
            # print('progressions: ', progressions.shape)
            # normalized_progressions = progressions / image_means
            # normalized_progressions = progressions.copy()
            # for i in range(normalized_progressions.shape[0]):
            # 	normalized_progressions[i] /= image_means[i]

            # outputs = normalized_progressions[:, indices]
            # print(outputs.shape, normalized_progressions.shape)

            outputs = np.zeros((len(indices)), dtype=channel_flat.dtype)
            for i in range(indices.shape[0]):
                # normalized_progression = progressions[:, i] / image_means
                # output = normalized_progression[indices[i]]

                output = progressions[indices[i], i] / image_means[indices[i]]
                outputs[i] = output

            channel_flat[yi] = np.repeat(outputs, downsize_factor)

            # print(channel_flat[yi, :10], outputs[:10])
            # exit(0)

    plt.imshow(channel_flat)
    plt.title('channel flat')
    plt.show()

    calibrated_img = test_img_channel / channel_flat

    plt.imshow(test_img_channel)
    plt.title('test img')
    plt.show()

    plt.imshow(calibrated_img)
    plt.title('calibrated test img')
    plt.show()

    # plt.hist(calibrated_img.flatten(), bins = 1000)
    # plt.show()

    benchmark_flat = full_flat_sequence.get_flat_matching_brightness_histogram_match(
        folder,
        test_img_channel,
        channel_index,
        bias_img=bias_img,
        half_images_to_average=5)

    plt.imshow(benchmark_flat)
    plt.title('benchmark')
    plt.show()

    benchmark_ratio = channel_flat / benchmark_flat
    plt.title('difference v benchmark')
    plt.imshow(benchmark_ratio)
    plt.show()