Skip to content
Snippets Groups Projects
Commit 32e67e4d authored by Cyrille Valladeau's avatar Cyrille Valladeau
Browse files

SVMTaylor Classif + KMeans streame.

parent 56edf642
No related branches found
No related tags found
No related merge requests found
......@@ -238,8 +238,11 @@ GenericInterpolateImageFunction<TInputImage, TFunction, TBoundaryCondition, TCoo
/* double xWeight[ImageDimension][ twiceRadius];*/
std::vector< std::vector<double> > xWeight;
xWeight.resize(ImageDimension);
for(int cpt=0 ;cpt <xWeight.size() ; cpt++) xWeight[cpt].resize(twiceRadius);
for(unsigned int cpt=0; cpt < xWeight.size(); cpt++)
{
xWeight[cpt].resize(twiceRadius);
}
for( unsigned int dim = 0; dim < ImageDimension; dim++ )
{
// x is the offset, hence the parameter of the kernel
......
......@@ -62,89 +62,73 @@ public:
int otbSVMClassifierImage(int argc, char* argv[] )
{
try
namespace stat = itk::Statistics ;
if (argc != 4)
{
namespace stat = itk::Statistics ;
if (argc != 4)
{
std::cout << "Usage : " << argv[0] << " inputImage modelFile outputImage"
<< std::endl ;
return EXIT_FAILURE;
}
const char * imageFilename = argv[1];
const char * modelFilename = argv[2];
const char * outputFilename = argv[3];
/** Read the input image and build the sample */
typedef double InputPixelType;
typedef std::vector<InputPixelType> InputVectorType;
typedef int LabelPixelType;
const unsigned int Dimension = 2;
typedef otb::Image< itk::FixedArray<InputPixelType,3>, Dimension > InputImageType;
typedef otb::ImageFileReader< InputImageType > ReaderType;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName( imageFilename );
reader->Update();
std::cout << "Image read" << std::endl;
typedef itk::Statistics::ImageToListAdaptor< InputImageType > SampleType;
SampleType::Pointer sample = SampleType::New();
sample->SetImage(reader->GetOutput());
/** preparing classifier and decision rule object */
typedef otb::SVMModel< InputPixelType, LabelPixelType > ModelType;
ModelType::Pointer model = ModelType::New();
otb::Linear lFunctor;
model->SetKernelFunctor(&lFunctor);
model->LoadModel( modelFilename );
int numberOfClasses = model->GetNumberOfClasses();
}
const char * imageFilename = argv[1];
const char * modelFilename = argv[2];
const char * outputFilename = argv[3];
/** Read the input image and build the sample */
typedef double InputPixelType;
typedef std::vector<InputPixelType> InputVectorType;
typedef int LabelPixelType;
const unsigned int Dimension = 2;
typedef otb::Image< itk::FixedArray<InputPixelType,3>, Dimension > InputImageType;
typedef otb::ImageFileReader< InputImageType > ReaderType;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName( imageFilename );
reader->Update();
typedef itk::Statistics::ImageToListAdaptor< InputImageType > SampleType;
SampleType::Pointer sample = SampleType::New();
sample->SetImage(reader->GetOutput());
/** preparing classifier and decision rule object */
typedef otb::SVMModel< InputPixelType, LabelPixelType > ModelType;
ModelType::Pointer model = ModelType::New();
typedef otb::SVMClassifier< SampleType, LabelPixelType > ClassifierType ;
ClassifierType::Pointer classifier = ClassifierType::New() ;
otb::Linear lFunctor;
model->SetKernelFunctor(&lFunctor);
classifier->SetNumberOfClasses(numberOfClasses) ;
classifier->SetModel( model );
classifier->SetSample(sample.GetPointer()) ;
classifier->Update() ;
/* Build the class map */
std::cout << "Output image creation" << std::endl;
typedef ClassifierType::ClassLabelType OutputPixelType;
typedef otb::Image< OutputPixelType, Dimension > OutputImageType;
model->LoadModel( modelFilename );
int numberOfClasses = model->GetNumberOfClasses();
typedef otb::SVMClassifier< SampleType, LabelPixelType > ClassifierType ;
ClassifierType::Pointer classifier = ClassifierType::New() ;
classifier->SetNumberOfClasses(numberOfClasses) ;
classifier->SetModel( model );
classifier->SetSample(sample.GetPointer()) ;
classifier->Update() ;
/* Build the class map */
itkGenericExceptionMacro( << "Output image creation" );
typedef ClassifierType::ClassLabelType OutputPixelType;
typedef otb::Image< OutputPixelType, Dimension > OutputImageType;
OutputImageType::Pointer outputImage = OutputImageType::New();
typedef itk::Index<Dimension> myIndexType;
typedef itk::Size<Dimension> mySizeType;
typedef itk::ImageRegion<Dimension> myRegionType;
typedef itk::ImageRegion<Dimension> myRegionType;
mySizeType size;
size[0] = reader->GetOutput()->GetRequestedRegion().GetSize()[0];
......@@ -162,16 +146,16 @@ int otbSVMClassifierImage(int argc, char* argv[] )
outputImage->Allocate();
std::cout << "classifier get output" << std::endl;
itkGenericExceptionMacro( << "classifier get output" );
ClassifierType::OutputType* membershipSample =
classifier->GetOutput() ;
std::cout << "Sample iterators" << std::endl;
itkGenericExceptionMacro( << "Sample iterators" );
ClassifierType::OutputType::ConstIterator m_iter =
membershipSample->Begin() ;
ClassifierType::OutputType::ConstIterator m_last =
membershipSample->End() ;
std::cout << "Image iterator" << std::endl;
itkGenericExceptionMacro( << "Image iterator" );
typedef itk::ImageRegionIterator< OutputImageType> OutputIteratorType;
OutputIteratorType outIt( outputImage,
outputImage->GetBufferedRegion() );
......@@ -179,13 +163,13 @@ int otbSVMClassifierImage(int argc, char* argv[] )
outIt.GoToBegin();
std::cout << "Iteration for output image = " << (membershipSample->Size()) << std::endl;
itkGenericExceptionMacro( << "Iteration for output image = " << (membershipSample->Size()) );
while (m_iter != m_last && !outIt.IsAtEnd())
{
outIt.Set(m_iter.GetClassLabel());
++m_iter ;
++outIt;
outIt.Set(m_iter.GetClassLabel());
++m_iter ;
++outIt;
}
......@@ -211,21 +195,8 @@ int otbSVMClassifierImage(int argc, char* argv[] )
writer->Update();
}
catch( itk::ExceptionObject & err )
{
std::cout << "Exception itk::ExceptionObject levee !" << std::endl;
std::cout << err << std::endl;
return EXIT_FAILURE;
}
catch( ... )
{
std::cout << "Unknown exception !" << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
return EXIT_SUCCESS;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment