Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
otb
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Julien Cabieces
otb
Commits
e101a5af
Commit
e101a5af
authored
7 years ago
by
Guillaume Pasero
Browse files
Options
Downloads
Patches
Plain Diff
REFAC: use new LibSVM model in example
parent
abf16310
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Examples/Learning/SVMImageEstimatorClassificationMultiExample.cxx
+54
-134
54 additions, 134 deletions
.../Learning/SVMImageEstimatorClassificationMultiExample.cxx
with
54 additions
and
134 deletions
Examples/Learning/SVMImageEstimatorClassificationMultiExample.cxx
+
54
−
134
View file @
e101a5af
...
...
@@ -52,15 +52,16 @@
#include
<iostream>
// Software Guide : BeginCodeSnippet
#include
"otb
SVMImageModelEstimator
.h"
#include
"itkImageToListSample
Adapto
r.h"
#include
"otb
SVM
Classifier.h"
#include
"otb
LibSVMMachineLearningModel
.h"
#include
"itkImageToListSample
Filte
r.h"
#include
"otb
Image
Classifi
cationFilt
er.h"
// Software Guide : EndCodeSnippet
#include
"otbImageFileWriter.h"
#include
"itkUnaryFunctorImageFilter.h"
#include
"itkScalarToRGBPixelFunctor.h"
#include
"itkBinaryThresholdImageFilter.h"
#include
"otbImageFileReader.h"
...
...
@@ -92,13 +93,13 @@ int main(int itkNotUsed(argc), char *argv[])
// Software Guide : BeginLatex
//
// The \doxygen{otb}{
SVMImageModelEstimator
} class is templated over
// the input (features) and the training (labels)
imag
es.
// The \doxygen{otb}{
LibSVMMachineLearningModel
} class is templated over
// the input (features) and the training (labels)
valu
es.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef
otb
::
SVMImageModelEstimator
<
Input
Image
Type
,
TrainingImageType
>
Estimator
Type
;
typedef
otb
::
LibSVMMachineLearningModel
<
Input
Pixel
Type
,
InputPixelType
>
Model
Type
;
// Software Guide : EndCodeSnippet
...
...
@@ -128,107 +129,67 @@ int main(int itkNotUsed(argc), char *argv[])
inputReader
->
SetFileName
(
inputImageFileName
);
trainingReader
->
SetFileName
(
trainingImageFileName
);
inputReader
->
Update
();
trainingReader
->
Update
();
//~
inputReader->Update();
//~
trainingReader->Update();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We can now instantiate the model estimator and set its parameters.
// The input data is contained in images. Only label values greater than 0
// shall be used, so we create two iterators to fill the input and target
// ListSamples.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
EstimatorType
::
Pointer
svmEstimator
=
EstimatorType
::
New
();
svmEstimator
->
SetInputImage
(
inputReader
->
GetOutput
());
svmEstimator
->
SetTrainingImage
(
trainingReader
->
GetOutput
());
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The model estimation procedure is triggered by calling the
// estimator's \code{Update} method.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
svmEstimator
->
Update
();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We can now proceed to the image classification. We start by
// declaring the type of the image to be classified. ITK's
// classification framework needs the type of the pixel to be of
// fixed type, so we declare the following types.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef
otb
::
Image
<
itk
::
FixedArray
<
InputPixelType
,
3
>
,
Dimension
>
ClassifyImageType
;
typedef
itk
::
BinaryThresholdImageFilter
<
TrainingImageType
,
TrainingImageType
>
ThresholdFilterType
;
ThresholdFilterType
::
Pointer
thresholder
=
ThresholdFilterType
::
New
();
thresholder
->
SetInput
(
trainingReader
->
GetOutput
());
thresholder
->
SetLowerThreshold
(
1
);
thresholder
->
SetOutsideValue
(
0
);
thresholder
->
SetInsideValue
(
1
);
typedef
otb
::
ImageFileReader
<
ClassifyImageType
>
ClassifyReaderTyp
e
;
// Software Guide : EndCodeSnippet
typedef
itk
::
Statistics
::
ImageToListSampleFilter
<
InputImageType
,
TrainingImageType
>
ImageToListSampl
e
;
typedef
itk
::
Statistics
::
ImageToListSampleFilter
<
TrainingImageType
,
TrainingImageType
>
ImageToTargetListSample
;
// Software Guide : BeginLatex
//
// We can now read the image by calling the \code{Update} method of the reader.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
ClassifyReaderType
::
Pointer
cReader
=
ClassifyReaderType
::
New
();
ImageToListSample
::
Pointer
imToList
=
ImageToListSample
::
New
();
imToList
->
SetInput
(
inputReader
->
GetOutput
());
imToList
->
SetMaskImage
(
thresholder
->
GetOutput
());
imToList
->
SetMaskValue
(
1
);
imToList
->
Update
();
cReader
->
SetFileName
(
inputImageFileName
);
ImageToTargetListSample
::
Pointer
imToTargetList
=
ImageToTargetListSample
::
New
();
imToTargetList
->
SetInput
(
trainingReader
->
GetOutput
());
imToTargetList
->
SetMaskImage
(
thresholder
->
GetOutput
());
imToTargetList
->
SetMaskValue
(
1
);
imToTargetList
->
Update
();
cReader
->
Update
();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The image has now to be transformed to a sample which
// is compatible with the classification framework. We will use a
// \doxygen{itk}{Statistics::ImageToListSampleAdaptor} for this
// task. This class is templated over the image type used for
// storing the measures.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef
itk
::
Statistics
::
ImageToListSampleAdaptor
<
ClassifyImageType
>
SampleType
;
SampleType
::
Pointer
sample
=
SampleType
::
New
();
// Software Guide : EndCodeSnippet
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
Software Guide : BeginLatex
//
// After instantiation, we can set the image as an imput of our
// sample adaptor.
// We can now instantiate the model and set its parameters.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
ModelType
::
Pointer
svmModel
=
ModelType
::
New
();
svmModel
->
SetInputListSample
(
const_cast
<
ModelType
::
InputListSampleType
*>
(
imToList
->
GetOutput
()));
svmModel
->
SetTargetListSample
(
const_cast
<
ModelType
::
TargetListSampleType
*>
(
imToTargetList
->
GetOutput
()));
// Software Guide : BeginCodeSnippet
sample
->
SetImage
(
cReader
->
GetOutput
());
// Software Guide : EndCodeSnippet
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
Software Guide : BeginLatex
//
// Now, we need to declare the SVM model which is to be used by the
// classifier. The SVM model is templated over the type of value used
// for the measures and the type of pixel used for the labels. The
// model is obtained from the model estimator by calling the
// \code{GetModel} method.
// The model training procedure is triggered by calling the
// model's \code{Train} method.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
svmModel
->
Train
();
// Software Guide : BeginCodeSnippet
typedef
InputPixelType
LabelPixelType
;
typedef
otb
::
SVMModel
<
InputPixelType
,
LabelPixelType
>
ModelType
;
ModelType
::
Pointer
model
=
svmEstimator
->
GetModel
();
// Software Guide : EndCodeSnippet
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
...
...
@@ -239,7 +200,7 @@ int main(int itkNotUsed(argc), char *argv[])
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef
otb
::
SVM
Classifi
er
<
SampleType
,
LabelPixel
Type
>
ClassifierType
;
typedef
otb
::
Image
Classifi
cationFilter
<
InputImageType
,
TrainingImage
Type
>
ClassifierType
;
ClassifierType
::
Pointer
classifier
=
ClassifierType
::
New
();
// Software Guide : EndCodeSnippet
...
...
@@ -253,11 +214,8 @@ int main(int itkNotUsed(argc), char *argv[])
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
int
numberOfClasses
=
model
->
GetNumberOfClasses
();
classifier
->
SetNumberOfClasses
(
numberOfClasses
);
classifier
->
SetModel
(
model
);
classifier
->
SetInput
(
sample
.
GetPointer
());
classifier
->
Update
();
classifier
->
SetModel
(
svmModel
);
classifier
->
SetInput
(
inputReader
->
GetOutput
());
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
...
...
@@ -272,10 +230,7 @@ int main(int itkNotUsed(argc), char *argv[])
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef
ClassifierType
::
ClassLabelType
OutputPixelType
;
typedef
otb
::
Image
<
OutputPixelType
,
Dimension
>
OutputImageType
;
OutputImageType
::
Pointer
outputImage
=
OutputImageType
::
New
();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
...
...
@@ -286,25 +241,7 @@ int main(int itkNotUsed(argc), char *argv[])
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef
itk
::
Index
<
Dimension
>
myIndexType
;
typedef
itk
::
Size
<
Dimension
>
mySizeType
;
typedef
itk
::
ImageRegion
<
Dimension
>
myRegionType
;
mySizeType
size
;
size
[
0
]
=
cReader
->
GetOutput
()
->
GetRequestedRegion
().
GetSize
()[
0
];
size
[
1
]
=
cReader
->
GetOutput
()
->
GetRequestedRegion
().
GetSize
()[
1
];
myIndexType
start
;
start
[
0
]
=
0
;
start
[
1
]
=
0
;
myRegionType
region
;
region
.
SetIndex
(
start
);
region
.
SetSize
(
size
);
outputImage
->
SetRegions
(
region
);
outputImage
->
Allocate
();
std
::
cout
<<
"---"
<<
std
::
endl
;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
...
...
@@ -315,18 +252,7 @@ int main(int itkNotUsed(argc), char *argv[])
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
ClassifierType
::
OutputType
*
membershipSample
=
classifier
->
GetOutput
();
ClassifierType
::
OutputType
::
ConstIterator
m_iter
=
membershipSample
->
Begin
();
ClassifierType
::
OutputType
::
ConstIterator
m_last
=
membershipSample
->
End
();
typedef
itk
::
ImageRegionIterator
<
OutputImageType
>
OutputIteratorType
;
OutputIteratorType
outIt
(
outputImage
,
outputImage
->
GetBufferedRegion
());
outIt
.
GoToBegin
();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
...
...
@@ -337,21 +263,15 @@ int main(int itkNotUsed(argc), char *argv[])
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
while
(
m_iter
!=
m_last
&&
!
outIt
.
IsAtEnd
())
{
outIt
.
Set
(
m_iter
.
GetClassLabel
());
++
m_iter
;
++
outIt
;
}
std
::
cout
<<
"---"
<<
std
::
endl
;
// Software Guide : EndCodeSnippet
typedef
otb
::
ImageFileWriter
<
Output
ImageType
>
WriterType
;
typedef
otb
::
ImageFileWriter
<
Training
ImageType
>
WriterType
;
WriterType
::
Pointer
writer
=
WriterType
::
New
();
writer
->
SetFileName
(
outputImageFileName
);
writer
->
SetInput
(
outputImage
);
writer
->
SetInput
(
classifier
->
GetOutput
()
);
writer
->
Update
();
...
...
@@ -372,12 +292,12 @@ int main(int itkNotUsed(argc), char *argv[])
typedef
otb
::
Image
<
RGBPixelType
,
2
>
RGBImageType
;
typedef
itk
::
Functor
::
ScalarToRGBPixelFunctor
<
unsigned
long
>
ColorMapFunctorType
;
typedef
itk
::
UnaryFunctorImageFilter
<
Output
ImageType
,
typedef
itk
::
UnaryFunctorImageFilter
<
Training
ImageType
,
RGBImageType
,
ColorMapFunctorType
>
ColorMapFilterType
;
ColorMapFilterType
::
Pointer
colormapper
=
ColorMapFilterType
::
New
();
colormapper
->
SetInput
(
outputImage
);
colormapper
->
SetInput
(
classifier
->
GetOutput
()
);
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment