Exemplo n.º 1
0
def process(img1, img2, outfile):
    data1 = rasterio.open(img1)
    lons1, lats1 = utils.get_bounds(data1)

    data2 = rasterio.open(img2)
    lons2, lats2 = utils.get_bounds(data2)

    lons, lats = utils.get_overlap(lons1, lats1, lons2, lats2)
    if not lons or not lats:
        raise Exception(f'ERR no overlap: {lons1} {lats1} {lons2} {lats2}')

    crop1 = utils.crop_data(data1, lons, lats)
    crop2 = utils.crop_data(data2, lons, lats)

    new1, new2 = utils.scale_img(crop1, crop2)
    res = abs(int(new1.shape[0] / crop1.shape[0]) * data1.res[0])

    s = get_ssim(new1, new2)
    print(f'{img1}, {img2}, ssim, {s:.06f}')

    norm1, norm2 = get_norm(new1), get_norm(new2)
    p = get_psnr(norm1, norm2)
    print(f'{img1}, {img2}, pnsr, {p:.06f}')

    transform = Affine.translation(lons[0], lats[0])
    transform *= Affine.scale(res)

    utils.write_tiff(outfile, norm1 * norm2, transform, nodata=data1.nodata)
def plot_feature_distribution(
        sample_features,
        ax=None,
        t_sne=True,
        hue=None,
        hue_order=None,
        style=None,
        style_order=None,
        x_lim='min_max',
        y_lim='min_max',
        contour=False,
        z_generator=None):
    if ax is None:
        raise AttributeError('you must specify an ax')
    sample_features = np.array(sample_features)
    assert sample_features.ndim == 2
    assert x_lim in ['min_max', 'min_max_extend', 'box'] or len(x_lim) == 2
    assert y_lim in ['min_max', 'min_max_extend', 'box'] or len(y_lim) == 2

    ndim = sample_features.shape[1]
    if ndim != 2 and not t_sne:
        raise AttributeError('must use t_sne to preprocess features whose ndim is not 2')
    if ndim == 1:
        sample_features = np.concatenate([sample_features, np.zeros_like(sample_features)], 1)
        ndim = 2

    if t_sne and ndim != 2:
        sample_features = TSNE(n_components=2, init='pca', random_state=1, n_jobs=4, method='exact')\
            .fit_transform(sample_features)
    lower_bound_x, upper_bound_x = x_lim if len(x_lim) == 2 else \
        get_bounds(sample_features[:, 0], x_lim, extend_factor=0.2)
    lower_bound_y, upper_bound_y = y_lim if len(y_lim) == 2 else \
        get_bounds(sample_features[:, 1], y_lim, extend_factor=0.1)

    if contour:
        plot_contour([lower_bound_x, upper_bound_x], [lower_bound_y, upper_bound_y], z_generator, ax=ax)
    sns.scatterplot(x=sample_features[:, 0], y=sample_features[:, 1],
                    hue=hue,
                    hue_order=hue_order,
                    style=style,
                    style_order=style_order,
                    ax=ax)

    ax.legend(loc="best")
    ax.set_xlim(lower_bound_x, upper_bound_x)
    ax.set_ylim(lower_bound_y, upper_bound_y)
Exemplo n.º 3
0
 def _createFeatures(self, points):
     features_list = []
     bounds = utils.get_bounds(points)
     min_x = bounds.left
     avg_y = np.average(np.sum(points, 1))
     vectors = utils.get_vectors_from_points(
         points, normalize_vectors=False)  #Будет len(points)-1 векторов
     normalized_vectors = normalize(vectors)
     for i in range(len(points) - 1):  #Посчитать относительные координаты
         pt = points[i]
         rel_x = pt[0] - min_x  #x Относительно начала
         rel_y = pt[1] - avg_y  #y относительно среднего
         x_dir = normalized_vectors[i][0]  #Направление по x
         y_dir = normalized_vectors[i][1]  #Направление по y
         length = np.linalg.norm(vectors[i])  #Длина вектора
         features_list.append((rel_x, rel_y, x_dir, y_dir, length))
     return features_list
Exemplo n.º 4
0
def search(heightmap, row, col):
    row_bounds, col_bounds = get_bounds(heightmap)
    basin = list()
    basin.append((row, col))
    frontier = list()
    frontier.append((row, col))

    while len(frontier) > 0:
        r, c = frontier.pop()
        neighbor_indices = [(r - 1, c), (r, c - 1), (r, c + 1), (r + 1, c)]

        for r_i, c_i in neighbor_indices:
            if (r_i in row_bounds and c_i in col_bounds
                    and heightmap.data[r_i, c_i] != 9
                    and (r_i, c_i) not in basin):
                basin.append((r_i, c_i))
                frontier.append((r_i, c_i))
    return basin
