Commit c7b5c13e authored by Charles Peyrega's avatar Charles Peyrega

ENH: Templates of Classification Map Regularization filter+application+example+tests were modified

parent c90e15a3
......@@ -20,7 +20,6 @@
#include "otbWrapperApplicationFactory.h"
// Majority Voting filter includes
#include "itkBinaryBallStructuringElement.h"
#include "otbNeighborhoodMajorityVotingImageFilter.h"
namespace otb
......@@ -47,16 +46,14 @@ public:
typedef UInt8ImageType InputLabelImageType;
typedef UInt8ImageType OutputLabelImageType;
// Neighborhood majority voting filter type
typedef otb::NeighborhoodMajorityVotingImageFilter<InputLabelImageType, OutputLabelImageType> NeighborhoodMajorityVotingFilterType;
//SE TYPE
typedef itk::Neighborhood<InputLabelPixelType, 2> StructuringType;
// Binary ball Structuring Element type
typedef NeighborhoodMajorityVotingFilterType::KernelType StructuringType;
typedef StructuringType::RadiusType RadiusType;
//BINARY BALL SE TYPE
typedef itk::BinaryBallStructuringElement<InputLabelPixelType, 2> BallStructuringType;
//NEIGHBORHOOD MAJORITY FILTER TYPE
typedef otb::NeighborhoodMajorityVotingImageFilter<InputLabelImageType, OutputLabelImageType, StructuringType> NeighborhoodMajorityVotingFilterType;
private:
void DoInit()
......@@ -127,15 +124,15 @@ private:
// Majority Voting
m_NeighMajVotingFilter = NeighborhoodMajorityVotingFilterType::New();
// Load input image to CLASSIFY
// Load input labeled image to regularize
UInt8ImageType::Pointer inImage = GetParameterUInt8Image("io.in");
// NEIGHBORHOOD MAJORITY FILTER SETTINGS
// Neighborhood majority voting filter settings
RadiusType rad;
rad[0] = GetParameterInt("ip.radius");
rad[1] = GetParameterInt("ip.radius");
BallStructuringType seBall;
StructuringType seBall;
seBall.SetRadius(rad);
seBall.CreateStructuringElement();
m_NeighMajVotingFilter->SetKernel(seBall);
......@@ -158,14 +155,14 @@ private:
/** REGULARIZATION OF CLASSIFICATION */
SetParameterOutputImage<OutputLabelImageType>("io.out", m_NeighMajVotingFilter->GetOutput());
}//END DoExecute()
}// END DoExecute()
NeighborhoodMajorityVotingFilterType::Pointer m_NeighMajVotingFilter;
}; //END class ClassificationMapRegularization
}; // END class ClassificationMapRegularization
}//END namespace wrapper
}//END namespace otb
}// END namespace wrapper
}// END namespace otb
OTB_APPLICATION_EXPORT(otb::Wrapper::ClassificationMapRegularization)
......@@ -24,13 +24,14 @@
#include "itkConfigure.h"
#include "itkMorphologyImageFilter.h"
#include "itkBinaryBallStructuringElement.h"
namespace otb
{
/**
* \class NeighborhoodMajorityVotingImageFilter
* \Majority Voting of an image
* Neighborhood Majority Voting of an image
*
* Filters a labeled image using Majority Voting in a specified neighbordhood. Majority Voting takes the
* more representative value of all the pixels identified by the structuring element and then sets the
......@@ -56,7 +57,11 @@ namespace otb
* \sa MorphologyImageFilter, GrayscaleFunctionDilateImageFilter, BinaryDilateImageFilter
* \ingroup ImageEnhancement MathematicalMorphologyImageFilters
*/
template<class TInputImage, class TOutputImage=TInputImage, class TKernel=typename itk::Neighborhood<typename TInputImage::PixelType, TInputImage::ImageDimension> >
template<
class TInputImage,
class TOutputImage=TInputImage,
class TKernel=typename itk::BinaryBallStructuringElement<typename TInputImage::PixelType, TInputImage::ImageDimension>
>
class ITK_EXPORT NeighborhoodMajorityVotingImageFilter :
public itk::MorphologyImageFilter<TInputImage, TOutputImage, TKernel>
{
......
......@@ -28,6 +28,9 @@ TARGET_LINK_LIBRARIES(SOMImageClassificationExample OTBIO OTBLearning)
ADD_EXECUTABLE(SVMImageClassificationExample2 SVMImageClassifierExample.cxx )
TARGET_LINK_LIBRARIES(SVMImageClassificationExample2 OTBIO OTBLearning)
ADD_EXECUTABLE(ClassificationMapRegularizationExample ClassificationMapRegularizationExample.cxx )
TARGET_LINK_LIBRARIES(ClassificationMapRegularizationExample OTBIO OTBCommon)
#ADD_EXECUTABLE( BayesianClassifierInitializer BayesianClassifierInitializer.cxx )
#TARGET_LINK_LIBRARIES(BayesianClassifierInitializer ITKStatistics)
......@@ -153,6 +156,20 @@ ADD_TEST(clTeScalarImageKmeansClassifierTest ${EXE_TESTS}
0 4 79.5097 138.136 213.846 25.9395
)
ADD_TEST(clTeNeighborhoodMajorityVotingImageFilterTest ${EXE_TESTS}
--compare-n-images ${NOTOL}
${OTB_DATA_ROOT}/Baseline/OTB/Images/QB_1_ortho_4Cls_N_Classified_OTB_NMV_x2_y5_nodv10_undv7.tif
${TEMP}/QB_1_ortho_4Cls_N_Classified_OTB_NMV_x2_y5_nodv10_undv7_EX.tif
ClassificationMapRegularizationExampleTest
${OTB_DATA_ROOT}/Input/Classification/QB_1_ortho_4Cls_N_Classified_OTB.tif
${TEMP}/QB_1_ortho_4Cls_N_Classified_OTB_NMV_x2_y5_nodv10_undv7_EX.tif
false #KeepOriginalLabelBool
2 #xRadius
5 #yRadius
10 #LabelForNoDataPixels
7 #LabelForUndecidedPixels
)
INCLUDE_DIRECTORIES(${OTB_SOURCE_DIR}/Testing/Code)
ADD_EXECUTABLE(otbClassificationExamplesTests otbClassificationExamplesTests.cxx)
TARGET_LINK_LIBRARIES(otbClassificationExamplesTests ITKAlgorithms ITKStatistics OTBBasicFilters OTBCommon OTBDisparityMap OTBIO OTBSpatialReasoning OTBChangeDetection OTBFeatureExtraction OTBLearning OTBMultiScale OTBTesting)
......
/*=========================================================================
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.
=========================================================================*/
// Software Guide : BeginLatex
//
// After having generated a classification map, it is possible to
// regularize such a labeled image in order to obtain more homogeneous
// areas, which makes the interpretation of its classes easier. For this
// purpose, the \doxygen{otb}{NeighborhoodMajorityVotingImageFilter} was
// implemented. Like a morphological filter, this filter uses majority
// voting in a ball shaped neighborhood in order to set each pixel of the
// classification map to the more representative label value in its
// neighborhood.
//
// In this example we will illustrate its use. We start by including the
// appropriate header file.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
#include "otbNeighborhoodMajorityVotingImageFilter.h"
// Software Guide : EndCodeSnippet
#include "itkMacro.h"
#include "otbImage.h"
#include <iostream>
#include <otbImageFileReader.h>
#include "otbImageFileWriter.h"
#include "itkTimeProbe.h"
int main(int argc, char * argv[])
{
// Software Guide : BeginLatex
//
// Since the input image is a classification map, we will assume a
// single band input image for which each pixel value is a label coded
// on 8 bits as an integer between 0 and 255.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef unsigned char InputLabelPixelType;
typedef unsigned char OutputLabelPixelType;
const unsigned int Dimension = 2;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Thus, both input and output images are single band labeled images,
// which are composed of the same type of pixels in this example
// (unsigned char).
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef otb::Image<InputLabelPixelType, Dimension> InputLabelImageType;
typedef otb::Image<OutputLabelPixelType, Dimension> OutputLabelImageType;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We can now define the type for the neighborhood majority voting filter,
// which is templated over its input and output images types and over its
// structuring element type.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
// Neighborhood majority voting filter type
typedef otb::NeighborhoodMajorityVotingImageFilter<InputLabelImageType,
OutputLabelImageType> NeighborhoodMajorityVotingFilterType;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Since the \doxygen{otb}{NeighborhoodMajorityVotingImageFilter} is a
// neighborhood based image filter, it is necessary to set the structuring
// element which will be used for the majority voting process. By default, the
// structuring element is a ball
// (\doxygen{itk}{BinaryBallStructuringElement}) with a radius defined by two sizes
// (respectively along X and Y). Thus, it is possible to handle anisotropic
// structuring elements such as ovals.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
// Binary ball Structuring Element type
typedef NeighborhoodMajorityVotingFilterType::KernelType StructuringType;
typedef StructuringType::RadiusType RadiusType;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Finally, we define the reader and the writer.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef otb::ImageFileReader<InputLabelImageType> ReaderType;
typedef otb::ImageFileWriter<OutputLabelImageType> WriterType;
// Software Guide : EndCodeSnippet
const char * inputFileName = argv[1];
const char * outputFileName = argv[2];
// Software Guide : BeginLatex
//
// We instantiate the \doxygen{otb}{NeighborhoodMajorityVotingImageFilter} and the
// reader objects.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
// Neighborhood majority voting filter
NeighborhoodMajorityVotingFilterType::Pointer NeighMajVotingFilter;
NeighMajVotingFilter = NeighborhoodMajorityVotingFilterType::New();
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(inputFileName);
// Software Guide : EndCodeSnippet
std::string KeepOriginalLabelBoolStr = argv[3];
unsigned int radiusX = atoi(argv[4]);
unsigned int radiusY = atoi(argv[5]);
OutputLabelPixelType noDataValue = atoi(argv[6]);
OutputLabelPixelType undecidedValue = atoi(argv[7]);
// Software Guide : BeginLatex
//
// The ball shaped structuring element seBall is instantiated and its
// two radii along X and Y are initialized.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
StructuringType seBall;
RadiusType rad;
rad[0] = radiusX;
rad[1] = radiusY;
seBall.SetRadius(rad);
seBall.CreateStructuringElement();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Then, this ball shaped neighborhood is used as the kernel structuring element
// for the \doxygen{otb}{NeighborhoodMajorityVotingImageFilter}.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
NeighMajVotingFilter->SetKernel(seBall);
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Not classified input pixels are assumed to have the noDataValue label
// and will keep this label in the output image.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
NeighMajVotingFilter->SetLabelForNoDataPixels(noDataValue);
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Moreover, since the majority voting regularization may lead to not unique
// majority labels in the neighborhood, it is important to define which behaviour
// the filter must have in this case. For this purpose, a Boolean parameter is used
// in the filter to choose whether pixels with more than one majority class are set
// to undecidedValue (true), or to their Original labels (false = default value)
// in the output image.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
NeighMajVotingFilter->SetLabelForUndecidedPixels(undecidedValue);
if (KeepOriginalLabelBoolStr.compare("true") == 0)
{
NeighMajVotingFilter->SetKeepOriginalLabelBool(true);
}
else
{
NeighMajVotingFilter->SetKeepOriginalLabelBool(false);
}
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We plug the pipeline and
// trigger its execution by updating the output of the writer.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
NeighMajVotingFilter->SetInput(reader->GetOutput());
WriterType::Pointer writer = WriterType::New();
writer->SetFileName(outputFileName);
writer->SetInput(NeighMajVotingFilter->GetOutput());
writer->Update();
// Software Guide : EndCodeSnippet
return EXIT_SUCCESS;
}
......@@ -26,6 +26,7 @@ void RegisterTests()
{
REGISTER_TEST(ScalarImageMarkovRandomField1Test);
REGISTER_TEST(ScalarImageKmeansClassifierTest);
REGISTER_TEST(ClassificationMapRegularizationExampleTest);
}
#undef main
......@@ -35,3 +36,7 @@ void RegisterTests()
#undef main
#define main ScalarImageKmeansClassifierTest
#include "ScalarImageKmeansClassifier.cxx"
#undef main
#define main ClassificationMapRegularizationExampleTest
#include "ClassificationMapRegularizationExample.cxx"
......@@ -19,26 +19,27 @@
#include "otbImage.h"
#include <iostream>
#include "itkBinaryBallStructuringElement.h"
#include "otbNeighborhoodMajorityVotingImageFilter.h"
int otbNeighborhoodMajorityVotingImageFilterNew(int argc, char* argv[])
{
typedef unsigned char InputLabelPixelType; //8 bits
typedef unsigned short OutputLabelPixelType; //16 bits
typedef unsigned char InputLabelPixelType; // 8 bits
typedef unsigned short OutputLabelPixelType; // 16 bits
const unsigned int Dimension = 2;
typedef otb::Image<InputLabelPixelType, Dimension> InputLabelImageType;
typedef otb::Image<OutputLabelPixelType, Dimension> OutputLabelImageType;
//SE TYPE
typedef itk::Neighborhood<InputLabelPixelType, Dimension> StructuringType;
// Binary ball Structuring Element type
typedef itk::BinaryBallStructuringElement<InputLabelPixelType, Dimension> BallStructuringType;
//NEIGHBORHOOD MAJORITY FILTER TYPES
typedef otb::NeighborhoodMajorityVotingImageFilter<InputLabelImageType, OutputLabelImageType, StructuringType> NeighborhoodMajorityVotingFilter3ArgsType;
// Neighborhood majority voting filter types
typedef otb::NeighborhoodMajorityVotingImageFilter<InputLabelImageType, OutputLabelImageType, BallStructuringType> NeighborhoodMajorityVotingFilter3ArgsType;
typedef otb::NeighborhoodMajorityVotingImageFilter<InputLabelImageType, OutputLabelImageType> NeighborhoodMajorityVotingFilter2ArgsType;
typedef otb::NeighborhoodMajorityVotingImageFilter<InputLabelImageType> NeighborhoodMajorityVotingFilter1ArgType;
//NEIGHBORHOOD MAJORITY FILTERS
// Neighborhood majority voting filters
NeighborhoodMajorityVotingFilter3ArgsType::Pointer NeighMajVotingFilter3Args = NeighborhoodMajorityVotingFilter3ArgsType::New();
NeighborhoodMajorityVotingFilter2ArgsType::Pointer NeighMajVotingFilter2Args = NeighborhoodMajorityVotingFilter2ArgsType::New();
NeighborhoodMajorityVotingFilter1ArgType::Pointer NeighMajVotingFilter1Arg = NeighborhoodMajorityVotingFilter1ArgType::New();
......
......@@ -23,8 +23,6 @@
#include <otbImageFileReader.h>
#include "otbImageFileWriter.h"
#include "itkBinaryBallStructuringElement.h"
#include "otbNeighborhoodMajorityVotingImageFilter.h"
#include "itkTimeProbe.h"
......@@ -32,8 +30,8 @@
int otbNeighborhoodMajorityVotingImageFilterTest(int argc, char* argv[])
{
typedef unsigned char InputLabelPixelType; //8 bits
typedef unsigned char OutputLabelPixelType; //8 bits
typedef unsigned char InputLabelPixelType; // 8 bits
typedef unsigned char OutputLabelPixelType; // 8 bits
const unsigned int Dimension = 2;
typedef otb::Image<InputLabelPixelType, Dimension> InputLabelImageType;
......@@ -42,15 +40,13 @@ int otbNeighborhoodMajorityVotingImageFilterTest(int argc, char* argv[])
typedef otb::ImageFileReader<InputLabelImageType> ReaderType;
typedef otb::ImageFileWriter<OutputLabelImageType> WriterType;
//SE TYPE
typedef itk::Neighborhood<InputLabelPixelType, Dimension> StructuringType;
typedef StructuringType::RadiusType RadiusType;
//BINARY BALL SE TYPE
typedef itk::BinaryBallStructuringElement<InputLabelPixelType, Dimension> BallStructuringType;
// Neighborhood majority voting filter type
typedef otb::NeighborhoodMajorityVotingImageFilter<InputLabelImageType, OutputLabelImageType> NeighborhoodMajorityVotingFilterType;
//NEIGHBORHOOD MAJORITY FILTER TYPE
typedef otb::NeighborhoodMajorityVotingImageFilter<InputLabelImageType, OutputLabelImageType, StructuringType> NeighborhoodMajorityVotingFilterType;
// Binary ball Structuring Element type
typedef NeighborhoodMajorityVotingFilterType::KernelType StructuringType;
typedef StructuringType::RadiusType RadiusType;
const char * inputFileName = argv[1];
......@@ -59,13 +55,13 @@ int otbNeighborhoodMajorityVotingImageFilterTest(int argc, char* argv[])
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(inputFileName);
//NEIGHBORHOOD MAJORITY FILTER
// Neighborhood majority voting filter
NeighborhoodMajorityVotingFilterType::Pointer NeighMajVotingFilter;
NeighMajVotingFilter = NeighborhoodMajorityVotingFilterType::New();
NeighMajVotingFilter->SetInput(reader->GetOutput());
BallStructuringType seBall;
StructuringType seBall;
RadiusType rad;
......
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