def save_capture_as_stack(self, outfilename): from osgeo.gdal import GetDriverByName, GDT_UInt16 if self.__aligned_capture is None: raise RuntimeError("call Capture.create_aligned_capture prior to saving as stack") rows, cols, bands = self.__aligned_capture.shape driver = GetDriverByName('GTiff') outRaster = driver.Create(outfilename, cols, rows, bands, GDT_UInt16, options = [ 'INTERLEAVE=BAND','COMPRESS=None','PHOTOMETRIC=RGB' ]) if outRaster is None: raise IOError("could not load gdal GeoTiff driver") for i in range(0,5): outband = outRaster.GetRasterBand(i+1) outdata = self.__aligned_capture[:,:,i] outdata[outdata<0] = 0 outdata[outdata>2] = 2 #limit reflectance data to 200% to allow some specular reflections outband.WriteArray(outdata*32768) # scale reflectance images so 100% = 32768 outband.FlushCache() if bands == 6: outband = outRaster.GetRasterBand(6) outdata = (self.__aligned_capture[:,:,5]+273.15) * 100 # scale data from float degC to back to centi-Kelvin to fit into uint16 outdata[outdata<0] = 0 outdata[outdata>65535] = 65535 outband.WriteArray(outdata) outband.FlushCache() outRaster = None
def save_capture_as_stack(self, outfilename, sort_by_wavelength=False, photometric='MINISBLACK'): from osgeo.gdal import GetDriverByName, GDT_UInt16 if self.__aligned_capture is None: raise RuntimeError( "call Capture.create_aligned_capture prior to saving as stack") rows, cols, bands = self.__aligned_capture.shape driver = GetDriverByName('GTiff') outRaster = driver.Create(outfilename, cols, rows, bands, GDT_UInt16, options=[ 'INTERLEAVE=BAND', 'COMPRESS=DEFLATE', f'PHOTOMETRIC={photometric}' ]) try: if outRaster is None: raise IOError("could not load gdal GeoTiff driver") if sort_by_wavelength: eo_list = list( np.argsort( np.array( self.center_wavelengths())[self.eo_indices()])) else: eo_list = self.eo_indices() for outband, inband in enumerate(eo_list): outband = outRaster.GetRasterBand(outband + 1) outdata = self.__aligned_capture[:, :, inband] outdata[outdata < 0] = 0 outdata[ outdata > 2] = 2 #limit reflectance data to 200% to allow some specular reflections outband.WriteArray( outdata * 32768) # scale reflectance images so 100% = 32768 outband.FlushCache() for outband, inband in enumerate(self.lw_indices()): outband = outRaster.GetRasterBand(len(eo_list) + outband + 1) outdata = ( self.__aligned_capture[:, :, inband] + 273.15 ) * 100 # scale data from float degC to back to centi-Kelvin to fit into uint16 outdata[outdata < 0] = 0 outdata[outdata > 65535] = 65535 outband.WriteArray(outdata) outband.FlushCache() finally: outRaster = None
def _export_tif(self, file_path: Path) -> None: LOG.debug( "Writing (%ix%i) array to '%s' ...", self.h.shape[0], self.h.shape[1], file_path, ) driver = GetDriverByName("GTiff") data_set = driver.Create(str(file_path), self.h.shape[1], self.h.shape[0], 3, GDT_Float32) data_set.GetRasterBand(1).WriteArray(self.store.mean("h")) data_set.GetRasterBand(2).WriteArray(self.store.mean("u")) data_set.GetRasterBand(3).WriteArray(self.store.diff("h")) data_set.SetGeoTransform(self._geo_trans) data_set.SetProjection(self._geo_proj) data_set.FlushCache()
def save_capture_as_reflectance_stack(self, outfilename, irradiance_list=None, warp_matrices=None, normalize=False): from osgeo.gdal import GetDriverByName, GDT_UInt16 self.compute_reflectance(irradiance_list) if warp_matrices is None: warp_matrices = self.get_warp_matrices() cropped_dimensions, edges = imageutils.find_crop_bounds( self, warp_matrices) im_aligned = imageutils.aligned_capture(self, warp_matrices, cv2.MOTION_HOMOGRAPHY, cropped_dimensions, None, img_type="reflectance") rows, cols, bands = im_aligned.shape driver = GetDriverByName('GTiff') outRaster = driver.Create(outfilename, cols, rows, bands, GDT_UInt16) if outRaster is None: raise IOError("could not load gdal GeoTiff driver") for i in range(0, 5): outband = outRaster.GetRasterBand(i + 1) outdata = im_aligned[:, :, i] outdata[outdata < 0] = 0 outdata[outdata > 1] = 1 outband.WriteArray(outdata * 32768) outband.FlushCache() if bands == 6: outband = outRaster.GetRasterBand(6) outdata = ( im_aligned[:, :, 5] + 273.15 ) * 100 # scale data from float degC to back to centi-Kelvin to fit into uint16 outdata[outdata < 0] = 0 outdata[outdata > 65535] = 65535 outband.WriteArray(outdata) outband.FlushCache() outRaster = None
def save_capture_as_stack(self, outfilename, generateIndividualBands=True): from osgeo.gdal import GetDriverByName, GDT_UInt16, GDT_Float64 if self.__aligned_capture is None: raise RuntimeError( "call Capture.create_aligned_capture prior to saving as stack") rows, cols, bands = self.__aligned_capture.shape driver = GetDriverByName('GTiff') # outRaster = driver.Create(outfilename, cols, rows, bands, GDT_UInt16, options = [ 'INTERLEAVE=BAND','COMPRESS=DEFLATE' ]) outRaster = driver.Create( outfilename, cols, rows, bands, GDT_Float64, options=['INTERLEAVE=BAND', 'COMPRESS=DEFLATE']) if outRaster is None: raise IOError("could not load gdal GeoTiff driver") for i in range(0, 5): outband = outRaster.GetRasterBand(i + 1) if i == 3: # replace NIR and RedEdge outdata = self.__aligned_capture[:, :, 4] elif i == 4: outdata = self.__aligned_capture[:, :, 3] else: outdata = self.__aligned_capture[:, :, i] outdata[outdata < 0] = 0 # outdata[outdata>2] = 2 #limit reflectance data to 200% to allow some specular reflections # outband.WriteArray(outdata*32768) # scale reflectance images so 100% = 32768 outdata[ outdata > 1] = 1 #limit reflectance data to 200% to allow some specular reflections outband.WriteArray( outdata) # scale reflectance images so 100% = 32768 outband.FlushCache() #------------------------------------------------------------------ if generateIndividualBands: head, tail = os.path.split(outfilename) path_for_bands = os.path.join(head, '..', 'individual_bands') if not os.path.exists(path_for_bands): os.makedirs(path_for_bands) name_no_suffix = tail[0:-4] path_to_save = path_for_bands + '\\' + name_no_suffix + '_' + str( i + 1) + '.tif' # band_im = self.images[i] # band_ref = band_im.reflectance() # band_ref[band_ref<0] = 0 # band_ref[band_ref>1] = 1 imageio.imwrite(path_to_save, (outdata).astype('float32')) #------------------------------------------------------------------ if bands == 6: outband = outRaster.GetRasterBand(6) outdata = ( self.__aligned_capture[:, :, 5] + 273.15 ) * 100 # scale data from float degC to back to centi-Kelvin to fit into uint16 outdata[outdata < 0] = 0 outdata[outdata > 65535] = 65535 outband.WriteArray(outdata) outband.FlushCache() outRaster = None
def save_capture_as_stack_gtif(self, outfilename, flight_alt, generateIndividualBands=True): from osgeo.gdal import GetDriverByName, GDT_Float64 import osr if self.__aligned_capture is None: raise RuntimeError( "call Capture.create_aligned_capture prior to saving as stack") rows, cols, bands = self.__aligned_capture.shape driver = GetDriverByName('GTiff') # outRaster = driver.Create(outfilename, cols, rows, bands, GDT_UInt16, options = [ 'INTERLEAVE=BAND','COMPRESS=DEFLATE' ]) outRaster = driver.Create( outfilename, cols, rows, bands, GDT_Float64, options=['INTERLEAVE=BAND', 'COMPRESS=DEFLATE']) lat, long, _ = self.location() fov_half = self.fov() / 2 imWidth, imHeight = self.im_size() spatial_res = np.tan( fov_half * math.pi / 180) * flight_alt * 2 / imWidth x_center_pixel, y_center_pixel, zone_number, zone_letter = latlong_to_utm( lat, long, force_zone_number=None, force_zone_letter=None) # easting, northing, zone_number, zone_letter x_tl_pixel = x_center_pixel - (spatial_res * imWidth / 2) y_tl_pixel = y_center_pixel + (spatial_res * imHeight / 2) outRaster.SetGeoTransform( (x_tl_pixel, spatial_res, 0, y_tl_pixel, 0, -1 * spatial_res)) outRasterSRS = osr.SpatialReference() # outRasterSRS.ImportFromEPSG(4326) # WGS 84 for the entire world but probably not projected? outRasterSRS.ImportFromEPSG(32610) outRaster.SetProjection(outRasterSRS.ExportToWkt()) # outRaster.SetMetadata( if outRaster is None: raise IOError("could not load gdal GeoTiff driver") for i in range(0, 5): outband = outRaster.GetRasterBand(i + 1) outdata = self.__aligned_capture[:, :, i] outdata[outdata < 0] = 0 # outdata[outdata>2] = 2 #limit reflectance data to 200% to allow some specular reflections # outband.WriteArray(outdata*32768) # scale reflectance images so 100% = 32768 outdata[ outdata > 1] = 1 #limit reflectance data to 100% (i.e. 1) to allow some specular reflections outband.WriteArray(outdata) outband.FlushCache() #------------------------------------------------------------------ if generateIndividualBands: head, tail = os.path.split(outfilename) path_for_bands = os.path.join(head, '..', 'individual_bands') if not os.path.exists(path_for_bands): os.makedirs(path_for_bands) name_no_suffix = tail[0:-4] path_to_save = path_for_bands + '\\' + name_no_suffix + '_' + str( i + 1) + '.tif' # band_im = self.images[i] # band_ref = band_im.reflectance() # band_ref[band_ref<0] = 0 # band_ref[band_ref>1] = 1 imageio.imwrite(path_to_save, (outdata).astype('float32')) #------------------------------------------------------------------ if bands == 6: outband = outRaster.GetRasterBand(6) outdata = ( self.__aligned_capture[:, :, 5] + 273.15 ) * 100 # scale data from float degC to back to centi-Kelvin to fit into uint16 outdata[outdata < 0] = 0 outdata[outdata > 65535] = 65535 outband.WriteArray(outdata) outband.FlushCache() outRaster = None