Skip to content
Snippets Groups Projects
Commit 5627f4e4 authored by Jonathan Guinet's avatar Jonathan Guinet
Browse files

ENH: train SVM classifier sample.edg non mandatory new option.

parent 3f388dd9
Branches
Tags
No related merge requests found
......@@ -170,9 +170,9 @@ private:
SetDefaultParameterInt("sample.mv", -1);
SetParameterDescription("sample.mv", "Maximum size of the validation sample list (default = -1)");
// AddParameter(ParameterType_Int, "sample.b", "Balance and grow the training set");
// SetParameterDescription("sample.b", "Balance and grow the training set.");
// MandatoryOff("sample.b");
AddParameter(ParameterType_Empty, "sample.edg", "On edge pixel inclusion");
SetParameterDescription("sample.edg", "Take pixels on polygon edge into consideration when building training and validation samples.");
MandatoryOff("sample.edg");
AddParameter(ParameterType_Float, "sample.vtr", "training and validation sample ratio");
SetParameterDescription("sample.vtr",
......@@ -238,6 +238,8 @@ private:
FloatVectorImageListType* imageList = GetParameterImageList("io.il");
VectorDataListType* vectorDataList = GetParameterVectorDataList("io.vd");
vdreproj = VectorDataReprojectionType::New();
//Iterate over all input images
for (unsigned int imgIndex = 0; imgIndex < imageList->Size(); ++imgIndex)
{
......@@ -253,7 +255,6 @@ private:
VectorDataType::Pointer vectorData = vectorDataList->GetNthElement(imgIndex);
vectorData->Update();
VectorDataReprojectionType::Pointer vdreproj = VectorDataReprojectionType::New();
vdreproj->SetInputImage(image);
vdreproj->SetInput(vectorData);
vdreproj->SetUseOutputSpacingAndOriginFromImage(false);
......@@ -287,8 +288,7 @@ private:
//Sample list generator
ListSampleGeneratorType::Pointer sampleGenerator = ListSampleGeneratorType::New();
//m_sampleGenerator = sampleGenerator;
//Set inputs of the sample generator
sampleGenerator->SetInput(image);
sampleGenerator->SetInputVectorData(vdreproj->GetOutput());
......@@ -297,6 +297,12 @@ private:
sampleGenerator->SetMaxValidationSize(GetParameterInt("sample.mv"));
sampleGenerator->SetValidationTrainingProportion(GetParameterFloat("sample.vtr"));
// take pixel located on polygon edge into consideration
if (IsParameterEnabled("sample.edg"))
{
sampleGenerator->SetPolygonEdgeInclusion(true);
}
sampleGenerator->Update();
//Concatenate training and validation samples from the image
......@@ -467,7 +473,7 @@ private:
}
VectorDataReprojectionType::Pointer vdreproj;
};
}
......
......@@ -99,6 +99,8 @@ public:
/** Accessors */
itkGetConstMacro(MaxTrainingSize, long int);
itkSetMacro(MaxTrainingSize, long int);
itkGetConstMacro(PolygonEdgeInclusion, bool);
itkSetMacro(PolygonEdgeInclusion, bool);
itkGetConstMacro(MaxValidationSize, long int);
itkSetMacro(MaxValidationSize, long int);
itkGetConstMacro(ValidationTrainingProportion, double);
......@@ -153,6 +155,9 @@ private:
double m_ValidationTrainingProportion; // proportion of training vs validation
// (0.0 = all training, 1.0 = all validation)
bool m_PolygonEdgeInclusion; // if true take into consideration pixel which are on polygon edge
// useful, when dealing with small polygon area (1 or two pixels)
// false by default
unsigned short m_NumberOfClasses;
std::string m_ClassKey;
double m_ClassMinSize;
......
......@@ -64,6 +64,7 @@ ListSampleGenerator<TImage, TVectorData>
m_MaxTrainingSize(-1),
m_MaxValidationSize(-1),
m_ValidationTrainingProportion(0.0),
m_PolygonEdgeInclusion(false),
m_NumberOfClasses(0),
m_ClassKey("Class"),
m_ClassMinSize(-1)
......@@ -183,12 +184,9 @@ ListSampleGenerator<TImage, TVectorData>
otb::TransformPhysicalRegionToIndexRegion(itVector.Get()->GetPolygonExteriorRing()->GetBoundingRegion(),
image.GetPointer());
//std::cout << "Image region from polygon:\n" << polygonRegion << std::endl;
//std::cout << "Image largest possible region:\n" << image->GetLargestPossibleRegion() << std::endl;
image->SetRequestedRegion(polygonRegion);
image->PropagateRequestedRegion();
image->UpdateOutputData();
//std::cout << "Image region requested:\n" << image->GetRequestedRegion() << std::endl;
typedef itk::ImageRegionConstIteratorWithIndex<ImageType> IteratorType;
IteratorType it(image, polygonRegion);
......@@ -197,8 +195,8 @@ ListSampleGenerator<TImage, TVectorData>
{
itk::ContinuousIndex<double, 2> point;
image->TransformIndexToPhysicalPoint(it.GetIndex(), point);
//std::cout << it.GetIndex() << " -> " << point << std::endl;
if (itVector.Get()->GetPolygonExteriorRing()->IsInside(point))
if (itVector.Get()->GetPolygonExteriorRing()->IsInside(point) ||
(itVector.Get()->GetPolygonExteriorRing()->IsOnEdge(point) && (this->GetPolygonEdgeInclusion())))
{
double randomValue = m_RandomGenerator->GetUniformVariate(0.0, 1.0);
if (randomValue < m_ClassesProbTraining[itVector.Get()->GetFieldAsInt(m_ClassKey)])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment