Пример #1
0
def show_in_8bits(fnames, fig=None, axes=None, ncols=3, figsize=(20,20), verbose=False):
    """
    show raster image in 8 bits. It silently performs 16 to 8bit conversion 
    if the input is 16 bits by calling `convert_to_8bits` in apls_tools.py
    """
    from math import ceil
    nrows = max(1, int(ceil(len(fnames)/ncols)))
    
    if fig is None or axes is None: 
        fig,axes = plt.subplots(nrows,ncols,figsize=figsize)
    axes = axes.flatten()

    for i,fname in enumerate(fnames):
        if not isinstance(fname, str):
            fname = str(fname)
        img = cv2.imread(fname,-1)

        if img.dtype == 'uint16':
            if verbose:
                print("{} is 16bits. We'll convert it to 8bits".format(fname))
            apls_tools.convert_to_8Bit(fname, './temp.tif')
            img = cv2.imread('./temp.tif',-1)
        assert(img.dtype != 'uint16')

        axes[i].imshow(img)
        axes[i].set_title("_".join(fname.split('_')[-2:]))
    
    # Delete any empty axes
    for j in range(len(fnames),len(axes)): 
        fig.delaxes(axes[j])

    return fig,axes[:len(fnames)]
Пример #2
0
def main():

    parser = argparse.ArgumentParser()
    parser.add_argument('--buffer_meters',
                        default=2,
                        type=float,
                        help='Buffer distance (meters) around graph')
    parser.add_argument('--burnValue',
                        default=150,
                        type=int,
                        help='Value of road pixels (for plotting)')
    parser.add_argument(
        '--test_data_loc',
        default='AOI_2_Vegas_Train',
        type=str,
        help='Folder within sample_data directory of test data')
    args = parser.parse_args()

    # set paths
    path_apls = os.path.dirname(path_apls_src)
    path_data = os.path.join(path_apls, 'sample_data/' + args.test_data_loc)
    path_outputs = os.path.join(path_apls,
                                'example_output_ims/' + args.test_data_loc)
    path_images_raw = os.path.join(path_data, 'RGB-PanSharpen')
    path_images_8bit = os.path.join(path_data, 'RGB-PanSharpen_8bit')
    path_labels = os.path.join(path_data, 'truthGeoJson/spacenetroads')
    # output directories
    path_masks = os.path.join(path_outputs,
                              'masks_' + str(args.buffer_meters) + 'm')
    path_masks_plot = os.path.join(
        path_outputs, 'masks_' + str(args.buffer_meters) + 'm_plots')
    # create directories
    for d in [path_outputs, path_images_8bit, path_masks, path_masks_plot]:
        if not os.path.exists(d):
            os.mkdir(d)

    # iterate through images, convert to 8-bit, and create masks
    im_files = os.listdir(path_images_raw)
    for im_file in im_files:
        if not im_file.endswith('.tif'):
            continue

        name_root = im_file.split('_')[-1].split('.')[0]

        # create 8-bit image
        im_file_raw = os.path.join(path_images_raw, im_file)
        im_file_out = os.path.join(path_images_8bit, im_file)
        # convert to 8bit
        apls_tools.convert_to_8Bit(im_file_raw,
                                   im_file_out,
                                   outputPixType='Byte',
                                   outputFormat='GTiff',
                                   rescale_type='rescale',
                                   percentiles=[2, 98])

        # determine output files
        label_file = os.path.join(path_labels, 'spacenetroads_AOI_2_Vegas_' \
                                     + name_root + '.truthGeoJson')
        label_file_tot = os.path.join(path_labels, label_file)
        output_raster = os.path.join(path_masks, 'mask_' + name_root + '.png')
        plot_file = os.path.join(path_masks_plot, 'mask_' + name_root + '.png')

        print("\nname_root:", name_root)
        print("  output_raster:", output_raster)
        print("  output_plot_file:", plot_file)

        # create masks
        mask, gdf_buffer = apls_tools.get_road_buffer(
            label_file_tot,
            im_file_out,
            output_raster,
            buffer_meters=args.buffer_meters,
            burnValue=args.burnValue,
            bufferRoundness=6,
            plot_file=plot_file,
            figsize=(6, 6),  #(13,4), 
            fontsize=8,
            dpi=200,
            show_plot=False,
            verbose=False)
    return
