Exemplo n.º 1
0
def segments_main_image(path_image, output_path, little_shape,
    format=(9,16)):
    image = imread(path_image)
    main_size = (little_shape[0] * format[0], little_shape[1] * format[1])
    # print(main_size)
    # print(little_shape)
    # print(format)
    # print(image.shape)
    #jk = input()
    output = resize_format(image, main_size)
    # imshow(output)
    cont = 0
    n_little = format[0]
    for i in range(n_little):
        for j in range(format[1]):
            filename_out = (
                join_path(output_path, "image_" + format_number(cont)) +
                '.png'
                )
            if is_file_exist(filename_out):
                break
            p1 = (little_shape[0] * i, little_shape[1] * j)
            p2 =(little_shape[0] * (i + 1), little_shape[1] * (j + 1))
            crop = crop_image(output, p1, p2)
            # imshow(crop)
            # time.sleep(0.3)
            imwrite(filename_out, crop)
            cont += 1
        print('creating small images... progress: ' +
            format_percent(100*(i+1)/n_little)+ '%        ', end='\r')
    print('')
    pass
Exemplo n.º 2
0
def generate_mosaic(mosaic_shape, correlation_matrix, set_files,
    position_matrix, mini_image_shape, output_path,
    output_filenames_list_pos=None):
    hmos = mini_image_shape[0] * mosaic_shape[0]
    wmos = mini_image_shape[1] * mosaic_shape[1]
    mosaic = np.zeros((hmos, wmos, 3), dtype = np.uint8)
    # las correlaciones no exceden este valor
    max_corr = 12
    index = [ (int(i),int(j))  for d,i,j in distances_to_point(mosaic_shape)]
    number_mini_images = len(index)
    hmin,wmin = mini_image_shape[:2]
    filenames_list = [[(0,0), ''] for i in range(number_mini_images)]
    # print(mini_image_shape)
    # print(mosaic_shape)
    #for item in index:
    #    print(item)
    #k = input()
    #print(mosaic_shape[0])
    #input()
    for n in range(number_mini_images):
        i, j = index[n]
        col = i *  mosaic_shape[1] + j
        #print(col)
        #jk = input()
        argmin = np.argmin(correlation_matrix[:,col].flatten())
        #print(argmin)
        #min_corr = correlation_matrix[argmin, col]
        #if min_corr == 12:
        #    print(min_corr)
        #    print(correlation_matrix[:,col])
        #    input()
        correlation_matrix[:, col] = max_corr
        correlation_matrix[argmin, :] = max_corr
        #print(correlation_matrix[:, col])
        #print(correlation_matrix[argmin, :])
        #input()
        pos = position_matrix[argmin, col]
        fn = set_files[argmin]
        mini_image = imread(fn)
        filenames_list[n] = [(i,j), fn]
        rot_mini_image = imgrotate(mini_image, pos)
        #print(mini_image_shape)
        #print(rot_mini_image.shape)
        #print(i, ' ',j)
        #jk = input()
        mosaic[hmin*i:hmin*i+hmin, wmin*j:wmin*(j+1)] = rot_mini_image
    #---------------------------------------------------------------------------
    if not is_file_exist(output_path):
        imwrite(output_path, mosaic)
    #print(output_path)
    #jk = input()
    if not output_filenames_list_pos == None:
        #save_list(output_filenames_list_pos, filenames_list)
        cretare_json_features(output_filenames_list_pos, '', set_files, (hmos, wmos), mosaic_shape, mini_image_shape, list_pos_files)
Exemplo n.º 3
0
def create_small_images(set_path, resize_path, little_shape, check=False):
    # ya se tienen los archivos, se varifican si existen en el path de salida
    # se crea un directorio donde guardar los archivos
    set_files = files_from_dir(set_path, root=False)
    mkdir(resize_path)
    n_little = len(set_files)
    # se recorren los archivo
    if check:
        for i,f in enumerate(set_files):
            fileout = join_path(resize_path, f)
            if not is_file_exist(fileout):
                #print('File not found, creating image...')
                limage = imread(join_path(set_path, f))
                #limage = imread(f)
                resize = resize_image(limage, little_shape[:2])
                #imshow(resize, axis='on')
                __ = imwrite(join_path(resize_path, f), resize)
            else:
                limage = imread(fileout)
                if limage.shape != little_shape:
                    limage = imread(join_path(set_path, f))
                    #limage = imread(f)
                    resize = resize_image(limage, little_shape[:2])
                    #imshow(resize, axis='on')
                    __ = imwrite(join_path(resize_path, f), resize)
            print('creating small images... progress: ' +
                format_percent(100*(i+1)/n_little)+ '%       ', end='\r')
    else:
        for i,f in enumerate(set_files):
            fileout = join_path(resize_path, f)
            if not is_file_exist(fileout):
                #print('File not found, creating image...')
                limage = imread(join_path(set_path, f))
                #limage = imread(f)
                resize = resize_image(limage, little_shape[:2])
                #imshow(resize, axis='on')
                _ = imwrite(join_path(resize_path, f), resize)
            print('creating small images... progress: ' +
                format_percent(100*(i+1)/n_little)+ '%       ', end='\r')
    print('')
