Commit 73afa1d3 authored by Charles Peyrega's avatar Charles Peyrega
Browse files

ENH: Majority Voting: Indentation of code is replaced by spaces

parent 202a195f
......@@ -23,155 +23,146 @@
#include "itkBinaryBallStructuringElement.h"
#include "otbNeighborhoodMajorityVotingImageFilter.h"
namespace otb
{
namespace Wrapper
namespace Wrapper
{
class ClassificationRegularisationMajorityVoting : public Application
{
public:
/** Standard class typedefs. */
typedef ClassificationRegularisationMajorityVoting Self;
typedef Application Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
/** Standard macro */
itkNewMacro(Self);
itkTypeMacro(ClassificationRegularisationMajorityVoting, otb::Application);
/** Filters typedef */
typedef unsigned char InputLabelPixelType;
typedef UInt8ImageType InputLabelImageType;
typedef UInt8ImageType OutputLabelImageType;
//SE TYPE
typedef itk::Neighborhood<InputLabelPixelType, 2> 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()
{
class ClassificationRegularisationMajorityVoting : public Application
{
public:
/** Standard class typedefs. */
typedef ClassificationRegularisationMajorityVoting Self;
typedef Application Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
/** Standard macro */
itkNewMacro(Self);
itkTypeMacro(ClassificationRegularisationMajorityVoting, otb::Application);
typedef unsigned char InputLabelPixelType;
SetName("ClassificationRegularisationMajorityVoting");
SetDescription("Filters the input labeled image using Majority Voting in a Ball shaped neighbordhood.");
SetDocName("Regularisation of a labeled image by Majority Voting");
SetDocLongDescription("This application filters the input labeled image using Majority Voting in a Ball shaped neighbordhood. Majority Voting takes the more representative value of all the pixels identified by the Ball shaped structuring element and then sets the center pixel to this majority label value.\n-'NoDataValue' is the label of the NOT classified pixels of the input image. These input pixels keep their 'NoDataValue' label in the output image.\n-If the 'UnDefinedValue if multiple majority labels' option is chosen, then 'UnDefinedValue' is the label of the output pixels, for which there are NOT unique majority labels. Otherwise, these output label pixels keep their Original values in the input image.");
SetDocLimitations("The input image must be a single band labeled image. Both radii along Ox and Oy must have a minimum value equal to 1.");
SetDocAuthors("OTB-Team");
SetDocSeeAlso("Documentation of the ClassificationRegularisationMajorityVoting application.");
AddDocTag(Tags::Learning);
AddDocTag(Tags::Analysis);
/** GROUP IO CLASSIFICATION */
AddParameter(ParameterType_Group,"io","Input and output images");
SetParameterDescription("io","This group of parameters allows to set input and output images for Majority Voting regularisation.");
AddParameter(ParameterType_InputImage, "io.in", "Input Image to REGULARIZE");
SetParameterDescription( "io.in", "The input labeled image to regularize.");
AddParameter(ParameterType_OutputImage, "io.out", "Output REGULARIZED Image");
SetParameterDescription( "io.out", "Output regularized labeled image.");
SetParameterOutputImagePixelType( "io.out", ImagePixelType_uint8);
AddParameter(ParameterType_Group,"ip","Input parameters");
SetParameterDescription("ip","This group of parameters allows to set input parameters for Majority Voting regularisation.");
AddParameter(ParameterType_Int, "ip.radiusx", "Radius along Ox");
SetDefaultParameterInt("ip.radiusx", 1.0);
AddParameter(ParameterType_Int, "ip.radiusy", "Radius along Oy");
SetDefaultParameterInt("ip.radiusy", 1.0);
AddParameter(ParameterType_Empty, "ip.suvbool", "UnDefinedValue if multiple majority labels");
AddParameter(ParameterType_Int, "ip.nodatavalue", "Value of the NoDataValue input label");
SetDefaultParameterInt("ip.nodatavalue", 0.0);
AddParameter(ParameterType_Int, "ip.undefinedvalue", "Value of the UnDefinedValue output label");
SetDefaultParameterInt("ip.undefinedvalue", 0.0);
AddRAMParameter();
// Doc example parameter settings
SetDocExampleParameterValue("io.in", "QB_1_ortho_4Cls_N_Classified_OTB.tif");
SetDocExampleParameterValue("io.out", "QB_1_ortho_4Cls_N_Classified_OTB_NMV_x2_y5_nodv10_undv7.tif");
SetDocExampleParameterValue("ip.radiusx", "2");
SetDocExampleParameterValue("ip.radiusy", "5");
SetDocExampleParameterValue("ip.suvbool", "true");
SetDocExampleParameterValue("ip.nodatavalue", "10");
SetDocExampleParameterValue("ip.undefinedvalue", "7");
}
void DoUpdateParameters()
{
// Nothing to do here : all parameters are independent
}
typedef UInt8ImageType InputLabelImageType;
typedef UInt8ImageType OutputLabelImageType;
//SE TYPE
typedef itk::Neighborhood<InputLabelPixelType, 2> 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()
void DoExecute()
{
// Majority Voting
m_NeighMajVotingFilter = NeighborhoodMajorityVotingFilterType::New();
// Load input image to CLASSIFY
UInt8ImageType::Pointer inImage = GetParameterUInt8Image("io.in");
// NEIGHBORHOOD MAJORITY FILTER SETTINGS
RadiusType rad;
rad[0] = GetParameterInt("ip.radiusx");
rad[1] = GetParameterInt("ip.radiusy");
BallStructuringType seBall;
seBall.SetRadius(rad);
seBall.CreateStructuringElement();
m_NeighMajVotingFilter->SetKernel(seBall);
m_NeighMajVotingFilter->SetInput(inImage);
m_NeighMajVotingFilter->SetNoDataValue(GetParameterInt("ip.nodatavalue"));
m_NeighMajVotingFilter->SetUndefinedValue(GetParameterInt("ip.undefinedvalue"));
// Set to UnDefinedValue if NOT unique Majority Voting
if (IsParameterEnabled("ip.suvbool"))
{
SetName("ClassificationRegularisationMajorityVoting");
SetDescription("Filters the input labeled image using Majority Voting in a Ball shaped neighbordhood.");
SetDocName("Regularisation of a labeled image by Majority Voting");
SetDocLongDescription("This application filters the input labeled image using Majority Voting in a Ball shaped neighbordhood. Majority Voting takes the more representative value of all the pixels identified by the Ball shaped structuring element and then sets the center pixel to this majority label value.\n-'NoDataValue' is the label of the NOT classified pixels of the input image. These input pixels keep their 'NoDataValue' label in the output image.\n-If the 'UnDefinedValue if multiple majority labels' option is chosen, then 'UnDefinedValue' is the label of the output pixels, for which there are NOT unique majority labels. Otherwise, these output label pixels keep their Original values in the input image.");
SetDocLimitations("The input image must be a single band labeled image. Both radii along Ox and Oy must have a minimum value equal to 1.");
SetDocAuthors("OTB-Team");
SetDocSeeAlso("Documentation of the ClassificationRegularisationMajorityVoting application.");
AddDocTag(Tags::Learning);
AddDocTag(Tags::Analysis);
/** GROUP IO CLASSIFICATION */
AddParameter(ParameterType_Group,"io","Input and output images");
SetParameterDescription("io","This group of parameters allows to set input and output images for Majority Voting regularisation.");
AddParameter(ParameterType_InputImage, "io.in", "Input Image to REGULARIZE");
SetParameterDescription( "io.in", "The input labeled image to regularize.");
AddParameter(ParameterType_OutputImage, "io.out", "Output REGULARIZED Image");
SetParameterDescription( "io.out", "Output regularized labeled image.");
SetParameterOutputImagePixelType( "io.out", ImagePixelType_uint8);
AddParameter(ParameterType_Group,"ip","Input parameters");
SetParameterDescription("ip","This group of parameters allows to set input parameters for Majority Voting regularisation.");
AddParameter(ParameterType_Int, "ip.radiusx", "Radius along Ox");
SetDefaultParameterInt("ip.radiusx", 1.0);
AddParameter(ParameterType_Int, "ip.radiusy", "Radius along Oy");
SetDefaultParameterInt("ip.radiusy", 1.0);
AddParameter(ParameterType_Empty, "ip.suvbool", "UnDefinedValue if multiple majority labels");
AddParameter(ParameterType_Int, "ip.nodatavalue", "Value of the NoDataValue input label");
SetDefaultParameterInt("ip.nodatavalue", 0.0);
AddParameter(ParameterType_Int, "ip.undefinedvalue", "Value of the UnDefinedValue output label");
SetDefaultParameterInt("ip.undefinedvalue", 0.0);
AddRAMParameter();
// Doc example parameter settings
SetDocExampleParameterValue("io.in", "QB_1_ortho_4Cls_N_Classified_OTB.tif");
SetDocExampleParameterValue("io.out", "QB_1_ortho_4Cls_N_Classified_OTB_NMV_x2_y5.tif");
SetDocExampleParameterValue("ip.radiusx", "2");
SetDocExampleParameterValue("ip.radiusy", "5");
SetDocExampleParameterValue("ip.nodatavalue", "10");
SetDocExampleParameterValue("ip.undefinedvalue", "7");
m_NeighMajVotingFilter->SetKeepOriginalLabelBool(false);
}
void DoUpdateParameters()
// Keep original label value if NOT unique Majority Voting
else
{
// Nothing to do here : all parameters are independent
m_NeighMajVotingFilter->SetKeepOriginalLabelBool(true);
}
void DoExecute()
{
// Majority Voting
m_NeighMajVotingFilter = NeighborhoodMajorityVotingFilterType::New();
// Load input image to CLASSIFY
UInt8ImageType::Pointer inImage = GetParameterUInt8Image("io.in");
// NEIGHBORHOOD MAJORITY FILTER SETTINGS
RadiusType rad;
rad[0] = GetParameterInt("ip.radiusx");
rad[1] = GetParameterInt("ip.radiusy");
BallStructuringType seBall;
seBall.SetRadius(rad);
seBall.CreateStructuringElement();
m_NeighMajVotingFilter->SetKernel(seBall);
m_NeighMajVotingFilter->SetInput(inImage);
m_NeighMajVotingFilter->SetNoDataValue(GetParameterInt("ip.nodatavalue"));
m_NeighMajVotingFilter->SetUndefinedValue(GetParameterInt("ip.undefinedvalue"));
// Set to UnDefinedValue if NOT unique Majority Voting
if (IsParameterEnabled("ip.suvbool")) {
m_NeighMajVotingFilter->SetKeepOriginalLabelBool(false);
}
// Keep original label value if NOT unique Majority Voting
else {
m_NeighMajVotingFilter->SetKeepOriginalLabelBool(true);
}
/** REGULARIZATION OF CLASSIFICATION */
SetParameterOutputImage<OutputLabelImageType>("io.out", m_NeighMajVotingFilter->GetOutput());
}//END DoExecute()
//NEIGHBORHOOD MAJORITY FILTER
NeighborhoodMajorityVotingFilterType::Pointer m_NeighMajVotingFilter;
}; //END class ClassificationRegularisationMajorityVoting
}//END namespace wrapper
}//END namespace otb
/** REGULARIZATION OF CLASSIFICATION */
SetParameterOutputImage<OutputLabelImageType>("io.out", m_NeighMajVotingFilter->GetOutput());
}//END DoExecute()
OTB_APPLICATION_EXPORT(otb::Wrapper::ClassificationRegularisationMajorityVoting)
NeighborhoodMajorityVotingFilterType::Pointer m_NeighMajVotingFilter;
};//END class ClassificationRegularisationMajorityVoting
}//END namespace wrapper
}//END namespace otb
OTB_APPLICATION_EXPORT(otb::Wrapper::ClassificationRegularisationMajorityVoting)
......@@ -48,8 +48,6 @@ 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> >
class ITK_EXPORT NeighborhoodMajorityVotingImageFilter :
public itk::MorphologyImageFilter<TInputImage, TOutputImage, TKernel>
......@@ -67,7 +65,7 @@ public:
/** Runtime information support. */
itkTypeMacro(NeighborhoodMajorityVotingImageFilter,
itk::MorphologyImageFilter);
itk::MorphologyImageFilter);
/** Declaration of pixel type. */
typedef typename Superclass::PixelType PixelType;
......
......@@ -25,90 +25,98 @@
#include "otbNeighborhoodMajorityVotingImageFilter.h"
namespace otb {
namespace otb
{
/**
* Constructor
*/
template<class TInputImage, class TOutputImage, class TKernel>
NeighborhoodMajorityVotingImageFilter<TInputImage, TOutputImage, TKernel>::NeighborhoodMajorityVotingImageFilter() {
this->SetNoDataValue(itk::NumericTraits<PixelType>::NonpositiveMin());
this->SetUndefinedValue(itk::NumericTraits<PixelType>::NonpositiveMin());
m_KeepOriginalLabelBool = true;
NeighborhoodMajorityVotingImageFilter<TInputImage, TOutputImage, TKernel>::NeighborhoodMajorityVotingImageFilter()
{
this->SetNoDataValue(itk::NumericTraits<PixelType>::NonpositiveMin()); //m_NoDataValue = 0
this->SetUndefinedValue(itk::NumericTraits<PixelType>::NonpositiveMin()); //m_UndefinedValue = 0
this->SetKeepOriginalLabelBool(true); //m_KeepOriginalLabelBool = true
}
template<class TInputImage, class TOutputImage, class TKernel>
typename NeighborhoodMajorityVotingImageFilter<TInputImage, TOutputImage,
TKernel>::PixelType NeighborhoodMajorityVotingImageFilter<TInputImage,
TOutputImage, TKernel>::Evaluate(const NeighborhoodIteratorType &nit,
const KernelIteratorType kernelBegin,
const KernelIteratorType kernelEnd) {
/*
KernelIteratorType kernelBegin_ = this->GetKernel().Begin();
KernelIteratorType kernelEnd_ = this->GetKernel().End();
*/
PixelType majorityLabel = 0; //Value of the more representative pixels in the neighborhood
unsigned int majorityFreq = 0; //Number of pixels with the more representative value (majorityLabel) in the neighborhood
unsigned int i;
KernelIteratorType kernel_it;
std::map<PixelType, unsigned int> histoNeigh;
PixelType centerPixel = nit.GetCenterPixel();
if (centerPixel != m_NoDataValue){
for (i = 0, kernel_it = kernelBegin; kernel_it < kernelEnd; ++kernel_it, ++i) {
// if structuring element is positive, use the pixel under that element
// in the image
PixelType label = nit.GetPixel(i);
if ((*kernel_it > itk::NumericTraits<KernelPixelType>::Zero) && (label != m_NoDataValue)) {
// note we use GetPixel() on the SmartNeighborhoodIterator to
// respect boundary conditions
//If the current label has already been added to the histogram histoNeigh
if(histoNeigh.count(label) > 0){
histoNeigh[label]++;
}
else{
histoNeigh[label] = 1;
}
}
}
//Extraction of the more representative Label in the neighborhood (majorityLabel) and its Frequency (majorityFreq)
typename std::map<PixelType, unsigned int>::iterator histoIt;
for (histoIt = histoNeigh.begin(); histoIt != histoNeigh.end(); ++histoIt) {
if (histoIt->second > majorityFreq) {
majorityFreq = histoIt->second; //Frequency
majorityLabel = histoIt->first; //Label
}
}
//If the majorityLabel is NOT unique in the neighborhood
for (histoIt = histoNeigh.begin(); histoIt != histoNeigh.end(); ++histoIt) {
if ( (histoIt->second == majorityFreq) && (histoIt->first != majorityLabel) ) {
if(m_KeepOriginalLabelBool == true) {
majorityLabel = centerPixel;
}
else {
majorityLabel = m_UndefinedValue;
}
break;
}
}
}//END if (centerPixel != m_NoDataValue)
//If (centerPixel == m_NoDataValue)
else{
majorityLabel = m_NoDataValue;
}
return majorityLabel;
TKernel>::PixelType NeighborhoodMajorityVotingImageFilter<TInputImage,
TOutputImage, TKernel>::Evaluate(const NeighborhoodIteratorType &nit,
const KernelIteratorType kernelBegin,
const KernelIteratorType kernelEnd)
{
PixelType majorityLabel = 0; //Value of the more representative pixels in the neighborhood
unsigned int majorityFreq = 0; //Number of pixels with the more representative value (majorityLabel) in the neighborhood
unsigned int i;
KernelIteratorType kernel_it;
std::map<PixelType, unsigned int> histoNeigh;
PixelType centerPixel = nit.GetCenterPixel();
if (centerPixel != m_NoDataValue)
{
for (i = 0, kernel_it = kernelBegin; kernel_it < kernelEnd; ++kernel_it, ++i)
{
// if structuring element is positive, use the pixel under that element
// in the image
PixelType label = nit.GetPixel(i);
if ((*kernel_it > itk::NumericTraits<KernelPixelType>::Zero) && (label != m_NoDataValue))
{
// note we use GetPixel() on the SmartNeighborhoodIterator to
// respect boundary conditions
//If the current label has already been added to the histogram histoNeigh
if(histoNeigh.count(label) > 0)
{
histoNeigh[label]++;
}
else
{
histoNeigh[label] = 1;
}
}
}
//Extraction of the more representative Label in the neighborhood (majorityLabel) and its Frequency (majorityFreq)
typename std::map<PixelType, unsigned int>::iterator histoIt;
for (histoIt = histoNeigh.begin(); histoIt != histoNeigh.end(); ++histoIt)
{
if (histoIt->second > majorityFreq)
{
majorityFreq = histoIt->second; //Frequency
majorityLabel = histoIt->first; //Label
}
}
//If the majorityLabel is NOT unique in the neighborhood
for (histoIt = histoNeigh.begin(); histoIt != histoNeigh.end(); ++histoIt)
{
if ( (histoIt->second == majorityFreq) && (histoIt->first != majorityLabel) )
{
if(m_KeepOriginalLabelBool == true)
{
majorityLabel = centerPixel;
}
else
{
majorityLabel = m_UndefinedValue;
}
break;
}
}
}//END if (centerPixel != m_NoDataValue)
//If (centerPixel == m_NoDataValue)
else
{
majorityLabel = m_NoDataValue;
}
return majorityLabel;
}
} // end namespace otb
......
......@@ -424,9 +424,9 @@ OTB_TEST_APPLICATION(NAME apTvClassificationRegMajorityVoting
APP ClassificationRegularisationMajorityVoting
OPTIONS -io.in ${INPUTDATA}/Classification/QB_1_ortho_4Cls_N_Classified_OTB.tif
-io.out ${TEMP}/QB_1_ortho_4Cls_N_Classified_OTB_NMV_x2_y5_nodv10_undv7_APP.tif
-ip.suvbool true
-ip.radiusx 2
-ip.radiusy 5
-ip.suvbool true
-ip.nodatavalue 10
-ip.undefinedvalue 7
VALID --compare-image ${NOTOL}
......
......@@ -26,24 +26,23 @@ int otbNeighborhoodMajorityVotingImageFilterNew(int argc, char* argv[])
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;
//NEIGHBORHOOD MAJORITY FILTER TYPES
typedef otb::NeighborhoodMajorityVotingImageFilter<InputLabelImageType, OutputLabelImageType, StructuringType> NeighborhoodMajorityVotingFilter3ArgsType;
typedef otb::NeighborhoodMajorityVotingImageFilter<InputLabelImageType, OutputLabelImageType> NeighborhoodMajorityVotingFilter2ArgsType;
typedef otb::NeighborhoodMajorityVotingImageFilter<InputLabelImageType> NeighborhoodMajorityVotingFilter1ArgType;
//NEIGHBORHOOD MAJORITY FILTERS
NeighborhoodMajorityVotingFilter3ArgsType::Pointer NeighMajVotingFilter3Args = NeighborhoodMajorityVotingFilter3ArgsType::New();
NeighborhoodMajorityVotingFilter2ArgsType::Pointer NeighMajVotingFilter2Args = NeighborhoodMajorityVotingFilter2ArgsType::New();
NeighborhoodMajorityVotingFilter1ArgType::Pointer NeighMajVotingFilter1Arg = NeighborhoodMajorityVotingFilter1ArgType::New();
std::cout << NeighMajVotingFilter3Args << std::endl;
std::cout << std::endl;
std::cout << NeighMajVotingFilter2Args << std::endl;
......
......@@ -35,70 +35,77 @@ int otbNeighborhoodMajorityVotingImageFilterTest(int argc, char* argv[])
typedef unsigned char InputLabelPixelType; //8 bits
typedef unsigned char OutputLabelPixelType; //8 bits
const unsigned int Dimension = 2;
typedef otb::Image<InputLabelPixelType, Dimension> InputLabelImageType;
typedef otb::Image<OutputLabelPixelType, Dimension> OutputLabelImageType;
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 FILTER TYPE
typedef otb::NeighborhoodMajorityVotingImageFilter<InputLabelImageType, OutputLabelImageType, StructuringType> NeighborhoodMajorityVotingFilterType;
const char * inputFileName = argv[1];
const char * outputFileName = argv[2];
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(inputFileName);
//NEIGHBORHOOD MAJORITY FILTER
NeighborhoodMajorityVotingFilterType::Pointer NeighMajVotingFilter;
NeighMajVotingFilter = NeighborhoodMajorityVotingFilterType::New();
NeighMajVotingFilter->SetInput(reader->GetOutput());
BallStructuringType seBall;
RadiusType rad;
if (argc >= 3) {
std::string KeepOriginalLabelBoolStr = argv[3];
if (KeepOriginalLabelBoolStr.compare("true") == 0) {
NeighMajVotingFilter->SetKeepOriginalLabelBool(true);
}
else {
NeighMajVotingFilter->SetKeepOriginalLabelBool(false);
}
if (argc >= 5) {
rad[0] = atoi(argv[4]);
rad[1] = atoi(argv[5]);
if (argc >= 6) {
NeighMajVotingFilter->SetNoDataValue(atoi(argv[6]));
if (argc >= 7) {
NeighMajVotingFilter->SetUndefinedValue(atoi(argv[7]));
}
}
}
else {