Exemplo n.º 5
0
def draw_strokes(data, svg_filename, factor=0.2, padding=50, make_png=True):
    """
    little function that displays vector images and saves them to .svg
    :param data:
    :param factor:
    :param svg_filename:
    :return:
    """
    min_x, max_x, min_y, max_y = utils.get_bounds(data, factor)
    dims = (padding + max_x - min_x, padding + max_y - min_y)
    dwg = svgwrite.Drawing(svg_filename, size=dims)
    dwg.add(dwg.rect(insert=(0, 0), size=dims, fill='white'))
    lift_pen = 1
    abs_x = int(padding / 2) - min_x
    abs_y = int(padding / 2) - min_y
    p = "M%s, %s " % (abs_x, abs_y)
    # use lowcase for relative position
    command = "m"
    for i in range(len(data)):
        if lift_pen == 1:
            command = "m"
        elif command != "l":
            command = "l"
        else:
            command = ""
        x = float(data[i, 0]) / factor
        y = float(data[i, 1]) / factor
        lift_pen = data[i, 2]
        p += command + str(x) + ", " + str(y) + " "
    the_color = "black"
    stroke_width = 1
    dwg.add(dwg.path(p).stroke(the_color, stroke_width).fill("none"))
    dwg.save()

    if make_png:
        png_filename = svg_filename[:-4] + '.png'
        svg2png(bytestring=dwg.tostring(), write_to=png_filename)

    return dims, dwg.tostring()
Exemplo n.º 6
0
 def bounds(self):
     return get_bounds(self.x, self.y, self.radius)
Exemplo n.º 7
0
 def bounds(self):
     return get_bounds(*self.__center, self.__radius)
Exemplo n.º 8
0
def main():
    # Obtain input arguments
    parser = argparse.ArgumentParser(description='Process inputs to sampler')
    parser.add_argument('input_file', default=None, type=str)
    parser.add_argument('output_file', default="output.txt", type=str)
    parser.add_argument('N', default=1000, type=int)
    parser.add_argument('--plot_bool', action="store_true")

    # Parse input arguments
    args = parser.parse_args()
    input_file = args.input_file
    output_file = args.output_file
    N = args.N
    plot_bool = args.plot_bool

    np.random.seed(0)

    # Load file and the corresponding constraint functions and valid example
    if not path.isfile(input_file) and input_file:
        raise ValueError('Input file does not exist')
    elif input_file:
        constraint = Constraint(input_file)
        x0 = np.array(constraint.get_example())
        constraints = constraint.eval_constraints
        constraint_bool = constraint.apply
        constraint_bools = constraint.apply_list
    else:
        # Used for local testing
        constraints = func_constraints
        constraint_bool = func_constraints_bool
        x0 = x0

    # Obtain bounds of input space from which initial sample guesses will
    # be uniformly drawn
    bounds = get_bounds(x0, constraint_bools)

    # Find valid uniformly distributed samples with constrained SMC
    N_star = int(N * 1.1)
    run = RunSMC(N=N_star,
                 bounds=bounds,
                 type="constrained_smc",
                 tau_T=1e7,
                 constraints=constraints)
    x, x0 = run.get_x(), run.get_x0()

    # Evaluate validity. uniqueness, and spread of results
    # Subsample result
    acc, x_valid = get_accuracy(x, constraint_bool)
    idx_x, idx_x_valid = np.arange(x.shape[0]), np.arange(x_valid.shape[0])
    x = x_valid[np.random.choice(idx_x_valid, N, replace=False)] if x_valid.shape[0] >= N else \
        x[np.random.choice(idx_x, N, replace=False)]
    std = np.std(x, axis=0)
    uniq_nums = np.unique(x, axis=0).shape[0]

    print('accuracy: {acc} std: {std} unique candidates: {uniq}'.format(
        acc=acc, std=std, uniq=uniq_nums))

    # Save output and plot if 2D and plot_bool
    np.savetxt(output_file, x, delimiter=' ')
    if x.shape[1] == 2 and plot_bool:
        plt.plot(x0[:, 0], x0[:, 1], 'o', x[:, 0], x[:, 1], '*')
        plt.show()