Пример #3
0
def main():

    parser = argparse.ArgumentParser()
    parser.add_argument('--buffer_meters', default=2, type=float,
                        help='Buffer distance (meters) around graph')
    parser.add_argument('--burnValue', default=150, type=int,
                        help='Value of road pixels (for plotting)')
    parser.add_argument('--filepath', type=str,
                        default='/home/ananya/Documents/data/spacenet/SpaceNet_Buildings_Competition_Round2_Sample/AOI_3_Paris_Roads_Train/')
    parser.add_argument('--imgSizePix', type=int, default=416)
    args = parser.parse_args()

    # set paths
    path_apls_src = os.path.dirname(os.path.realpath(__file__))
    path_apls = os.path.dirname(path_apls_src)
    path_data = args.filepath
    path_outputs = os.path.join(args.filepath, 'annotations')
    path_images_raw = os.path.join(path_data, 'RGB-PanSharpen')
    path_images_8bit = os.path.join(path_data, 'RGB-PanSharpen_clip_8bit')
    path_labels = os.path.join(path_data, 'geojson/spacenetroads')
    # output directories
    path_masks = os.path.join(
        path_outputs, 'clip_masks_' + str(args.buffer_meters) + 'm')
    path_masks_plot = os.path.join(
        path_outputs, 'clip_masks_' + str(args.buffer_meters) + 'm_plots')
    # create directories
    for d in [path_outputs, path_images_8bit, path_masks, path_masks_plot]:
        if not os.path.exists(d):
            os.mkdir(d)

    # iterate through images, chip, convert to 8-bit, and create masks
    im_files = os.listdir(path_images_raw)
    for im_file in im_files:
        if not im_file.endswith('.tif'):
            continue
        # chip images
        name_root = im_file.split('_')[-1].split('.')[0]
        label_file = os.path.join(path_labels, 'spacenetroads_AOI_3_Paris_'
                                  + name_root + '.geojson')
        chipSummaryList = processRasterChip(os.path.join(path_images_raw,im_file), path_images_raw,
                                            label_file, os.path.dirname(label_file),
                                            outputDirectory=path_outputs,
                                            imagePixSize=args.imgSizePix, clipOverlap=0.0, randomClip=False,
                                            minpartialPerc=0.0,
                                            outputPrefix='')
        for chipSummary in chipSummaryList:
            # create 8-bit image
            # im_file_raw = os.path.join(path_images_raw, im_file)
            # im_file_out = os.path.join(path_images_8bit, im_file)
            # annotationName = os.path.basename(chipSummary['rasterSource'])
            # annotationName = os.path.join(path_outputs, annotationName)

            im_file_raw = chipSummary['rasterSource']
            im_file_out = os.path.join(
                path_images_8bit, chipSummary['chipName'])
            # convert to 8bit
            apls_tools.convert_to_8Bit(im_file_raw, im_file_out,
                                       outputPixType='Byte',
                                       outputFormat='GTiff',
                                       rescale_type='rescale',
                                       percentiles=[2, 98])

            # determine output files
            # label_file = os.path.join(path_labels, 'spacenetroads_AOI_3_Paris_'
            #                           + name_root + '.geojson')
            # label_file_tot = os.path.join(path_labels, label_file)
            label_file_tot = chipSummary['geoVectorName']
            output_raster = os.path.join(
                path_masks, 'mask_' + name_root + '.tif')
            plot_file = os.path.join(
                path_masks_plot, 'mask_' + name_root + '.png')

            print("\nname_root:", name_root)
            print("  output_raster:", output_raster)
            print("  output_plot_file:", plot_file)

            # create masks
            mask, gdf_buffer = apls_tools.get_road_buffer(label_file_tot, im_file_out,
                                                          output_raster,
                                                          buffer_meters=args.buffer_meters,
                                                          burnValue=args.burnValue,
                                                          bufferRoundness=6,
                                                          plot_file=plot_file,
                                                          # (13,4),
                                                          figsize=(6, 6),
                                                          fontsize=8,
                                                          dpi=200, show_plot=False,
                                                          verbose=False)
    return
