1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 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 124 125 126 127 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 154 155 156 157 158 159 160 161 162 163 164 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 199 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 243 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
|
/*=========================================================================
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
//
// This example illustrates the Change Detector framework implemented
// in OTB. This framework uses the generic programming approach. All
// change detection filters are
// \doxygen{otb}{BinaryFunctorNeighborhoodImageFilter}s, that is, they
// are filters taking two images as input and providing one image as
// output. The change detection computation itself is performed on a
// the neighborhood of each pixel of the input images.
//
// The first step required to build a change detection filter is to
// include the header of the parent class.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
#include "otbBinaryFunctorNeighborhoodImageFilter.h"
// Software Guide : EndCodeSnippet
#include "otbImageFileReader.h"
#include "otbImageFileWriter.h"
#include "itkUnaryFunctorImageFilter.h"
#include "itkRescaleIntensityImageFilter.h"
#include "otbImage.h"
#include "otbCommandProgressUpdate.h"
// Software Guide : BeginLatex
//
// The change
// detection operation itself is one of the templates of the change
// detection filters and takes the form of a function, that is,
// something accepting the syntax \code{foo()}. This can be
// implemented using classical C/C++ functions, but it is preferable
// to implement it using C++ functors. These are classical C++ classes
// which overload the \code{()} operator. This allows using them with
// the same syntax as C/C++ functions.
//
// Since change detectors operate on neighborhoods, the functor
// call will take 2 arguments which are
// \doxygen{itk}{ConstNeighborhoodIterator}s.
//
// The change detector functor is templated over the types of the
// input iterators and the output result type. The core of the change
// detection is implemented in the \code{operator()} section.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
template<class TInput1, class TInput2, class TOutput>
class MyChangeDetector
{
public:
// The constructor and destructor.
MyChangeDetector() {}
~MyChangeDetector() {}
// Change detection operation
inline TOutput operator ()(const TInput1& itA,
const TInput2& itB)
{
TOutput result = 0.0;
for (unsigned long pos = 0; pos < itA.Size(); ++pos)
{
result += static_cast<TOutput>(itA.GetPixel(pos) - itB.GetPixel(pos));
}
return static_cast<TOutput>(result / itA.Size());
}
};
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The interest of using functors is that complex operations can be
// performed using internal \code{protected} class methods and that
// class variables can be used to store information so different pixel
// locations can access to results of previous computations.
//
// The next step is the definition of the change detector filter. As
// stated above, this filter will inherit from
// \doxygen{otb}{BinaryFunctorNeighborhoodImageFilter} which is
// templated over the 2 input image types, the output image type and
// the functor used to perform the change detection operation.
//
// Inside the class only a few \code{typedef}s and the constructors
// and destructors have to be declared.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
template <class TInputImage1, class TInputImage2, class TOutputImage>
class ITK_EXPORT MyChangeDetectorImageFilter :
public otb::BinaryFunctorNeighborhoodImageFilter<
TInputImage1, TInputImage2, TOutputImage,
MyChangeDetector<
typename itk::ConstNeighborhoodIterator<TInputImage1>,
typename itk::ConstNeighborhoodIterator<TInputImage2>,
typename TOutputImage::PixelType> >
{
public:
/** Standard class typedefs. */
typedef MyChangeDetectorImageFilter Self;
typedef typename otb::BinaryFunctorNeighborhoodImageFilter<
TInputImage1, TInputImage2, TOutputImage,
MyChangeDetector<
typename itk::ConstNeighborhoodIterator<TInputImage1>,
typename itk::ConstNeighborhoodIterator<TInputImage2>,
typename TOutputImage::PixelType>
> Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
/** Method for creation through the object factory. */
itkNewMacro(Self);
protected:
MyChangeDetectorImageFilter() {}
~MyChangeDetectorImageFilter() ITK_OVERRIDE {}
private:
MyChangeDetectorImageFilter(const Self &); //purposely not implemented
void operator =(const Self&); //purposely not implemented
};
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Pay attention to the fact that no \code{.txx} file is needed, since
// filtering operation is implemented in the
// \doxygen{otb}{BinaryFunctorNeighborhoodImageFilter} class. So all
// the algorithmics part is inside the functor.
//
// We can now write a program using the change detector.
//
// SoftwareGuide : EndLatex
int main(int argc, char* argv[])
{
if (argc < 5)
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0] <<
" inputImageFile1 inputImageFile2 radius outputImageFile " << std::endl;
return -1;
}
// Define the dimension of the images
const unsigned int Dimension = 2;
// Software Guide : BeginLatex
//
// As usual, we start by defining the image types. The internal
// computations will be performed with floating point precision,
// while the output image will be stored using one byte per pixel.
//
// SoftwareGuide : EndLatex
// Software Guide : BeginCodeSnippet
typedef float InternalPixelType;
typedef unsigned char OutputPixelType;
typedef otb::Image<InternalPixelType, Dimension> InputImageType1;
typedef otb::Image<InternalPixelType, Dimension> InputImageType2;
typedef otb::Image<InternalPixelType, Dimension> ChangeImageType;
typedef otb::Image<OutputPixelType, Dimension> OutputImageType;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We declare the readers, the writer, but also the
// \doxygen{itk}{RescaleIntensityImageFilter} which will be used to
// rescale the result before writing it to a file.
//
// SoftwareGuide : EndLatex
// Software Guide : BeginCodeSnippet
typedef otb::ImageFileReader<InputImageType1> ReaderType1;
typedef otb::ImageFileReader<InputImageType2> ReaderType2;
typedef otb::ImageFileWriter<OutputImageType> WriterType;
typedef itk::RescaleIntensityImageFilter<ChangeImageType,
OutputImageType> RescalerType;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The next step is declaring the filter for the change detection.
//
// SoftwareGuide : EndLatex
// Software Guide : BeginCodeSnippet
typedef MyChangeDetectorImageFilter<InputImageType1, InputImageType2,
ChangeImageType> FilterType;
// Software Guide : EndCodeSnippet
ReaderType1::Pointer reader1 = ReaderType1::New();
ReaderType2::Pointer reader2 = ReaderType2::New();
WriterType::Pointer writer = WriterType::New();
FilterType::Pointer filter = FilterType::New();
RescalerType::Pointer rescaler = RescalerType::New();
const char * inputFilename1 = argv[1];
const char * inputFilename2 = argv[2];
const char * outputFilename = argv[4];
// Software Guide : BeginLatex
//
// We connect the pipeline.
//
// SoftwareGuide : EndLatex
// Software Guide : BeginCodeSnippet
reader1->SetFileName(inputFilename1);
reader2->SetFileName(inputFilename2);
writer->SetFileName(outputFilename);
rescaler->SetOutputMinimum(itk::NumericTraits<OutputPixelType>::min());
rescaler->SetOutputMaximum(itk::NumericTraits<OutputPixelType>::max());
filter->SetInput1(reader1->GetOutput());
filter->SetInput2(reader2->GetOutput());
filter->SetRadius(atoi(argv[3]));
rescaler->SetInput(filter->GetOutput());
writer->SetInput(rescaler->GetOutput());
// Software Guide : EndCodeSnippet
typedef otb::CommandProgressUpdate<FilterType> CommandType;
CommandType::Pointer observer = CommandType::New();
filter->AddObserver(itk::ProgressEvent(), observer);
try
{
writer->Update();
}
catch (itk::ExceptionObject& err)
{
std::cout << "ExceptionObject caught !" << std::endl;
std::cout << err << std::endl;
return -1;
}
// Software Guide : BeginLatex
//
// And that is all.
//
// SoftwareGuide : EndLatex
return EXIT_SUCCESS;
}
|