Exemplo n.º 9
0
def dream_image(image, settings, out_name):
    print("Processing...")

    ####Global settings for every renderer
    img = image
    orig_img = image  #maybe clean up

    #Iterations and octaves
    iterations = settings['iterations']
    octave_n = settings['octaves']
    octave_scale = settings['octave_scale']
    iteration_descent = settings['iteration_descent']

    #Additional Settings
    save_gradient = settings['save_gradient']
    background_color = settings['background']

    #tf.random.set_seed(settings.tf_seed)#not working?

    #Renderers
    renderers = settings['renderers']

    ###Set layers and channels
    t_obs = []
    for r in renderers:
        t_obs.append(
            set_layer(r['layer'], r['squared'], r['f_channel'],
                      r['l_channel']))

    ##Create background

    g_sum = np.zeros_like(img)

    # split the image into a number of octaves

    octaves = []
    g_sums = []

    #Prepare Image & backgrounds for every octave
    for i in range(octave_n - 1):
        hw = img.shape[:2]
        lo = resize(img, np.int32(np.float32(hw) / octave_scale))
        hi = img - resize(lo, hw)
        img = lo
        octaves.append(hi)

        lo = resize(g_sum, np.int32(np.float32(hw) / octave_scale))
        hi = g_sum - resize(lo, hw)
        g_sum = lo
        g_sums.append(hi)

    # generate details octave by octave
    for octave in range(octave_n):

        ##Prepare current Octave
        if octave > 0:
            hi = octaves[-octave]

            img = resize(img, hi.shape[:2]) + hi

            hi_g = g_sums[-octave]

            g_sum = resize(g_sum, hi.shape[:2]) + hi_g

        ##More Preperations
        bounds = utils.get_bounds(img.shape[1], img.shape[0], renderers)
        iteration_masks = []
        for r in renderers:
            if r['masked']:
                iteration_masks.append(resize(r['mask'], img.shape[:2]) /
                                       255)  #move up, /255 just once
            else:
                iteration_masks.append([])
        orig_img_m = resize(image,
                            img.shape[:2]) / 255  #move up, /255 just once

        ####Iterations

        for iteration in range(iterations - octave * iteration_descent):

            print("Iteration " + str(iteration + 1) + " / " +
                  str(iterations - octave * iteration_descent) + " Octave: " +
                  str(octave + 1) + " / " + str(octave_n))
            ####Gradient

            gradients = []

            for i in range(len(renderers)):
                if (iteration + 1) % renderers[i]['render_x_iteration'] == 0:
                    start_time = time.time()
                    ##Pre Gradient preperations

                    #Crop the image
                    t_img = img[bounds[i][2]:bounds[i][3],
                                bounds[i][0]:bounds[i][1]]

                    #Rotate if true
                    if renderers[i]['rotate']:
                        t_img = np.rot90(t_img, renderers[i]['rotation'])

                    ##Get the gradient
                    g = calc_grad_tiled(t_img,
                                        t_obs[i],
                                        tile_size=renderers[i]['tile_size'])

                    g = g * (renderers[i]['step_size'] /
                             (np.abs(g).mean() + 1e-7))

                    ##
                    ##Gradient manipulations:
                    ##
                    #Rotate back if necessary
                    if renderers[i]['rotate']:
                        g = np.rot90(g, 4 - renderers[i]['rotation'])

                    #Masking the gradient
                    if renderers[i]['masked']:
                        g *= iteration_masks[i][bounds[i][2]:bounds[i][3],
                                                bounds[i][0]:bounds[i][1]]

                    #Color Correction
                    if renderers[i]['color_correction']:
                        g = utils.gradient_grading(
                            g,
                            orig_img_m[bounds[i][2]:bounds[i][3],
                                       bounds[i][0]:bounds[i][1]],
                            method=renderers[i]['cc_vars'][0],
                            fr=renderers[i]['cc_vars'][1],
                            fg=renderers[i]['cc_vars'][2],
                            fb=renderers[i]['cc_vars'][3])

                    ##Adding the finalized Gradient to list
                    gradients.append(g)

                    print('Finished computing Gradient for Renderer {} in {}s'.
                          format(i,
                                 time.time() - start_time))
                else:
                    gradients.append(np.zeros_like(img))
            for i in range(len(bounds)):

                img[bounds[i][2]:bounds[i][3],
                    bounds[i][0]:bounds[i][1]] += gradients[i]
                g_sum[bounds[i][2]:bounds[i][3],
                      bounds[i][0]:bounds[i][1]] += gradients[i]

####Save Image
    if settings['color_correction']:
        img = image + utils.gradient_grading(g_sum,
                                             image / 255,
                                             method=settings['cc_vars'][0],
                                             fr=settings['cc_vars'][1],
                                             fg=settings['cc_vars'][2],
                                             fb=settings['cc_vars'][3])

    g_sum[:, :] += background_color
    utils.save_image(img, out_name)
    if save_gradient:
        utils.save_image(g_sum, 'gradient_' + out_name)