def compute_samples_stats(global_parameters, proceed=True): ''' 1. Compute the samples stats ''' print " Polygon Classes Statistics" main_dir = global_parameters["user_choices"]["main_dir"] raw_img = op.join(main_dir, 'In_data', 'Image', global_parameters["user_choices"]["raw_img"]) class_stats = op.join(main_dir, 'Statistics', global_parameters["general"]["class_stats"]) training_shp = op.join( main_dir, 'Intermediate', global_parameters["general"]["training_shp_extended"]) no_data_shp = op.join(main_dir, 'In_data', 'Masks', global_parameters["general"]["no_data_mask"]) no_data_mask = no_data_shp[0:-4] + '.tif' PolygonClassStatistics = otbApplication.Registry_CreateApplication( "PolygonClassStatistics") PolygonClassStatistics.SetParameterString("in", str(raw_img)) PolygonClassStatistics.SetParameterString("vec", str(training_shp)) PolygonClassStatistics.SetParameterString("out", str(class_stats)) PolygonClassStatistics.SetParameterString("mask", str(no_data_mask)) PolygonClassStatistics.UpdateParameters() PolygonClassStatistics.SetParameterStringList("field", ["class"]) PolygonClassStatistics.ExecuteAndWriteOutput() print('Done') return class_stats
def extract_samples(global_parameters, proceed=True): ''' 3. Extract the samples ''' main_dir = global_parameters["user_choices"]["main_dir"] raw_img = op.join(main_dir, 'In_data', 'Image', global_parameters["user_choices"]["raw_img"]) training_samples_extracted = op.join( main_dir, 'Samples', global_parameters["general"]["training_samples_extracted"]) training_samples_location = op.join( main_dir, 'Samples', global_parameters["general"]["training_samples_location"]) print(" Training Samples Extraction") SampleExtraction = otbApplication.Registry_CreateApplication( "SampleExtraction") SampleExtraction.SetParameterString("in", str(raw_img)) SampleExtraction.SetParameterString("vec", str(training_samples_location)) SampleExtraction.SetParameterString("outfield", "prefix") SampleExtraction.SetParameterString("outfield.prefix.name", "band_") SampleExtraction.SetParameterString("out", str(training_samples_extracted)) SampleExtraction.UpdateParameters() SampleExtraction.SetParameterStringList("field", ["class"]) SampleExtraction.ExecuteAndWriteOutput() print('Done') return training_samples_extracted
def select_samples(global_parameters, strategy="smallest", proceed=True): ''' 2. Select the samples ''' main_dir = global_parameters["user_choices"]["main_dir"] raw_img = op.join(main_dir, 'In_data', 'Image', global_parameters["user_choices"]["raw_img"]) training_samples_location = op.join( main_dir, 'Samples', global_parameters["general"]["training_samples_location"]) class_stats = op.join(main_dir, 'Statistics', global_parameters["general"]["class_stats"]) training_shp = op.join( main_dir, 'Intermediate', global_parameters["general"]["training_shp_extended"]) rates = op.join(main_dir, 'Statistics', 'rates.csv') no_data_shp = op.join(main_dir, 'In_data', 'Masks', global_parameters["general"]["no_data_mask"]) no_data_mask = no_data_shp[0:-4] + '.tif' print(" Training Samples Selection") SampleSelection = otbApplication.Registry_CreateApplication( "SampleSelection") SampleSelection.SetParameterString("in", str(raw_img)) SampleSelection.SetParameterString("vec", str(training_shp)) SampleSelection.SetParameterString("mask", str(no_data_mask)) SampleSelection.SetParameterString("instats", str(class_stats)) SampleSelection.SetParameterString("out", str(training_samples_location)) SampleSelection.SetParameterString("outrates", str(rates)) SampleSelection.SetParameterString("sampler", "random") # default is periodic if strategy == "smallest": SampleSelection.SetParameterString("strategy", "smallest") elif strategy == "constant": SampleSelection.SetParameterString("strategy", "constant") minSamplesNb = min(get_samples_nb(class_stats)) SampleSelection.SetParameterString("strategy.constant.nb", str(minSamplesNb)) elif strategy.split('_')[0] == "constant": SampleSelection.SetParameterString("strategy", "constant") SampleSelection.SetParameterString("strategy.constant.nb", str(strategy.split('_')[1])) SampleSelection.UpdateParameters() SampleSelection.SetParameterStringList("field", ["class"]) SampleSelection.Execute() print('Done') return training_samples_location
# -*- coding: utf-8 -*- # # Example on the use of the outxml via BandMath # from sys import argv import otbApplication as otb imagelist = argv[1:-2] exp = "cos(im1b1)+im2b1*im3b1-im3b2+ndvi(im3b3, im3b4)" out = argv[-2] outxml = argv[-1] app = otb.Registry_CreateApplication('BandMath') app.SetParameterStringList("il", imagelist, True) app.SetParameterString("out", out, True) app.SetParameterString("exp", exp, True) app.SetParameterString("outxml", outxml, True) app.UpdateParameters() #for paramKey in app.GetParametersKeys(): #app.GetParameterValue(paramKey) #print app.HasUserValue(paramKey) app.ExecuteAndWriteOutput()
import otbApplication app = otbApplication.Registry_CreateApplication('TrainImagesClassifier') app.SetParameterFloat('classifier.libsvm.opt', True) print app.GetParameterValue('classifier.libsvm.opt')