Exemplo n.º 4
0
def correlation_matrix_resize(path_image, path_output_main_image, set_path,
    mini_little_shape, format=(9,16), listpos=None,
    generate_main_image=True, scale=1):
    # se lee la imagen principal
    little_shape = tuple([int(item*scale) for item in mini_little_shape])
    print('reading main image')
    image = imread(path_image)
    print('creating new main image')
    if is_file_exist(path_output_main_image):
        output = imread(path_output_main_image)
        #imwrite(path_output_main_image, output)
    elif(generate_main_image):
        main_size = (little_shape[0] * format[0], little_shape[1] * format[1])
        output = resize_format(image, main_size)
        imwrite(path_output_main_image, output)
    #imshow(main_image)
    print('reshape main image')
    output = reshape_main_image(output, format, little_shape)
    n = len(set_path)
    print('create empty correlation matrix')
    mat_corr = np.zeros((n, format[0]*format[1]), dtype=np.float32)
    print('create empty position matrix')
    pos_mat = np.zeros((n,format[0]*format[1]), dtype=np.uint8)
    print('comparitions')
    #n_elem = int(np.prod(little_shape))
    #print(pos_mat.shape)
    for i,filename in enumerate(set_path):
        mini_image = imread(filename)
        mini_image_resize = square_image(mini_image, little_shape)
        #mat_corr[i,:] = get_correlation(output, mini_image)
        #mat_corr[i,:] = np.log(1 + distance)
        mat_corr[i,:],pos_mat[i,:] = get_correlation(output, mini_image_resize,
            format, listpos=listpos)
        #print(pos_mat[i,:])
        #jk = input()
        print('creating correlation matrix... progress: ' +
            format_percent(100*i/n)+ '%        ', end='\r')
    print('creating correlation matrix... progress: 100%         ')
    return mat_corr, pos_mat
Exemplo n.º 5
0
def main():
    """
    set_path = 'set_images'
    resize_path = 'output_images/resize'
    main_image = 'main_images/jennifer.jpg'
    segments_path = 'output_images/segments'
    correlation_file = 'output_images/correlation_file'
    path_output_main_image = 'output_images/main_image/output_main.png'
    path_output_filename_small = 'output_images/path_output_filename_small.txt'
    output_path_mosaic = 'output_images/mosac_output.png'
    """
    # files
    main_image = 'main_images/jennifer.jpg'
    correlation_file = 'output_images/correlation_file'
    path_output_filename_small = 'output_images/path_output_filename_small.txt'
    output_path_mosaic = 'output_images/mosac_output.png'
    path_output_main_image = 'output_images/main_image/output_main.png'

    # folders
    set_path = 'set_images'
    output_path = 'output_images'
    files_dict = mk_all_dirs(output_path, root=True)
    resize_path = files_dict['resize']

    # --------------------------------------------------------------------------
    # se escoge el tamano apropiado para las imagenes pequenas

    #little_shape = (590, 590, 3)
    little_shape = (32,32,3)
    shape_images = (18,32) #16:9

    #little_shape = (295, 295, 3)
    #shape_images = (36, 64) #16:9

    #"""
    #shape_images = (28,21) #4:3
    #"""
    #"""
    #shape_images = (24,24) #1:1
    #"""
    # --------------------------------------------------------------------------

    # se leen las imagenes pequenas
    if (is_file_exist(path_output_filename_small)):
        set_files = load_list(path_output_filename_small)
    else:
        set_files = files_from_dir(set_path, root=False)
        set_files.sort()
        save_list(path_output_filename_small, set_files)
    """
    set_files = files_from_dir(set_path, root=False)
    set_files.sort()
    n_little = len(set_files)
    for f in set_files: print(f)
    """
    # --------------------------------------------------------------------------
    create_small_images(set_path, resize_path, little_shape)
    # --------------------------------------------------------------------------
    # segments_main_image(main_image, segments_path, little_shape, shape_images)
    #segments_files = files_from_dir(segments_path, root=True)
    #segments_files.sort()
    # --------------------------------------------------------------------------
    resize_files = files_from_dir(resize_path, root=True)
    resize_files.sort()
    # --------------------------------------------------------------------------
    if not is_file_exist(correlation_file + '.npz'):
        print('creating a correlation matrix and save... ')
        pos_list = list(range(8))
        corr_mat, pos_mat =  correlation_matrix(main_image, path_output_main_image, resize_files, little_shape, shape_images, pos_list)
        np.savez_compressed('output_images/correlation_file', a=corr_mat, b=pos_mat)
    else:
        print('load correlation matrix... ')
        loaded = np.load(correlation_file + '.npz')
        corr_mat = loaded['a']
        pos_mat = loaded['b']

    #print(corr_mat.shape)
    # -----------------------------------------------------------------------------
    # -----------------------------------------------------------------------------
    #index = [ (int(i),int(j)) for d,i,j in distances_to_point(shape_images)]
    # -----------------------------------------------------------------------------
    # -----------------------------------------------------------------------------
    new_set_files = [join_path(resize_path, item) for item in set_files]
    generate_mosaic(shape_images, corr_mat, new_set_files, pos_mat, little_shape, output_path_mosaic)

    # -----------------------------------------------------------------------------
    # -----------------------------------------------------------------------------

    print('success!')
