def generate(image_paths,
             output_path,
             method='mean_with_uniform_weight',
             image_nodata=None):
    '''Synthesizes a time stack image set into a single reference image.

    All images in time stack must:
        - contain the same number of bands
        - have the same band order
        - have the same size
        - have the same nodata value
        - have the same geographic metadata

    The output file is only supposed to be used internally so options to change
    the nodata value and the datatype are not exposed.
    - The output image data type is uint16
    - The output nodata values are indicated by a 0 in the alpha band

    Input:
        image_paths (list of str): A list of paths for input time stack images
        output_path (str): A path to write the file to
        method (str): Time stack analysis method [Identity]
        image_nodata (int): [Optional] Manually provide a no data value
    '''

    output_datatype = numpy.uint16

    if method == 'mean_with_uniform_weight':
        output_gimage = mean_with_uniform_weight(image_paths, output_datatype,
                                                 image_nodata)
    else:
        raise NotImplementedError("Only 'mean_with_uniform_weight'"
                                  "method is implemented")

    gimage.save(output_gimage, output_path, compress=False)
def generate(image_paths, output_path,
             method='mean_with_uniform_weight',
             image_nodata=None):
    '''Synthesizes a time stack image set into a single reference image.

    All images in time stack must:
        - contain the same number of bands
        - have the same band order
        - have the same size
        - have the same nodata value
        - have the same geographic metadata

    The output file is only supposed to be used internally so options to change
    the nodata value and the datatype are not exposed.
    - The output image data type is uint16
    - The output nodata values are indicated by a 0 in the alpha band

    Input:
        image_paths (list of str): A list of paths for input time stack images
        output_path (str): A path to write the file to
        method (str): Time stack analysis method [Identity]
        image_nodata (int): [Optional] Manually provide a no data value
    '''

    output_datatype = numpy.uint16

    if method == 'mean_with_uniform_weight':
        output_gimage = mean_with_uniform_weight(
            image_paths, output_datatype, image_nodata)
    else:
        raise NotImplementedError("Only 'mean_with_uniform_weight'"
                                  "method is implemented")

    gimage.save(output_gimage, output_path, compress=False)
def apply_transformations(input_path, transformations, output_path):
    ''' This wrapper function applies the transformations
    derived by the library onto an input file.
    '''

    gimg = gimage.load(input_path)
    out_gimg = transformation.apply(gimg, transformations)
    gimage.save(out_gimg, output_path)
    def test_save_with_compress(self):
        output_file = 'test_save_with_compress.tif'
        test_band = numpy.array([[5, 2, 2], [1, 6, 8]], dtype=numpy.uint16)
        test_alpha = numpy.array([[0, 0, 0], [1, 1, 1]], dtype=numpy.bool)
        test_gimage = gimage.GImage([test_band, test_band, test_band],
                                    test_alpha, self.metadata)
        gimage.save(test_gimage, output_file, compress=True)

        result_gimg = gimage.load(output_file)
        numpy.testing.assert_array_equal(result_gimg.bands[0], test_band)
        numpy.testing.assert_array_equal(result_gimg.bands[1], test_band)
        numpy.testing.assert_array_equal(result_gimg.bands[2], test_band)
        numpy.testing.assert_array_equal(result_gimg.alpha, test_alpha)
        self.assertEqual(result_gimg.metadata, self.metadata)

        os.unlink(output_file)
    def test_save_with_compress(self):
        output_file = 'test_save_with_compress.tif'
        test_band = numpy.array([[5, 2, 2], [1, 6, 8]], dtype=numpy.uint16)
        test_alpha = numpy.array([[0, 0, 0], [65535, 65535, 65535]],
                                 dtype=numpy.uint16)
        test_gimage = gimage.GImage([test_band, test_band, test_band],
                                    test_alpha, self.metadata)
        gimage.save(test_gimage, output_file, compress=True)

        result_gimg = gimage.load(output_file)
        numpy.testing.assert_array_equal(result_gimg.bands[0], test_band)
        numpy.testing.assert_array_equal(result_gimg.bands[1], test_band)
        numpy.testing.assert_array_equal(result_gimg.bands[2], test_band)
        numpy.testing.assert_array_equal(result_gimg.alpha, test_alpha)
        self.assertEqual(result_gimg.metadata, self.metadata)

        os.unlink(output_file)
    def test_mean_with_uniform_weight(self):
        # Three images of three bands of 2 by 2 pixels
        gimage_one = gimage.GImage(
            [numpy.array([[4, 1],
                          [2, 5]], dtype='uint16'),
             numpy.array([[4, 1],
                          [2, 5]], dtype='uint16'),
             numpy.array([[7, 8],
                          [6, 3]], dtype='uint16')],
            numpy.array([[65535, 0], [65535, 65535]], dtype='uint16'), {})

        gimage_two = gimage.GImage(
            [numpy.array([[9, 9],
                          [5, 1]], dtype='uint16'),
             numpy.array([[2, 7],
                          [7, 3]], dtype='uint16'),
             numpy.array([[2, 6],
                          [7, 2]], dtype='uint16')],
            numpy.array([[0, 0], [65535, 65535]], dtype='uint16'), {})

        gimage_three = gimage.GImage(
            [numpy.array([[4, 7],
                          [5, 3]], dtype='uint16'),
             numpy.array([[1, 2],
                          [5, 1]], dtype='uint16'),
             numpy.array([[1, 6],
                          [3, 2]], dtype='uint16')],
            numpy.array([[65535, 0], [65535, 0]], dtype='uint16'), {})

        gimage.save(gimage_one, 'gimage_one.tif')
        gimage.save(gimage_two, 'gimage_two.tif')
        gimage.save(gimage_three, 'gimage_three.tif')
        image_paths = ['gimage_one.tif', 'gimage_two.tif', 'gimage_three.tif']

        golden_output = gimage.GImage(
            [numpy.array([[4, 0],
                          [4, 3]], dtype='uint16'),
             numpy.array([[2, 0],
                          [4, 4]], dtype='uint16'),
             numpy.array([[4, 0],
                          [5, 2]], dtype='uint16')],
            numpy.array([[65535, 0],
                         [65535, 65535]], dtype='uint16'),
            {'geotransform': (0.0, 1.0, 0.0, 0.0, 0.0, 1.0)})

        output_gimage = time_stack.mean_with_uniform_weight(image_paths,
                                                            numpy.uint16,
                                                            None)

        for band in range(3):
            numpy.testing.assert_array_equal(output_gimage.bands[band],
                                             golden_output.bands[band])
        numpy.testing.assert_array_equal(output_gimage.alpha,
                                         golden_output.alpha)
        self.assertEqual(output_gimage.metadata,
                         golden_output.metadata)

        for image in image_paths:
            os.unlink(image)
