otbBinaryMorphologicalOperation.cxx 11.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*=========================================================================

  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.

=========================================================================*/

#include "otbWrapperApplication.h"
#include "otbWrapperApplicationFactory.h"

#include "itkBinaryBallStructuringElement.h"
#include "itkBinaryCrossStructuringElement.h"

25 26
#include "itkBinaryDilateImageFilter.h"
#include "itkBinaryErodeImageFilter.h"
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#include "itkBinaryMorphologicalOpeningImageFilter.h"
#include "itkBinaryMorphologicalClosingImageFilter.h"

#include "otbMultiToMonoChannelExtractROI.h"
#include "otbImageList.h"
#include "otbImageListToVectorImageFilter.h"

#include "itkTimeProbe.h"

namespace otb
{
namespace Wrapper
{

class BinaryMorphologicalOperation : public Application
{
public:
/** Standard class typedefs. */
typedef BinaryMorphologicalOperation          Self;
typedef Application                   Superclass;
typedef itk::SmartPointer<Self>       Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;

typedef MultiToMonoChannelExtractROI<FloatVectorImageType::InternalPixelType,FloatVectorImageType::InternalPixelType>
ExtractorFilterType;

typedef itk::BinaryBallStructuringElement<FloatImageType::PixelType, 2>        BallStructuringType;
typedef BallStructuringType::RadiusType                                        RadiusType;
typedef BallStructuringType::Superclass                                        StructuringType;
typedef itk::BinaryCrossStructuringElement<FloatImageType::PixelType, 2>       CrossStructuringType;

typedef itk::BinaryDilateImageFilter<FloatImageType, FloatImageType, StructuringType>
                                                                               DilateFilterType;
typedef itk::BinaryErodeImageFilter<FloatImageType, FloatImageType, StructuringType>
                                                                               ErodeFilterType;
typedef itk::BinaryMorphologicalOpeningImageFilter<FloatImageType, FloatImageType, StructuringType>
                                                                               OpeningFilterType;
typedef itk::BinaryMorphologicalClosingImageFilter<FloatImageType, FloatImageType, StructuringType>
                                                                               ClosingFilterType;

typedef ImageList<FloatImageType>                                              ImageListType;
typedef ImageListToVectorImageFilter<ImageListType, FloatVectorImageType>      ImageListToVectorImageFilterType;

/** Standard macro */
itkNewMacro(Self);
itkTypeMacro(BinaryMorphologicalOperation, otb::Application);

private:

76
void DoInit() ITK_OVERRIDE
77 78 79 80 81 82
{
SetName("BinaryMorphologicalOperation");
SetDescription("Performs morphological operations on an input image channel");

// Documentation
SetDocName("Binary Morphological Operation");
83
SetDocLongDescription("This application performs binary morphological operations on a mono band image");
84 85 86 87 88
SetDocLimitations("None");
SetDocAuthors("OTB-Team");
SetDocSeeAlso("itkBinaryDilateImageFilter, itkBinaryErodeImageFilter, itkBinaryMorphologicalOpeningImageFilter and itkBinaryMorphologicalClosingImageFilter classes");

AddDocTag(Tags::FeatureExtraction);
89
AddDocTag("MorphologicalOperations");
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

AddParameter(ParameterType_InputImage, "in",  "Input Image");
SetParameterDescription("in", "The input image to be filtered.");

AddParameter(ParameterType_OutputImage, "out", "Feature Output Image");
SetParameterDescription("out", "Output image containing the filtered output image.");

AddParameter(ParameterType_Int,  "channel",  "Selected Channel");
SetParameterDescription("channel", "The selected channel index");
SetDefaultParameterInt("channel", 1);
SetMinimumParameterIntValue("channel", 1);

AddRAMParameter();

AddParameter(ParameterType_Choice, "structype", "Structuring Element Type");
SetParameterDescription("structype", "Choice of the structuring element type");
//Ball
AddChoice("structype.ball", "Ball");
AddParameter(ParameterType_Int, "structype.ball.xradius", "The Structuring Element X Radius");
SetParameterDescription("structype.ball.xradius", "The Structuring Element X Radius");
SetDefaultParameterInt("structype.ball.xradius", 5);
AddParameter(ParameterType_Int, "structype.ball.yradius", "The Structuring Element Y Radius");
SetParameterDescription("structype.ball.yradius", "The Structuring Element Y Radius");
SetDefaultParameterInt("structype.ball.yradius", 5);
//Cross
AddChoice("structype.cross", "Cross");

AddParameter(ParameterType_Choice, "filter", "Morphological Operation");
SetParameterDescription("filter", "Choice of the morphological operation");
//Dilate
AddChoice("filter.dilate", "Dilate");
AddParameter(ParameterType_Float, "filter.dilate.foreval", "Foreground Value");
SetParameterDescription("filter.dilate.foreval", "The Foreground Value");
SetDefaultParameterFloat("filter.dilate.foreval", 1.0);
124 125 126 127
AddParameter(ParameterType_Float, "filter.dilate.backval", "Background Value");
SetParameterDescription("filter.dilate.backval", "The Background Value");
SetDefaultParameterFloat("filter.dilate.backval", 0.0);

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
//Erode
AddChoice("filter.erode", "Erode");
AddParameter(ParameterType_Float, "filter.erode.foreval", "Foreground Value");
SetParameterDescription("filter.erode.foreval", "The Foreground Value");
SetDefaultParameterFloat("filter.erode.foreval", 1.0);
AddParameter(ParameterType_Float, "filter.erode.backval", "Background Value");
SetParameterDescription("filter.erode.backval", "The Background Value");
SetDefaultParameterFloat("filter.erode.backval", 0.0);
//Opening
AddChoice("filter.opening", "Opening");
AddParameter(ParameterType_Float, "filter.opening.foreval", "Foreground Value");
SetParameterDescription("filter.opening.foreval", "The Foreground Value");
SetDefaultParameterFloat("filter.opening.foreval", 1.0);
AddParameter(ParameterType_Float, "filter.opening.backval", "Background Value");
SetParameterDescription("filter.opening.backval", "The Background Value");
SetDefaultParameterFloat("filter.opening.backval", 0.0);
//Closing
AddChoice("filter.closing", "Closing");
AddParameter(ParameterType_Float, "filter.closing.foreval", "Foreground Value");
SetParameterDescription("filter.closing.foreval", "The Foreground Value");
SetDefaultParameterFloat("filter.closing.foreval", 1.0);

// Doc example parameter settings
SetDocExampleParameterValue("in", "qb_RoadExtract.tif");
SetDocExampleParameterValue("out", "opened.tif");
SetDocExampleParameterValue("channel", "1");
154 155
SetDocExampleParameterValue("structype.ball.xradius", "5");
SetDocExampleParameterValue("structype.ball.yradius", "5");
156 157 158
SetDocExampleParameterValue("filter", "erode");
}

159
void DoUpdateParameters() ITK_OVERRIDE
160 161 162 163
{
  // Nothing to do here : all parameters are independent
}

164
void DoExecute() ITK_OVERRIDE
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
{
  FloatVectorImageType::Pointer inImage = GetParameterImage("in");
  inImage->UpdateOutputInformation();
  int nBComp = inImage->GetNumberOfComponentsPerPixel();

  if( GetParameterInt("channel") > nBComp )
    {
    itkExceptionMacro(<< "The specified channel index is invalid.");
    }

  m_ExtractorFilter = ExtractorFilterType::New();
  m_ExtractorFilter->SetInput(inImage);
  m_ExtractorFilter->SetStartX(inImage->GetLargestPossibleRegion().GetIndex(0));
  m_ExtractorFilter->SetStartY(inImage->GetLargestPossibleRegion().GetIndex(1));
  m_ExtractorFilter->SetSizeX(inImage->GetLargestPossibleRegion().GetSize(0));
  m_ExtractorFilter->SetSizeY(inImage->GetLargestPossibleRegion().GetSize(1));
  m_ExtractorFilter->SetChannel(GetParameterInt("channel"));
  m_ExtractorFilter->UpdateOutputInformation();

  if(GetParameterString("structype") == "ball")
    {
    BallStructuringType se;
    RadiusType rad;
    rad[0] = this->GetParameterInt("structype.ball.xradius");
    rad[1] = this->GetParameterInt("structype.ball.yradius");
    se.SetRadius(rad);
    se.CreateStructuringElement();

    if(GetParameterString("filter") == "dilate")
      {
      m_DilFilter = DilateFilterType::New();
      m_DilFilter->SetKernel(se);
      m_DilFilter->SetInput(m_ExtractorFilter->GetOutput());
      m_DilFilter->SetForegroundValue(GetParameterFloat("filter.dilate.foreval"));
199
      m_DilFilter->SetBackgroundValue(GetParameterFloat("filter.dilate.backval"));
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
      SetParameterOutputImage("out", m_DilFilter->GetOutput());
      }

    if(GetParameterString("filter") == "erode")
      {
      m_EroFilter = ErodeFilterType::New();
      m_EroFilter->SetKernel(se);
      m_EroFilter->SetInput(m_ExtractorFilter->GetOutput());
      m_EroFilter->SetForegroundValue(GetParameterFloat("filter.erode.foreval"));
      m_EroFilter->SetBackgroundValue(GetParameterFloat("filter.erode.backval"));
      SetParameterOutputImage("out", m_EroFilter->GetOutput());
      }

    if(GetParameterString("filter") == "opening")
      {
      m_OpeFilter = OpeningFilterType::New();
      m_OpeFilter->SetKernel(se);
      m_OpeFilter->SetInput(m_ExtractorFilter->GetOutput());
      m_OpeFilter->SetForegroundValue(GetParameterFloat("filter.opening.foreval"));
      m_OpeFilter->SetBackgroundValue(GetParameterFloat("filter.opening.backval"));
      SetParameterOutputImage("out", m_OpeFilter->GetOutput());
      }

    if(GetParameterString("filter") == "closing")
      {
      m_CloFilter = ClosingFilterType::New();
      m_CloFilter->SetKernel(se);
      m_CloFilter->SetInput(m_ExtractorFilter->GetOutput());
      m_CloFilter->SetForegroundValue(GetParameterFloat("filter.closing.foreval"));
      SetParameterOutputImage("out", m_CloFilter->GetOutput());
      }
    }
  if(GetParameterString("structype") == "cross")
    {
    CrossStructuringType se;
    se.CreateStructuringElement();

    if(GetParameterString("filter") == "dilate")
      {
      m_DilFilter = DilateFilterType::New();
      m_DilFilter->SetKernel(se);
      m_DilFilter->SetInput(m_ExtractorFilter->GetOutput());
      m_DilFilter->SetForegroundValue(GetParameterFloat("filter.dilate.foreval"));
243
      m_DilFilter->SetBackgroundValue(GetParameterFloat("filter.dilate.backval"));
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
      SetParameterOutputImage("out", m_DilFilter->GetOutput());
      }

    if(GetParameterString("filter") == "erode")
      {
      m_EroFilter = ErodeFilterType::New();
      m_EroFilter->SetKernel(se);
      m_EroFilter->SetInput(m_ExtractorFilter->GetOutput());
      m_EroFilter->SetForegroundValue(GetParameterFloat("filter.erode.foreval"));
      m_EroFilter->SetBackgroundValue(GetParameterFloat("filter.erode.backval"));
      SetParameterOutputImage("out", m_EroFilter->GetOutput());
      }

    if(GetParameterString("filter") == "opening")
      {
      m_OpeFilter = OpeningFilterType::New();
      m_OpeFilter->SetKernel(se);
      m_OpeFilter->SetInput(m_ExtractorFilter->GetOutput());
      m_OpeFilter->SetForegroundValue(GetParameterFloat("filter.opening.foreval"));
      m_OpeFilter->SetBackgroundValue(GetParameterFloat("filter.opening.backval"));
      SetParameterOutputImage("out", m_OpeFilter->GetOutput());
      }

    if(GetParameterString("filter") == "closing")
      {
      m_CloFilter = ClosingFilterType::New();
      m_CloFilter->SetKernel(se);
      m_CloFilter->SetInput(m_ExtractorFilter->GetOutput());
      m_CloFilter->SetForegroundValue(GetParameterFloat("filter.closing.foreval"));
      SetParameterOutputImage("out", m_CloFilter->GetOutput());
      }
    }
}

ExtractorFilterType::Pointer                m_ExtractorFilter;

DilateFilterType::Pointer                   m_DilFilter;
ErodeFilterType::Pointer                    m_EroFilter;
OpeningFilterType::Pointer                  m_OpeFilter;
ClosingFilterType::Pointer                  m_CloFilter;
};
}
}

OTB_APPLICATION_EXPORT(otb::Wrapper::BinaryMorphologicalOperation)