Commit fa07ab7f authored by Marina Bertolino's avatar Marina Bertolino

ENH: add outmeans parameter in KMeansClassification

parent 17996b76
......@@ -138,7 +138,10 @@ private:
// Compute a classification of the input image according to a model file
KMeansClassif();
// remove all tempory files
// Create the output text file containing centroids positions
CreateOutMeansFile(GetParameterImage("in"), fileNames.modelFile, GetParameterInt("nc"));
// Remove all tempory files
if( IsParameterEnabled( "cleanup" ) )
{
otbAppLogINFO( <<"Final clean-up ..." );
......
......@@ -21,12 +21,9 @@
#ifndef otbClassKMeansBase_h
#define otbClassKMeansBase_h
#include "otbVectorDataFileWriter.h"
#include "otbWrapperCompositeApplication.h"
#include "otbWrapperApplicationFactory.h"
#include "otbStatisticsXMLFileWriter.h"
#include "otbImageToEnvelopeVectorDataFilter.h"
#include "otbOGRDataToSamplePositionFilter.h"
namespace otb
......@@ -42,6 +39,7 @@ namespace Wrapper
*
* KMeansClassification = ImageEnveloppe + PolygonClassStatistics +
* SampleSelection + SamplesExtraction +
* ComputeImagesStatistics +
* TrainVectorClassifier + ImageClassifier.
*
* \ingroup OTBAppClassification
......@@ -120,14 +118,26 @@ public:
* \param imageFileName input image filename
* \param imagesStatsFileName image statistics
*/
void ComputeImageStatistics(std::string imageFileName,
std::string imagesStatsFileName);
void ComputeImageStatistics(std::string imageFileName,
std::string imagesStatsFileName);
/**
* Performs a classification of the input image according to a model file
*/
void KMeansClassif();
/**
* Write in the output file the centroids positions
* class1Dim1 .. class1DimN
* ... ...
* classMDim1 .. classMDimN
* \param image input image
* \param modelFileName model filename
* \param nbClasses number of class
*/
void CreateOutMeansFile(FloatVectorImageType *image, std::string modelFileName,
unsigned int nbClasses);
/**
* \class KMeansFileNamesHandler
* This class is used to store file names requires for the application's input and output.
......
......@@ -61,6 +61,10 @@ void ClassKMeansBase::InitKMSampling()
SetDefaultParameterInt("maxit", 1000);
MandatoryOff("maxit");
AddParameter(ParameterType_OutputFilename, "outmeans", "Centroid filename");
SetParameterDescription("outmeans", "Output text file containing centroid positions");
MandatoryOff("outmeans");
ShareKMSamplingParameters();
ConnectKMSamplingParams();
}
......@@ -257,6 +261,58 @@ void ClassKMeansBase::KMeansClassif()
ExecuteInternal( "classif" );
}
void ClassKMeansBase::CreateOutMeansFile(FloatVectorImageType *image,
std::string modelFileName,
unsigned int nbClasses)
{
if (IsParameterEnabled("outmeans"))
{
unsigned int nbBands = image->GetNumberOfComponentsPerPixel();
unsigned int nbElements = nbClasses * nbBands;
// get the line in model file that contains the centroids positions
std::ifstream infile(modelFileName.c_str());
if(infile)
{
// get the end line with the centroids
std::string line, centroidLine;
while(!infile.eof())
{
std::getline(infile,line);
if (!line.empty())
centroidLine = line;
}
std::vector<std::string> centroidElm;
boost::split(centroidElm,centroidLine,boost::is_any_of(" "));
// remove the first elements, not the centroids positions
int nbWord = centroidElm.size();
int beginCentroid = nbWord-nbElements;
centroidElm.erase(centroidElm.begin(), centroidElm.begin()+beginCentroid);
// write in the output file
std::ofstream outfile;
outfile.open(GetParameterString("outmeans").c_str());
for (unsigned int i = 0; i < nbClasses; i++)
{
for (unsigned int j = 0; j < nbBands; j++)
{
outfile << std::setw(8) << centroidElm[i * nbBands + j] << " ";
}
outfile << std::endl;
}
outfile.close();
}
else
{
itkExceptionMacro(<< "File : " << modelFileName << " couldn't be opened");
}
infile.close();
}
}
}
}
......
......@@ -640,6 +640,7 @@ if(OTB_USE_SHARK)
-sampler periodic
-rand 121212
-nodatalabel 255
-outmeans ${TEMP}/apTvClKMeansImageClassificationFilterOutMeans.txt
-out ${TEMP}/apTvClKMeansImageClassificationFilterOutput.tif uint8
-cleanup 0
VALID --compare-image ${NOTOL}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment