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

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

parent 202a195f
......@@ -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 {
rad[0] = 1;
rad[1] = 1;
}
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
{
rad[0] = 1;
rad[1] = 1;
}
}
seBall.SetRadius(rad);
seBall.CreateStructuringElement();
NeighMajVotingFilter->SetKernel(seBall);
WriterType::Pointer writer = WriterType::New();
writer->SetFileName(outputFileName);
writer->SetInput(NeighMajVotingFilter->GetOutput());
......
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