def get_images(self, imageId, img_key=None):
        '''
        Load images correspoding to imageId
    
        Parameters
        ----------
        imageId : str
            imageId as used in grid_size.csv
        img_key : str 
                  {None, '3', 'A', 'M', 'P'}, optional
                  Specify this to load single image
                  None loads all images (i.e. every band of the same image) and returns in a dict
                  '3' loads image from three_band/
                  'A' loads '_A' image from sixteen_band/
                  'M' loads '_M' image from sixteen_band/
                  'P' loads '_P' image from sixteen_band/
        Returns
        -------
        images : dict
                 A dict of image data from TIFF files as numpy array
        '''

        creator = gdal_utils()

        img_names = self.get_image_names(imageId)
        images = dict()
        if img_key is None:
            for k in img_names.keys():
                images[k] = creator.gdal_to_nparr(img_names[k])
        else:
            images[img_key] = creator.gdal_to_nparr(img_names[img_key])
        return images
Exemplo n.º 2
0
def main():
    ''' Performs Pansharpening with M bands and Panchromatic Band.'''

    # Base Address
    inDir = os.getcwd()

    # Train-images multiploygon co-ordinates
    df = pd.read_csv(os.path.join(inDir, 'Data/train_wkt_v4.csv'))
    print(df.head())

    # Distinct imageIds in the DataFrame
    trainImageIds = []

    for i in range(len(df)):
        string = (df.iloc[i, 0])

        if string not in trainImageIds:
            trainImageIds.append(string)

    print("The are " + str(len(trainImageIds)) + " distinct ids." +
          str(df.head()))

    trainImageIds.sort()

    creator = gdal_utils()

    for imageID in trainImageIds:

        string_base = "gdal_sharpen.py "

        bands = "-b 1 -b 2 -b 3 -b 4 -b 5 -b 6 -b 7 -b 8 "

        panchro = os.path.join(inDir,
                               "Data/sixteen_band/" + imageID + "_P.tif ")
        print(panchro)

        m_band = os.path.join(inDir,
                              "Data/sixteen_band/" + imageID + "_M.tif ")
        print(m_band)

        output = os.path.join(
            inDir, "Data/pan_sharpened_images/" + imageID + "_M.tif")
        print(output)

        final = string_base + bands + panchro + m_band + output
        print(final)

        creator.gdal_pansharpen(final)
    def generate_all_masks(self):

        # Base Address
        inDir = os.getcwd()

        # Helper class
        creator = gdal_utils()

        # train-images multiploygon co-ordinates
        df = pd.read_csv(os.path.join(inDir, 'Data/train_wkt_v4.csv'))
        print(df.head())

        # grid size will also be needed later..
        gs = pd.read_csv(os.path.join(inDir, 'Data/grid_sizes.csv'),
                         names=['ImageId', 'Xmax', 'Ymin'],
                         skiprows=1)
        print(gs.head())

        # Distinct imageIds in the DataFrame
        trainImageIds = self.get_distinct_ids(df)

        for key, classes in enumerate(self.CLASSES):

            base = os.path.join(os.getcwd(), 'Data_masks')
            path = os.path.join(base, self.CLASSES[classes])

            print(base)
            print(path)
            print(classes)

            for imageId in trainImageIds:

                image = self.get_images(imageId, 'P')
                print("imageId : {}, image_shape : {}".format(
                    imageId, image['P'].shape))

                mask = self.generate_mask_for_image_and_class(
                    image['P'].shape, imageId, classes, gs, df)
                print("mask : {}".format(mask.shape))

                ref_raster_fn = os.path.join(
                    os.getcwd(), "Data/sixteen_band/" + imageId + "_P.tif")
                new_raster_fn = os.path.join(
                    path, imageId + "_" + self.CLASSES[classes] + ".tif")

                temp = creator.create_tiff_file_from_array(
                    ref_raster_fn, new_raster_fn, mask * 255)
                temp = None
Exemplo n.º 4
0
def get_training_image_pair(files_train, imageId):
    ''' Gets the input,truth for an image.

    Description :
        Get image-ground-truth pair of the image with id "imageId" from the list of
        training images "files_train"

    Arguments :
        files_train -- List.
                       The list of names of the tiff images that will be used for training
        imageId     -- String.
                       The id of the image.
                       ex. '6010_0_0'
    Returns :
        (image_train,truth) -- Tuple.
                               This tuple containing the input image and the ground truth.
    '''

    if imageId not in files_train:
        raise ValueError("Invalid value of imageId")
        return None

    # Using gdal to read the input image
    reader = gdal_utils()
    path = os.path.join(os.getcwd(), "Data/image_stacks/" + imageId + ".tif")
    image_train = reader.gdal_to_nparr(path)

    if image_train is None:
        print("Failed to load image. Wrong path name provided.")
        return None

    # Using gdal to read the ground truth
    path = os.path.join(os.getcwd(),
                        "Data_masks/Fast_H20/" + imageId + "_Fast_H20.tif")
    truth = reader.gdal_to_nparr(path)

    if truth is None:
        print("Failed to load groung truth. Wrong path name provided.")
        return None

    return (image_train, truth)
Exemplo n.º 5
0
        return thresh*255

############################################# MODEL BUILDING #############################################

if __name__ == '__main__':

    files_train,files_test = get_masks_list()

    (image_test,truth1) = get_testing_image_pair(files_test,files_test[1])
    
    # Predictions and Results before Post Processing 
    answer = model(image_test,truth1,img_rows=112,img_cols=112,num_channels=9)
    result = compute_jaccard(truth1/255,answer/255)
    answer = answer.astype(dtype = np.uint8)
    
    writer_1 = gdal_utils()
    writer_1.create_tiff_file_from_array(os.path.join(os.getcwd(),"Data_masks/Road/Test/" + files_test[1] + "_Road.tif"),os.path.join(os.getcwd(),"Results/pred_Roads.tif"),answer)    
    
    # Post Processed    
    re = post_processing(answer/255)
    result = compute_jaccard(truth1/255,re)
    print("Post processing : {}".format(result))
    
    re = re*255
    re = re.astype(dtype = np.uint8)
    plt.imshow(re[:,:,0])    
    
    writer_2 = gdal_utils()
    writer_2.create_tiff_file_from_array(os.path.join(os.getcwd(),"Data_masks/Road/Test/" + files_test[1] + "_Road.tif"),os.path.join(os.getcwd(),"Results/processed_pred_Roads.tif"),re)        
    
    
Exemplo n.º 6
0
if __name__ == '__main__':

    import pandas as pd

    # Base Address
    inDir = os.getcwd()

    # train-images multiploygon co-ordinates
    df = pd.read_csv(os.path.join(inDir, 'Data/train_wkt_v4.csv'))
    print(df.head())

    # Distinct imageIds in the DataFrame
    trainImageIds = []

    for i in range(len(df)):
        string = (df.iloc[i, 0])

        if string not in trainImageIds:
            trainImageIds.append(string)

    print("The are " + str(len(trainImageIds)) + " distinct ids." +
          str(df.head()))
    trainImageIds.sort()

    creator = gdal_utils()
    for imageId in trainImageIds:
        new_img = give_image_sandwhich(imageId)
        creator.create_tiff_file_from_array(
            "./Data/sixteen_band/" + imageId + "_P.tif",
            "./Data/image_stacks/" + imageId + ".tif", new_img)