Commit 15aecc1a authored by Victor Poughon's avatar Victor Poughon

DOC: review BandMathFilterExample

parent e15f2620
This diff is collapsed.
...@@ -22,22 +22,6 @@ ...@@ -22,22 +22,6 @@
./BandMathFilterExample Input/qb_RoadExtract.tif Output/RoadExtractBandMath.tif Output/qb_BandMath-pretty.jpg ./BandMathFilterExample Input/qb_RoadExtract.tif Output/RoadExtractBandMath.tif Output/qb_BandMath-pretty.jpg
*/ */
// This filter is based on the mathematical parser library muParser.
// The built in functions and operators list is available at:
// http://muparser.sourceforge.net/mup_features.html.
//
// In order to use this filter, at least one input image should be
// set. An associated variable name can be specified or not by using
// the corresponding SetNthInput method. For the nth input image, if
// no associated variable name has been specified, a default variable
// name is given by concatenating the letter "b" (for band) and the
// corresponding input index.
//
// The next step is to set the expression according to the variable
// names. For example, in the default case with three input images the
// following expression is valid: ``(b1+b2)*b3``.
#include "itkMacro.h" #include "itkMacro.h"
#include <iostream> #include <iostream>
...@@ -65,11 +49,10 @@ int main(int argc, char* argv[]) ...@@ -65,11 +49,10 @@ int main(int argc, char* argv[])
return EXIT_FAILURE; return EXIT_FAILURE;
} }
// We start by the typedef needed for reading and // We start by the typedefs needed for reading and
// writing the images. The BandMathImageFilter class // writing the images. The BandMathImageFilter class
// works with Image as input, so we need to define additional // works with Image as input, so we need to define additional
// filters to extract each layer of the multispectral image. // filters to extract each layer of the multispectral image.
typedef double PixelType; typedef double PixelType;
typedef otb::VectorImage<PixelType, 2> InputImageType; typedef otb::VectorImage<PixelType, 2> InputImageType;
typedef otb::Image<PixelType, 2> OutputImageType; typedef otb::Image<PixelType, 2> OutputImageType;
...@@ -78,13 +61,12 @@ int main(int argc, char* argv[]) ...@@ -78,13 +61,12 @@ int main(int argc, char* argv[])
typedef otb::ImageFileReader<InputImageType> ReaderType; typedef otb::ImageFileReader<InputImageType> ReaderType;
typedef otb::ImageFileWriter<OutputImageType> WriterType; typedef otb::ImageFileWriter<OutputImageType> WriterType;
// We can now define the type for the filter: // We can now define the type for the filter
typedef otb::BandMathImageFilter<OutputImageType> FilterType; typedef otb::BandMathImageFilter<OutputImageType> FilterType;
// We instantiate the filter, the reader, and the writer: // We instantiate the filter, the reader, and the writer
ReaderType::Pointer reader = ReaderType::New(); ReaderType::Pointer reader = ReaderType::New();
WriterType::Pointer writer = WriterType::New(); WriterType::Pointer writer = WriterType::New();
FilterType::Pointer filter = FilterType::New(); FilterType::Pointer filter = FilterType::New();
writer->SetInput(filter->GetOutput()); writer->SetInput(filter->GetOutput());
...@@ -93,9 +75,9 @@ int main(int argc, char* argv[]) ...@@ -93,9 +75,9 @@ int main(int argc, char* argv[])
reader->UpdateOutputInformation(); reader->UpdateOutputInformation();
// We now need to extract each band from the input \doxygen{otb}{VectorImage}, // We now need to extract each band from the input VectorImage,
// it illustrates the use of the \doxygen{otb}{VectorImageToImageList}. // it illustrates the use of the VectorImageToImageList.
// Each extracted layer is an input to the \doxygen{otb}{BandMathImageFilter}: // Each extracted layer is an input to the BandMathImageFilter
VectorImageToImageListType::Pointer imageList = VectorImageToImageListType::New(); VectorImageToImageListType::Pointer imageList = VectorImageToImageListType::New();
imageList->SetInput(reader->GetOutput()); imageList->SetInput(reader->GetOutput());
...@@ -111,8 +93,8 @@ int main(int argc, char* argv[]) ...@@ -111,8 +93,8 @@ int main(int argc, char* argv[])
// Now we can define the mathematical expression to perform on the layers (b1, b2, b3, b4). // Now we can define the mathematical expression to perform on the layers (b1, b2, b3, b4).
// The filter takes advantage of the parsing capabilities of the muParser library and // The filter takes advantage of the parsing capabilities of the muParser library and
// allows setting the expression as on a digital calculator. // allows setting the expression as on a digital calculator.
//
// The expression below returns 255 if the ratio $(NIR-RED)/(NIR+RED)$ is greater than 0.4 and 0 if not. // The expression below returns 255 if the ratio (NIR-RED)/(NIR+RED) is greater than 0.4 and 0 if not.
filter->SetExpression("if((b4-b3)/(b4+b3) > 0.4, 255, 0)"); filter->SetExpression("if((b4-b3)/(b4+b3) > 0.4, 255, 0)");
#ifdef OTB_MUPARSER_HAS_CXX_LOGICAL_OPERATORS #ifdef OTB_MUPARSER_HAS_CXX_LOGICAL_OPERATORS
...@@ -125,19 +107,7 @@ int main(int argc, char* argv[]) ...@@ -125,19 +107,7 @@ int main(int argc, char* argv[])
writer->Update(); writer->Update();
// The muParser library also provides the possibility to extend existing built-in functions. For example, // The muParser library also provides the possibility to extend existing built-in functions. For example,
// you can use the OTB expression "ndvi(b3, b4)" with the filter. In this instance, the mathematical expression would be // you can use the OTB expression "ndvi(b3, b4)" with the filter. In this instance, the mathematical expression would be "if(ndvi(b3, b4)>0.4, 255, 0)", which would return the same result.
// \textit{if($ndvi(b3, b4)>0.4$, 255, 0)}, which would return the same result.
// Figure~\ref{fig:BandMathImageFilter} shows the result of the threshold applied to the NDVI index
// of a Quickbird image.
// \begin{figure}
// \center
// \includegraphics[width=0.45\textwidth]{qb_ExtractRoad_pretty.eps}
// \includegraphics[width=0.45\textwidth]{qb_BandMath-pretty.eps}
// \itkcaption[Band Math]{From left to right:
// Original image, thresholded NDVI index.}
// \label{fig:BandMathImageFilter}
// \end{figure}
typedef otb::Image<unsigned char, 2> OutputPrettyImageType; typedef otb::Image<unsigned char, 2> OutputPrettyImageType;
typedef otb::ImageFileWriter<OutputPrettyImageType> PrettyImageFileWriterType; typedef otb::ImageFileWriter<OutputPrettyImageType> PrettyImageFileWriterType;
...@@ -151,6 +121,4 @@ int main(int argc, char* argv[]) ...@@ -151,6 +121,4 @@ int main(int argc, char* argv[])
prettyWriter->SetFileName(argv[3]); prettyWriter->SetFileName(argv[3]);
prettyWriter->Update(); prettyWriter->Update();
return EXIT_SUCCESS;
} }
The :doxygen:`BandMathImageFilter` is based on the mathematical parser library muParser.
The built in functions and operators list is available at:
http://muparser.sourceforge.net/mup_features.html.
In order to use this filter, at least one input image should be
set. An associated variable name can be specified or not by using
the corresponding ``SetNthInput`` method. For the nth input image, if
no associated variable name has been specified, a default variable
name is given by concatenating the letter "b" (for band) and the
corresponding input index.
The next step is to set the expression according to the variable
names. For example, in the default case with three input images the
following expression is valid: ``(b1+b2)*b3``.
.. |image1| image:: /Input/qb_ExtractRoad_pretty.png
.. |image2| image:: /Output/qb_BandMath-pretty.jpg
.. _Figure1:
+--------------------------+-------------------------+
| |image1| | |image2| |
+--------------------------+-------------------------+
NDVI of a Quickbird image computed with BandMathImageFilter
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