Пример #7
0
    def test_mean_with_uniform_weight(self):
        # Three images of three bands of 2 by 2 pixels
        gimage_one = gimage.GImage(
            [numpy.array([[4, 1],
                          [2, 5]], dtype='uint16'),
             numpy.array([[4, 1],
                          [2, 5]], dtype='uint16'),
             numpy.array([[7, 8],
                          [6, 3]], dtype='uint16')],
            numpy.array([[65535, 0], [65535, 65535]], dtype='uint16'), {})

        gimage_two = gimage.GImage(
            [numpy.array([[9, 9],
                          [5, 1]], dtype='uint16'),
             numpy.array([[2, 7],
                          [7, 3]], dtype='uint16'),
             numpy.array([[2, 6],
                          [7, 2]], dtype='uint16')],
            numpy.array([[0, 0], [65535, 65535]], dtype='uint16'), {})

        gimage_three = gimage.GImage(
            [numpy.array([[4, 7],
                          [5, 3]], dtype='uint16'),
             numpy.array([[1, 2],
                          [5, 1]], dtype='uint16'),
             numpy.array([[1, 6],
                          [3, 2]], dtype='uint16')],
            numpy.array([[65535, 0], [65535, 0]], dtype='uint16'), {})

        gimage.save(gimage_one, 'gimage_one.tif')
        gimage.save(gimage_two, 'gimage_two.tif')
        gimage.save(gimage_three, 'gimage_three.tif')
        image_paths = ['gimage_one.tif', 'gimage_two.tif', 'gimage_three.tif']

        golden_output = gimage.GImage(
            [numpy.array([[4, 0],
                          [4, 3]], dtype='uint16'),
             numpy.array([[2, 0],
                          [4, 4]], dtype='uint16'),
             numpy.array([[4, 0],
                          [5, 2]], dtype='uint16')],
            numpy.array([[65535, 0],
                         [65535, 65535]], dtype='uint16'),
            {'geotransform': (0.0, 1.0, 0.0, 0.0, 0.0, 1.0)})

        output_gimage = time_stack.mean_with_uniform_weight(image_paths,
                                                            numpy.uint16,
                                                            None)

        for band in range(3):
            numpy.testing.assert_array_equal(output_gimage.bands[band],
                                             golden_output.bands[band])
        numpy.testing.assert_array_equal(output_gimage.alpha,
                                         golden_output.alpha)
        self.assertEqual(output_gimage.metadata,
                         golden_output.metadata)

        for image in image_paths:
            os.unlink(image)