Exemplo n.º 6
0
def main():

    # folders
    set_path = 'set_images' # folder de imagenes pequenas # INPUT
    output_path = 'output_images' # folder donde se almancenan los archivos salida # INPUT
    __ = mk_all_dirs('output_images', root=True)

    # files
    main_image = 'main_images/jennifer.jpg' # imagen original # INPUT
    correlation_file = join_path(output_path,'correlation_file') # archivo de la matrix de correlacion
    path_output_filename_small = join_path(output_path,'path_output_filename_small.txt') # lista de miniarchivos
    output_path_mosaic = join_path(output_path,'mosaic_output.png') # mosaico
    path_output_main_image = join_path(output_path,'main_image/output_main.png') # salida output
    output_filenames_list_pos = join_path(output_path, 'filenames_and_positions.txt')
    output_photo_path = join_path(output_path, 'output_photo_path')

    # --------------------------------------------------------------------------
    # se escoge el tamano apropiado para las imagenes pequenas

    #little_shape = (590, 590, 3)
    little_shape = (32,32,3)
    shape_images = (36,64) #16:9

    #little_shape = (295, 295, 3)
    #shape_images = (36, 64) #16:9
    #shape_images = (28,21) #4:3
    #shape_images = (24,24) #1:1
    # --------------------------------------------------------------------------
    # se leen las imagenes pequenas
    if (is_file_exist(path_output_filename_small)):
        set_files = load_list(path_output_filename_small)
    else:
        set_files = files_from_dir(set_path, root=False)
        set_files.sort()
        save_list(path_output_filename_small, set_files)
    # --------------------------------------------------------------------------
    # --------------------------------------------------------------------------
    #resize_files = files_from_dir(resize_path, root=True)
    #resize_files.sort()
    new_set_path = [join_path(set_path, item) for item in set_files]
    # --------------------------------------------------------------------------
    # --------------------------------------------------------------------------
    if not is_file_exist(correlation_file + '.npz'):
        print('creating a correlation matrix and save... ')
        pos_list = list(range(8))
        corr_mat, pos_mat =  correlation_matrix_resize(main_image, path_output_main_image, new_set_path, little_shape, shape_images, pos_list)
        np.savez_compressed('output_images/correlation_file', a=corr_mat, b=pos_mat)
    else:
        print('load correlation matrix... ')
        loaded = np.load(correlation_file + '.npz')
        corr_mat = loaded['a']
        pos_mat = loaded['b']
    # --------------------------------------------------------------------------
    # --------------------------------------------------------------------------
    generate_mosaic_resize(shape_images, corr_mat, new_set_path, pos_mat, little_shape, output_path_mosaic, output_filenames_list_pos)
    # --------------------------------------------------------------------------
    # --------------------------------------------------------------------------
    create_photos(output_photo_path, output_filenames_list_pos)
    # --------------------------------------------------------------------------
    # --------------------------------------------------------------------------
    print('success!')