Пример #4
0
def create_masks(path_data,
                 buffer_meters=2,
                 n_bands=3,
                 burnValue=150,
                 make_plots=True,
                 overwrite_ims=False,
                 output_df_file='',
                 header=[
                     'name', 'im_file', 'im_vis_file', 'mask_file',
                     'mask_vis_file'
                 ]):
    '''
    Create masks from files in path_data.
    Write 8bit images and masks to file.
    Return a dataframe of file locations with the following columns:
        ['name', 'im_file', 'im_vis_file', 'mask_file', 'mask_vis_file']
    We record locations of im_vis_file and mask_vis_file in case im_file
      or mask_file is not 8-bit or has n_channels != [1,3]
    if using 8band data, the RGB-PanSharpen_8bit should already exist, so
        3band should be run prior to 8band
    '''

    t0 = time.time()
    # set paths
    #path_apls = os.path.dirname(path_apls_src)
    #path_data = os.path.join(path_apls, args.test_data_loc)
    path_labels = os.path.join(path_data, 'geojson/spacenetroads')
    # output directories
    path_masks = os.path.join(path_data, 'masks_' + str(buffer_meters) + 'm')
    path_masks_plot = os.path.join(path_data, 'masks_' \
                                   + str(buffer_meters) + 'm_plots')
    # image directories
    path_images_vis = os.path.join(path_data, 'RGB-PanSharpen_8bit')
    if n_bands == 3:
        path_images_raw = os.path.join(path_data, 'RGB-PanSharpen')
        path_images_8bit = os.path.join(path_data, 'RGB-PanSharpen_8bit')
    else:
        path_images_raw = os.path.join(path_data, 'MUL-PanSharpen')
        path_images_8bit = os.path.join(path_data, 'MUL-PanSharpen_8bit')
        if not os.path.exists(path_images_vis):
            print("Need to run 3band prior to 8band!")
            return

    # create directories
    for d in [path_images_8bit, path_masks, path_masks_plot]:
        if not os.path.exists(d):
            os.mkdir(d)

    # iterate through images, convert to 8-bit, and create masks
    outfile_list = []
    im_files = os.listdir(path_images_raw)
    nfiles = len(im_files)
    for i, im_name in enumerate(im_files):
        if not im_name.endswith('.tif'):
            continue

        # define files
        name_root = 'AOI' + im_name.split('AOI')[1].split('.')[0]
        im_file_raw = os.path.join(path_images_raw, im_name)
        im_file_out = os.path.join(path_images_8bit, im_name)
        im_file_out_vis = im_file_out.replace('MUL', 'RGB')
        ## get visible file (if using 8band imagery we want the 3band file
        ##   for plotting purposes)
        #if n_bands == 3:
        #    im_file_out_vis = im_file_out
        #else:
        #    name_vis = im_name.replace('MUL', 'RGB')
        #    im_file_out_vis = os.path.join(path_images_vis, name_vis)

        # convert to 8bit, if desired
        if not os.path.exists(im_file_out) or overwrite_ims:
            apls_tools.convert_to_8Bit(im_file_raw,
                                       im_file_out,
                                       outputPixType='Byte',
                                       outputFormat='GTiff',
                                       rescale_type='rescale',
                                       percentiles=[2, 98])

        # determine output files
        #label_file = os.path.join(path_labels, 'spacenetroads_AOI_2_Vegas_' \
        #                             + name_root + '.geojson')
        label_file = os.path.join(path_labels, 'spacenetroads_' + name_root \
                                  + '.geojson')
        label_file_tot = os.path.join(path_labels, label_file)
        mask_file = os.path.join(path_masks, name_root + '.png')
        if make_plots:
            plot_file = os.path.join(path_masks_plot, name_root + '.png')
        else:
            plot_file = ''

        print("\n", i + 1, "/", nfiles)
        print("  im_name:", im_name)
        print("  name_root:", name_root)
        print("  im_file_out:", im_file_out)
        print("  mask_file:", mask_file)
        print("  output_plot_file:", plot_file)

        # create masks
        if not os.path.exists(mask_file) or overwrite_ims:
            mask, gdf_buffer = apls_tools.get_road_buffer(
                label_file_tot,
                im_file_out_vis,
                mask_file,
                buffer_meters=buffer_meters,
                burnValue=burnValue,
                bufferRoundness=6,
                plot_file=plot_file,
                figsize=(6, 6),  #(13,4), 
                fontsize=8,
                dpi=500,
                show_plot=False,
                verbose=False)

        # resize in ingest so we don't have to save the very large arrays
        outfile_list.append(
            [im_name, im_file_out, im_file_out_vis, mask_file, mask_file])

    # make dataframe and save
    df = pd.DataFrame(outfile_list, columns=header)
    if len(output_df_file) > 0:
        df.to_csv(output_df_file, index=False)
    print("\ndf.ix[0]:", df.ix[0])
    print("\nTotal data length:", len(df))
    t4 = time.time()
    print("Time to run create_masks():", t4 - t0, "seconds")
    return df