Пример #8
0
from radiometric_normalization.wrappers import normalize_wrapper
from radiometric_normalization import gimage
from radiometric_normalization import pif

## OPTIONAL

import logging
import numpy
import subprocess
from osgeo import gdal
from radiometric_normalization.wrappers import display_wrapper
import os

logging.basicConfig(level=logging.DEBUG)
##
"""
## OPTIONAL - Cut dataset to colocated sub scenes and create and BGRN image
# LC08_L1TP_044034_20170105_20170218_01_T1 is the older scene and so it is set as the reference.
# Need to check the band mapping for the Sentinel 2 (S2A ?S2B)
band_mapping = [{'name': 'blue', 'S2': 'B2'}, {'name': 'green', 'S2':'B3'}, {'name': 'red', 'S2': 'B4'}, {'name': 'nir', 'S2': 'B8'}]
#get the list of band name for sentinel-2 from 

  Band_1=Blue
  Band_10=SWIR2
  Band_2=Green
  Band_3=Red
  Band_4=RedEdge705
  Band_5=RedEdge740
  Band_6=RedEdge783
  Band_7=NIR
  Band_8=NarrowNIR
Пример #9
0
  Band_5=RedEdge740
  Band_6=RedEdge783
  Band_7=NIR
  Band_8=NarrowNIR
  Band_9=SWIR1
"""
WorkSpace = os.chdir(r'C:\\DEV\\FuelMapping\\registration\\Multiband_Images')
#full_candidate_basename = 'LC08_L1TP_044034_20170427_20170428_01_RT'
full_candidate_name = 'S2A_MSIL2A_T14RPV_N0205_20170430_10m.tif'
#Analyze the file name for image to process
# e.g. S2B_MSIL2A_T14RMT_N0205_20171022_10m
# e.g. S2A_MSIL2A_T14RMT_N0205_20170430_10m

#full_reference_basename = 'LC08_L1TP_044034_20170105_20170218_01_T1'
full_reference_name = 'S2A_MSIL2A_T14RPV_N0205_20170507_10m'
"""
candidate_basename = 'candidate'
reference_basename = 'reference'
full_candidate_filenames = ['{}_{}.TIF'.format(full_candidate_basename, b['S2']) for b in band_mapping]
print full_candidate_filenames
candidate_filenames = ['{}_{}.TIF'.format(candidate_basename, b['name']) for b in band_mapping]
print candidate_filenames

full_reference_filenames = ['{}_{}.TIF'.format(full_reference_basename, b['S2']) for b in band_mapping]
reference_filenames = ['{}_{}.TIF'.format(reference_basename, b['name']) for b in band_mapping]

for full_filename, cropped_filename in zip(full_candidate_filenames, candidate_filenames):
    print full_filename,cropped_filename
    #subprocess.check_call(['gdal_translate', '-projwin', '545000', '4136000', '601000', '4084000', full_filename, cropped_filename])
    #subprocess.check_call(["c:\Program Files\QGIS 2.18\bin\gdal_translate.exe", '-projwin', '600000', '3500040', '709800', '3390240', full_filename, cropped_filename])
Пример #10
0
        'gdal_translate', '-projwin', '545000', '4136000', '601000', '4084000',
        full_filename, cropped_filename
    ])

band_gimgs = {}
for cropped_filename in candidate_filenames:
    band = cropped_filename.split('_')[1].split('.TIF')[0]
    band_gimgs[band] = gimage.load(cropped_filename)

candidate_path = 'D:\TOULOUSE2\MONTUS\python_module\candidate.tif'
combined_alpha = numpy.logical_and.reduce(
    [b.alpha for b in band_gimgs.values()])
temporary_gimg = gimage.GImage(
    [band_gimgs[b].bands[0] for b in ['blue', 'green', 'red', 'nir']],
    combined_alpha, band_gimgs['blue'].metadata)
gimage.save(temporary_gimg, candidate_path)

band_gimgs = {}
for cropped_filename in reference_filenames:
    band = cropped_filename.split('_')[1].split('.TIF')[0]
    band_gimgs[band] = gimage.load(cropped_filename)

reference_path = 'D:\TOULOUSE2\MONTUS\python_module\reference.tif'
combined_alpha = numpy.logical_and.reduce(
    [b.alpha for b in band_gimgs.values()])
temporary_gimg = gimage.GImage(
    [band_gimgs[b].bands[0] for b in ['blue', 'green', 'red', 'nir']],
    combined_alpha, band_gimgs['blue'].metadata)
gimage.save(temporary_gimg, reference_path)
##