1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
/*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#include <iterator>
#include "otbImage.h"
#include "otbRadiometricMomentsImageFunction.h"
#include "otbDescriptorsListSampleGenerator.h"
#include "otbImageFileReader.h"
#include "otbVectorDataFileReader.h"
#include "otbImageFunctionAdaptor.h"
#include "otbStatisticsXMLFileReader.h"
#include "otbShiftScaleSampleListFilter.h"
#include "otbSVMSampleListModelEstimator.h"
const unsigned int Dimension = 2;
typedef int LabelType;
typedef double PixelType;
typedef double FunctionPrecisionType;
typedef double CoordRepType;
typedef otb::Image<PixelType, Dimension> ImageType;
typedef otb::VectorData<> VectorDataType;
typedef otb::RadiometricMomentsImageFunction<ImageType, CoordRepType> FunctionType;
typedef otb::ImageFunctionAdaptor<FunctionType, FunctionPrecisionType> AdaptedFunctionType;
typedef itk::VariableLengthVector<FunctionPrecisionType> SampleType;
typedef itk::Statistics::ListSample<SampleType> ListSampleType;
typedef itk::FixedArray<LabelType, 1> LabelSampleType;
typedef itk::Statistics::ListSample<LabelSampleType> LabelListSampleType;
typedef otb::DescriptorsListSampleGenerator
< ImageType,
VectorDataType,
ListSampleType,
LabelType,
FunctionPrecisionType,
CoordRepType > DescriptorsListSampleGeneratorType;
typedef otb::ImageFileReader<ImageType> ImageReaderType;
typedef otb::VectorDataFileReader<VectorDataType> VectorDataReaderType;
typedef otb::Functor::VariableLengthVectorToMeasurementVectorFunctor<SampleType>
MeasurementVectorFunctorType;
typedef otb::StatisticsXMLFileReader<SampleType> StatisticsReader;
typedef otb::Statistics::ShiftScaleSampleListFilter<ListSampleType> ShiftScaleListSampleFilterType;
typedef otb::SVMSampleListModelEstimator<
ListSampleType,
LabelListSampleType,
MeasurementVectorFunctorType> SVMEstimatorType;
typedef FunctionType::PointType PointType;
typedef DescriptorsListSampleGeneratorType::SamplesPositionType SamplesPositionType;
struct SampleEntry
{
PointType position;
LabelType label;
SampleType measurement;
};
struct CompareSampleEntry
{
bool operator () (SampleEntry p, SampleEntry q)
{
// order with the y axis position
if (p.position[1] < q.position[1])
return true;
if (p.position[1] > q.position[1])
return false;
// If one the same line,
// order with the x axis position
if (p.position[0] < q.position[0])
return true;
return false;
}
};
std::ostream &operator<<(std::ostream &stream, SampleEntry entry)
{
stream << "---" << std::endl
<< "Label : " << entry.label << std::endl
<< "Position : " << entry.position << std::endl
<< "Measurements : " << entry.measurement;
return stream;
}
int otbDescriptorsListSampleGeneratorNew(int itkNotUsed(argc), char* itkNotUsed(argv)[])
{
// instantiation
DescriptorsListSampleGeneratorType::Pointer generator = DescriptorsListSampleGeneratorType::New();
std::cout << generator << std::endl;
return EXIT_SUCCESS;
}
int otbDescriptorsListSampleGenerator(int argc, char* argv[])
{
if (argc != 6)
{
std::cerr << "Wrong number of arguments" << std::endl;
return EXIT_FAILURE;
}
const char* inputImageFileName = argv[1];
const char* inputSamplesLocation = argv[2];
const char* outputFileName = argv[3];
int streaming = atoi(argv[4]);
int neighborhood = atoi(argv[5]);
ImageReaderType::Pointer imageReader = ImageReaderType::New();
imageReader->SetFileName(inputImageFileName);
VectorDataReaderType::Pointer vectorDataReader = VectorDataReaderType::New();
vectorDataReader->SetFileName(inputSamplesLocation);
//imageReader->Update();
//vectorDataReader->Update();
AdaptedFunctionType::Pointer descriptorsFunction = AdaptedFunctionType::New();
descriptorsFunction->SetInputImage(imageReader->GetOutput());
descriptorsFunction->GetInternalImageFunction()->SetNeighborhoodRadius(5);
DescriptorsListSampleGeneratorType::Pointer descriptorsGenerator = DescriptorsListSampleGeneratorType::New();
descriptorsGenerator->SetInputImage(imageReader->GetOutput());
descriptorsGenerator->SetSamplesLocations(vectorDataReader->GetOutput());
descriptorsGenerator->SetDescriptorsFunction(descriptorsFunction.GetPointer());
descriptorsGenerator->SetNeighborhoodRadius(neighborhood);
descriptorsGenerator->GetStreamer()->SetNumberOfLinesStrippedStreaming( streaming );
descriptorsGenerator->Update();
ListSampleType::Pointer samples = descriptorsGenerator->GetListSample();
LabelListSampleType::Pointer labels = descriptorsGenerator->GetLabelListSample();
SamplesPositionType& positions = descriptorsGenerator->GetSamplesPositions();
ListSampleType::Iterator sampleIt = samples->Begin();
LabelListSampleType::Iterator labelIt = labels->Begin();
SamplesPositionType::const_iterator posIt = positions.begin();
ListSampleType::Iterator sampleEnd = samples->End();
LabelListSampleType::Iterator labelEnd = labels->End();
SamplesPositionType::const_iterator posEnd = positions.end();
std::vector<SampleEntry> entries;
while (sampleIt != sampleEnd && labelIt != labelEnd && posIt != posEnd)
{
SampleEntry entry;
entry.position = *posIt;
entry.label = labelIt.GetMeasurementVector()[0];
entry.measurement = sampleIt.GetMeasurementVector();
entries.push_back(entry);
++sampleIt;
++labelIt;
++posIt;
}
std::sort(entries.begin(), entries.end(), CompareSampleEntry());
std::ofstream file(outputFileName);
std::copy(entries.begin(), entries.end(), std::ostream_iterator<SampleEntry>(file, "\n"));
file.close();
return EXIT_SUCCESS;
}
int otbDescriptorsSVMModelCreation(int argc, char* argv[])
{
if (argc != 7)
{
std::cerr << "Wrong number of arguments" << std::endl;
return EXIT_FAILURE;
}
const char* inputImageFileName = argv[1];
const char* inputSamplesLocation = argv[2];
const char* featureStatisticsFileName = argv[3];
const char* outputFileName = argv[4];
int streaming = atoi(argv[5]);
int neighborhood = atoi(argv[6]);
ImageReaderType::Pointer imageReader = ImageReaderType::New();
imageReader->SetFileName(inputImageFileName);
VectorDataReaderType::Pointer vectorDataReader = VectorDataReaderType::New();
vectorDataReader->SetFileName(inputSamplesLocation);
//imageReader->Update();
//vectorDataReader->Update();
AdaptedFunctionType::Pointer descriptorsFunction = AdaptedFunctionType::New();
descriptorsFunction->SetInputImage(imageReader->GetOutput());
descriptorsFunction->GetInternalImageFunction()->SetNeighborhoodRadius(neighborhood);
DescriptorsListSampleGeneratorType::Pointer descriptorsGenerator = DescriptorsListSampleGeneratorType::New();
descriptorsGenerator->SetInputImage(imageReader->GetOutput());
descriptorsGenerator->SetSamplesLocations(vectorDataReader->GetOutput());
descriptorsGenerator->SetDescriptorsFunction(descriptorsFunction.GetPointer());
descriptorsGenerator->SetNeighborhoodRadius(5);
descriptorsGenerator->GetStreamer()->SetNumberOfLinesStrippedStreaming( streaming );
descriptorsGenerator->Update();
// Normalize the samples
// Read the mean and variance form the XML file
StatisticsReader::Pointer statisticsReader = StatisticsReader::New();
statisticsReader->SetFileName(featureStatisticsFileName);
SampleType meanMeasurentVector = statisticsReader->GetStatisticVectorByName("mean");
SampleType varianceMeasurentVector = statisticsReader->GetStatisticVectorByName("stddev");
// Shift scale the samples
ShiftScaleListSampleFilterType::Pointer shiftscaleFilter = ShiftScaleListSampleFilterType::New();
shiftscaleFilter->SetInput(descriptorsGenerator->GetListSample());
shiftscaleFilter->SetShifts(meanMeasurentVector);
shiftscaleFilter->SetScales(varianceMeasurentVector);
shiftscaleFilter->Update();
SVMEstimatorType::Pointer svmEstimator = SVMEstimatorType::New();
svmEstimator->SetInputSampleList(shiftscaleFilter->GetOutput());
svmEstimator->SetTrainingSampleList(descriptorsGenerator->GetLabelListSample());
svmEstimator->Update();
svmEstimator->GetModel()->SaveModel(outputFileName);
return EXIT_SUCCESS;
}
|