diff --git a/Examples/DimensionReduction/CMakeLists.txt b/Examples/DimensionReduction/CMakeLists.txt index 88b4c3b6085b510cb05e5c3cb62636d2665c3111..3b8d1f90fde3eda84626ce2969f6cf08c3a1ccd1 100644 --- a/Examples/DimensionReduction/CMakeLists.txt +++ b/Examples/DimensionReduction/CMakeLists.txt @@ -9,6 +9,15 @@ IF(CMAKE_COMPILER_IS_GNUCXX) ) ENDIF(CMAKE_COMPILER_IS_GNUCXX) +ADD_EXECUTABLE(NAPCAExample NAPCAExample.cxx ) +TARGET_LINK_LIBRARIES(NAPCAExample OTBCommon OTBIO OTBBasicFilters) + +ADD_EXECUTABLE(MNFExample MNFExample.cxx ) +TARGET_LINK_LIBRARIES(MNFExample OTBCommon OTBIO OTBBasicFilters) + +ADD_EXECUTABLE(ICAExample ICAExample.cxx ) +TARGET_LINK_LIBRARIES(ICAExample OTBCommon OTBIO OTBBasicFilters) + ADD_EXECUTABLE(PCAExample PCAExample.cxx ) TARGET_LINK_LIBRARIES(PCAExample OTBCommon OTBIO OTBBasicFilters) diff --git a/Examples/DimensionReduction/ICAExample.cxx b/Examples/DimensionReduction/ICAExample.cxx new file mode 100644 index 0000000000000000000000000000000000000000..7277842d8065968cb2d0f8cc8d592e935e7c6aac --- /dev/null +++ b/Examples/DimensionReduction/ICAExample.cxx @@ -0,0 +1,243 @@ +/*========================================================================= + + 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 "otbImage.h" +#include "otbVectorImage.h" +#include "otbImageFileReader.h" +#include "otbImageFileWriter.h" +#include "otbPrintableImageFilter.h" + +// Software Guide : BeginCommandLineArgs +// INPUTS: {wv2_cannes_8bands.tif} +// OUTPUTS: {FastICAOutput.tif}, {InverseFastICAOutput.tif}, {FastICA-input-pretty.png}, {FastICA-output-pretty.png}, {FastICA-invoutput-pretty.png} +// 8 20 1. +// Software Guide : EndCommandLineArgs + +// Software Guide : BeginLatex +// +// This example illustrates the use of the +// \doxygen{otb}{FastICAImageFilter}. +// This filter computes a Principal Component Analysis using an +// efficient method based on the inner product in order to compute the +// covariance matrix. +// +// The first step required to use this filter is to include its header file. +// +// Software Guide : EndLatex + +// Software Guide : BeginCodeSnippet +#include "otbFastICAImageFilter.h" +// Software Guide : EndCodeSnippet + + +int main(int argc, char* argv[]) +{ + typedef double PixelType; + const unsigned int Dimension = 2; + const char * inputFileName = argv[1]; + const char * outputFilename = argv[2]; + const char * outputInverseFilename = argv[3]; + const unsigned int numberOfPrincipalComponentsRequired(atoi(argv[7])); + const char * inpretty = argv[4]; + const char * outpretty = argv[5]; + const char * invoutpretty = argv[6]; + unsigned int numIterations = atoi(argv[7]); + double mu = atof(argv[8]); + + // Software Guide : BeginLatex + // + // We start by defining the types for the images and the reader and + // the writer. We choose to work with a \doxygen{otb}{VectorImage}, + // since we will produce a multi-channel image (the principal + // components) from a multi-channel input image. + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + typedef otb::VectorImage<PixelType, Dimension> ImageType; + typedef otb::ImageFileReader<ImageType> ReaderType; + typedef otb::ImageFileWriter<ImageType> WriterType; + // Software Guide : EndCodeSnippet + // Software Guide : BeginLatex + // + // We instantiate now the image reader and we set the image file name. + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + ReaderType::Pointer reader = ReaderType::New(); + reader->SetFileName(inputFileName); + // Software Guide : EndCodeSnippet + + // Software Guide : BeginLatex + // + // We define the type for the filter. It is templated over the input + // and the output image types and also the transformation direction. The + // internal structure of this filter is a filter-to-filter like structure. + // We can now the instantiate the filter. + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + typedef otb::FastICAImageFilter<ImageType, ImageType, + otb::Transform::FORWARD> FastICAFilterType; + FastICAFilterType::Pointer FastICAfilter = FastICAFilterType::New(); + // Software Guide : EndCodeSnippet + + // Software Guide : BeginLatex + // + // We then set the number of principal + // components required as output. We can choose to get less PCs than + // the number of input bands. + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + FastICAfilter->SetNumberOfPrincipalComponentsRequired( + numberOfPrincipalComponentsRequired); + // Software Guide : EndCodeSnippet + + // Software Guide : BeginLatex + // + // We set the number of iterations of the ICA algorithm. + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + FastICAfilter->SetNumberOfIterations(numIterations); + // Software Guide : EndCodeSnippet + + // Software Guide : BeginLatex + // + // We also set the $\mu$ parameter. + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + FastICAfilter->SetMu( mu ); + // Software Guide : EndCodeSnippet + + // Software Guide : BeginLatex + // + // We now instantiate the writer and set the file name for the + // output image. + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + WriterType::Pointer writer = WriterType::New(); + writer->SetFileName(outputFilename); + // Software Guide : EndCodeSnippet + // Software Guide : BeginLatex + // + // We finally plug the pipeline and trigger the PCA computation with + // the method \code{Update()} of the writer. + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + FastICAfilter->SetInput(reader->GetOutput()); + writer->SetInput(FastICAfilter->GetOutput()); + + writer->Update(); + // Software Guide : EndCodeSnippet + + // Software Guide : BeginLatex + // + // \doxygen{otb}{FastICAImageFilter} allows also to compute inverse + // transformation from PCA coefficients. In reverse mode, the + // covariance matrix or the transformation matrix + // (which may not be square) has to be given. + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + typedef otb::FastICAImageFilter< ImageType, ImageType, + otb::Transform::INVERSE > InvFastICAFilterType; + InvFastICAFilterType::Pointer invFilter = InvFastICAFilterType::New(); + + invFilter->SetMeanValues( FastICAfilter->GetMeanValues() ); + invFilter->SetStdDevValues( FastICAfilter->GetStdDevValues() ); + invFilter->SetTransformationMatrix( FastICAfilter->GetTransformationMatrix() ); + invFilter->SetPCATransformationMatrix( FastICAfilter->GetPCATransformationMatrix() ); + invFilter->SetInput(FastICAfilter->GetOutput()); + + WriterType::Pointer invWriter = WriterType::New(); + invWriter->SetFileName(outputInverseFilename ); + invWriter->SetInput(invFilter->GetOutput() ); + + invWriter->Update(); + // Software Guide : EndCodeSnippet + + // Software Guide : BeginLatex + // Figure~\ref{fig:FastICA_FILTER} shows the result of applying forward + // and reverse FastICA transformation to a 8 bands Wordlview2 image. + // \begin{figure} + // \center + // \includegraphics[width=0.32\textwidth]{FastICA-input-pretty.eps} + // \includegraphics[width=0.32\textwidth]{FastICA-output-pretty.eps} + // \includegraphics[width=0.32\textwidth]{FastICA-invoutput-pretty.eps} + // \itkcaption[PCA Filter (forward trasnformation)]{Result of applying the + // \doxygen{otb}{FastICAImageFilter} to an image. From left + // to right: + // original image, color composition with first three principal + // components and output of the + // inverse mode (the input RGB image).} + // \label{fig:FastICA_FILTER} + // \end{figure} + // + // Software Guide : EndLatex + + // This is for rendering in software guide + typedef otb::PrintableImageFilter<ImageType,ImageType> PrintFilterType; + typedef PrintFilterType::OutputImageType VisuImageType; + typedef otb::ImageFileWriter<VisuImageType> VisuWriterType; + + PrintFilterType::Pointer inputPrintFilter = PrintFilterType::New(); + PrintFilterType::Pointer outputPrintFilter = PrintFilterType::New(); + PrintFilterType::Pointer invertOutputPrintFilter = PrintFilterType::New(); + VisuWriterType::Pointer inputVisuWriter = VisuWriterType::New(); + VisuWriterType::Pointer outputVisuWriter = VisuWriterType::New(); + VisuWriterType::Pointer invertOutputVisuWriter = VisuWriterType::New(); + + inputPrintFilter->SetInput(reader->GetOutput()); + inputPrintFilter->SetChannel(5); + inputPrintFilter->SetChannel(3); + inputPrintFilter->SetChannel(2); + outputPrintFilter->SetInput(FastICAfilter->GetOutput()); + outputPrintFilter->SetChannel(1); + outputPrintFilter->SetChannel(2); + outputPrintFilter->SetChannel(3); + invertOutputPrintFilter->SetInput(invFilter->GetOutput()); + invertOutputPrintFilter->SetChannel(5); + invertOutputPrintFilter->SetChannel(3); + invertOutputPrintFilter->SetChannel(2); + + inputVisuWriter->SetInput(inputPrintFilter->GetOutput()); + outputVisuWriter->SetInput(outputPrintFilter->GetOutput()); + invertOutputVisuWriter->SetInput(invertOutputPrintFilter->GetOutput()); + + inputVisuWriter->SetFileName(inpretty); + outputVisuWriter->SetFileName(outpretty); + invertOutputVisuWriter->SetFileName(invoutpretty); + + inputVisuWriter->Update(); + outputVisuWriter->Update(); + invertOutputVisuWriter->Update(); + + return EXIT_SUCCESS; +} diff --git a/Examples/DimensionReduction/MNFExample.cxx b/Examples/DimensionReduction/MNFExample.cxx new file mode 100644 index 0000000000000000000000000000000000000000..8977d85bdb74c7b16cbc9ffc86ed02ed0d362d85 --- /dev/null +++ b/Examples/DimensionReduction/MNFExample.cxx @@ -0,0 +1,267 @@ +/*========================================================================= + + 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 "otbImage.h" +#include "otbVectorImage.h" +#include "otbImageFileReader.h" +#include "otbImageFileWriter.h" +#include "otbPrintableImageFilter.h" + +// Software Guide : BeginCommandLineArgs +// INPUTS: {wv2_cannes_8bands.tif} +// OUTPUTS: {MNFOutput.tif}, {InverseMNFOutput.tif}, {MNF-input-pretty.png}, {MNF-output-pretty.png}, {MNF-invoutput-pretty.png} +// 8 1 1 +// Software Guide : EndCommandLineArgs + +// Software Guide : BeginLatex +// +// This example illustrates the use of the +// \doxygen{otb}{MNFImageFilter}. +// This filter computes a Principal Component Analysis using an +// efficient method based on the inner product in order to compute the +// covariance matrix. +// +// The first step required to use this filter is to include its header file. +// +// Software Guide : EndLatex + +// Software Guide : BeginCodeSnippet +#include "otbMNFImageFilter.h" +// Software Guide : EndCodeSnippet + +// Software Guide : BeginLatex +// +// We also need to include the header of the noise filter. +// +// SoftwareGuide : EndLatex + +// Software Guide : BeginCodeSnippet +#include "otbLocalActivityVectorImageFilter.h" +// Software Guide : EndCodeSnippet + + +int main(int argc, char* argv[]) +{ + typedef double PixelType; + const unsigned int Dimension = 2; + const char * inputFileName = argv[1]; + const char * outputFilename = argv[2]; + const char * outputInverseFilename = argv[3]; + const unsigned int numberOfPrincipalComponentsRequired(atoi(argv[7])); + const char * inpretty = argv[4]; + const char * outpretty = argv[5]; + const char * invoutpretty = argv[6]; + unsigned int vradius = atoi(argv[7]); + bool normalization = atoi(argv[8]); + + // Software Guide : BeginLatex + // + // We start by defining the types for the images and the reader and + // the writer. We choose to work with a \doxygen{otb}{VectorImage}, + // since we will produce a multi-channel image (the principal + // components) from a multi-channel input image. + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + typedef otb::VectorImage<PixelType, Dimension> ImageType; + typedef otb::ImageFileReader<ImageType> ReaderType; + typedef otb::ImageFileWriter<ImageType> WriterType; + // Software Guide : EndCodeSnippet + // Software Guide : BeginLatex + // + // We instantiate now the image reader and we set the image file name. + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + ReaderType::Pointer reader = ReaderType::New(); + reader->SetFileName(inputFileName); + // Software Guide : EndCodeSnippet + + // Software Guide : BeginLatex + // + // We define the type of the noise filter. + // + // Software Guide : EndLatex + + // SoftwareGuide : BeginCodeSnippet + typedef otb::LocalActivityVectorImageFilter< ImageType, ImageType > NoiseFilterType; + // SoftwareGuide : EndCodeSnippet + + + // Software Guide : BeginLatex + // + // We define the type for the filter. It is templated over the input + // and the output image types and also the transformation direction. The + // internal structure of this filter is a filter-to-filter like structure. + // We can now the instantiate the filter. + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + typedef otb::MNFImageFilter<ImageType, ImageType, + NoiseFilterType, + otb::Transform::FORWARD> MNFFilterType; + MNFFilterType::Pointer MNFfilter = MNFFilterType::New(); + // Software Guide : EndCodeSnippet + + // Software Guide : BeginLatex + // + // We then set the number of principal + // components required as output. We can choose to get less PCs than + // the number of input bands. + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + MNFfilter->SetNumberOfPrincipalComponentsRequired( + numberOfPrincipalComponentsRequired); + // Software Guide : EndCodeSnippet + + // Software Guide : BeginLatex + // + // We set the radius of the sliding window for noise estimation. + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + NoiseFilterType::RadiusType radius = {{ vradius, vradius }}; + MNFfilter->GetNoiseImageFilter()->SetRadius(radius); + // Software Guide : EndCodeSnippet + + // Software Guide : BeginLatex + // + // Last, we can activate normalisation. + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + MNFfilter->SetUseNormalization( normalization ); + // Software Guide : EndCodeSnippet + + // Software Guide : BeginLatex + // + // We now instantiate the writer and set the file name for the + // output image. + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + WriterType::Pointer writer = WriterType::New(); + writer->SetFileName(outputFilename); + // Software Guide : EndCodeSnippet + // Software Guide : BeginLatex + // + // We finally plug the pipeline and trigger the PCA computation with + // the method \code{Update()} of the writer. + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + MNFfilter->SetInput(reader->GetOutput()); + writer->SetInput(MNFfilter->GetOutput()); + + writer->Update(); + // Software Guide : EndCodeSnippet + + // Software Guide : BeginLatex + // + // \doxygen{otb}{MNFImageFilter} allows also to compute inverse + // transformation from PCA coefficients. In reverse mode, the + // covariance matrix or the transformation matrix + // (which may not be square) has to be given. + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + typedef otb::MNFImageFilter< ImageType, ImageType, + NoiseFilterType, + otb::Transform::INVERSE > InvMNFFilterType; + InvMNFFilterType::Pointer invFilter = InvMNFFilterType::New(); + + invFilter->SetMeanValues( MNFfilter->GetMeanValues() ); + if ( normalization ) + invFilter->SetStdDevValues( MNFfilter->GetStdDevValues() ); + invFilter->SetTransformationMatrix( MNFfilter->GetTransformationMatrix() ); + invFilter->SetInput(MNFfilter->GetOutput()); + + WriterType::Pointer invWriter = WriterType::New(); + invWriter->SetFileName(outputInverseFilename ); + invWriter->SetInput(invFilter->GetOutput() ); + + invWriter->Update(); + // Software Guide : EndCodeSnippet + + // Software Guide : BeginLatex + // Figure~\ref{fig:MNF_FILTER} shows the result of applying forward + // and reverse MNF transformation to a 8 bands Wordlview2 image. + // \begin{figure} + // \center + // \includegraphics[width=0.32\textwidth]{MNF-input-pretty.eps} + // \includegraphics[width=0.32\textwidth]{MNF-output-pretty.eps} + // \includegraphics[width=0.32\textwidth]{MNF-invoutput-pretty.eps} + // \itkcaption[PCA Filter (forward trasnformation)]{Result of applying the + // \doxygen{otb}{MNFImageFilter} to an image. From left + // to right: + // original image, color composition with first three principal + // components and output of the + // inverse mode (the input RGB image).} + // \label{fig:MNF_FILTER} + // \end{figure} + // + // Software Guide : EndLatex + + // This is for rendering in software guide + typedef otb::PrintableImageFilter<ImageType,ImageType> PrintFilterType; + typedef PrintFilterType::OutputImageType VisuImageType; + typedef otb::ImageFileWriter<VisuImageType> VisuWriterType; + + PrintFilterType::Pointer inputPrintFilter = PrintFilterType::New(); + PrintFilterType::Pointer outputPrintFilter = PrintFilterType::New(); + PrintFilterType::Pointer invertOutputPrintFilter = PrintFilterType::New(); + VisuWriterType::Pointer inputVisuWriter = VisuWriterType::New(); + VisuWriterType::Pointer outputVisuWriter = VisuWriterType::New(); + VisuWriterType::Pointer invertOutputVisuWriter = VisuWriterType::New(); + + inputPrintFilter->SetInput(reader->GetOutput()); + inputPrintFilter->SetChannel(5); + inputPrintFilter->SetChannel(3); + inputPrintFilter->SetChannel(2); + outputPrintFilter->SetInput(MNFfilter->GetOutput()); + outputPrintFilter->SetChannel(1); + outputPrintFilter->SetChannel(2); + outputPrintFilter->SetChannel(3); + invertOutputPrintFilter->SetInput(invFilter->GetOutput()); + invertOutputPrintFilter->SetChannel(5); + invertOutputPrintFilter->SetChannel(3); + invertOutputPrintFilter->SetChannel(2); + + inputVisuWriter->SetInput(inputPrintFilter->GetOutput()); + outputVisuWriter->SetInput(outputPrintFilter->GetOutput()); + invertOutputVisuWriter->SetInput(invertOutputPrintFilter->GetOutput()); + + inputVisuWriter->SetFileName(inpretty); + outputVisuWriter->SetFileName(outpretty); + invertOutputVisuWriter->SetFileName(invoutpretty); + + inputVisuWriter->Update(); + outputVisuWriter->Update(); + invertOutputVisuWriter->Update(); + + return EXIT_SUCCESS; +} diff --git a/Examples/DimensionReduction/NAPCAExample.cxx b/Examples/DimensionReduction/NAPCAExample.cxx new file mode 100644 index 0000000000000000000000000000000000000000..a6200c068168b048ec4597839925aa36390ce21b --- /dev/null +++ b/Examples/DimensionReduction/NAPCAExample.cxx @@ -0,0 +1,267 @@ +/*========================================================================= + + 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 "otbImage.h" +#include "otbVectorImage.h" +#include "otbImageFileReader.h" +#include "otbImageFileWriter.h" +#include "otbPrintableImageFilter.h" + +// Software Guide : BeginCommandLineArgs +// INPUTS: {wv2_cannes_8bands.tif} +// OUTPUTS: {NAPCAOutput.tif}, {InverseNAPCAOutput.tif}, {napca-input-pretty.png}, {napca-output-pretty.png}, {napca-invoutput-pretty.png} +// 8 1 1 +// Software Guide : EndCommandLineArgs + +// Software Guide : BeginLatex +// +// This example illustrates the use of the +// \doxygen{otb}{NAPCAImageFilter}. +// This filter computes a Principal Component Analysis using an +// efficient method based on the inner product in order to compute the +// covariance matrix. +// +// The first step required to use this filter is to include its header file. +// +// Software Guide : EndLatex + +// Software Guide : BeginCodeSnippet +#include "otbNAPCAImageFilter.h" +// Software Guide : EndCodeSnippet + +// Software Guide : BeginLatex +// +// We also need to include the header of the noise filter. +// +// SoftwareGuide : EndLatex + +// Software Guide : BeginCodeSnippet +#include "otbLocalActivityVectorImageFilter.h" +// Software Guide : EndCodeSnippet + + +int main(int argc, char* argv[]) +{ + typedef double PixelType; + const unsigned int Dimension = 2; + const char * inputFileName = argv[1]; + const char * outputFilename = argv[2]; + const char * outputInverseFilename = argv[3]; + const unsigned int numberOfPrincipalComponentsRequired(atoi(argv[7])); + const char * inpretty = argv[4]; + const char * outpretty = argv[5]; + const char * invoutpretty = argv[6]; + unsigned int vradius = atoi(argv[7]); + bool normalization = atoi(argv[8]); + + // Software Guide : BeginLatex + // + // We start by defining the types for the images and the reader and + // the writer. We choose to work with a \doxygen{otb}{VectorImage}, + // since we will produce a multi-channel image (the principal + // components) from a multi-channel input image. + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + typedef otb::VectorImage<PixelType, Dimension> ImageType; + typedef otb::ImageFileReader<ImageType> ReaderType; + typedef otb::ImageFileWriter<ImageType> WriterType; + // Software Guide : EndCodeSnippet + // Software Guide : BeginLatex + // + // We instantiate now the image reader and we set the image file name. + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + ReaderType::Pointer reader = ReaderType::New(); + reader->SetFileName(inputFileName); + // Software Guide : EndCodeSnippet + + // Software Guide : BeginLatex + // + // We define the type of the noise filter. + // + // Software Guide : EndLatex + + // SoftwareGuide : BeginCodeSnippet + typedef otb::LocalActivityVectorImageFilter< ImageType, ImageType > NoiseFilterType; + // SoftwareGuide : EndCodeSnippet + + + // Software Guide : BeginLatex + // + // We define the type for the filter. It is templated over the input + // and the output image types and also the transformation direction. The + // internal structure of this filter is a filter-to-filter like structure. + // We can now the instantiate the filter. + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + typedef otb::NAPCAImageFilter<ImageType, ImageType, + NoiseFilterType, + otb::Transform::FORWARD> NAPCAFilterType; + NAPCAFilterType::Pointer napcafilter = NAPCAFilterType::New(); + // Software Guide : EndCodeSnippet + + // Software Guide : BeginLatex + // + // We then set the number of principal + // components required as output. We can choose to get less PCs than + // the number of input bands. + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + napcafilter->SetNumberOfPrincipalComponentsRequired( + numberOfPrincipalComponentsRequired); + // Software Guide : EndCodeSnippet + + // Software Guide : BeginLatex + // + // We set the radius of the sliding window for noise estimation. + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + NoiseFilterType::RadiusType radius = {{ vradius, vradius }}; + napcafilter->GetNoiseImageFilter()->SetRadius(radius); + // Software Guide : EndCodeSnippet + + // Software Guide : BeginLatex + // + // Last, we can activate normalisation. + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + napcafilter->SetUseNormalization( normalization ); + // Software Guide : EndCodeSnippet + + // Software Guide : BeginLatex + // + // We now instantiate the writer and set the file name for the + // output image. + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + WriterType::Pointer writer = WriterType::New(); + writer->SetFileName(outputFilename); + // Software Guide : EndCodeSnippet + // Software Guide : BeginLatex + // + // We finally plug the pipeline and trigger the PCA computation with + // the method \code{Update()} of the writer. + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + napcafilter->SetInput(reader->GetOutput()); + writer->SetInput(napcafilter->GetOutput()); + + writer->Update(); + // Software Guide : EndCodeSnippet + + // Software Guide : BeginLatex + // + // \doxygen{otb}{NAPCAImageFilter} allows also to compute inverse + // transformation from PCA coefficients. In reverse mode, the + // covariance matrix or the transformation matrix + // (which may not be square) has to be given. + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + typedef otb::NAPCAImageFilter< ImageType, ImageType, + NoiseFilterType, + otb::Transform::INVERSE > InvNAPCAFilterType; + InvNAPCAFilterType::Pointer invFilter = InvNAPCAFilterType::New(); + + invFilter->SetMeanValues( napcafilter->GetMeanValues() ); + if ( normalization ) + invFilter->SetStdDevValues( napcafilter->GetStdDevValues() ); + invFilter->SetTransformationMatrix( napcafilter->GetTransformationMatrix() ); + invFilter->SetInput(napcafilter->GetOutput()); + + WriterType::Pointer invWriter = WriterType::New(); + invWriter->SetFileName(outputInverseFilename ); + invWriter->SetInput(invFilter->GetOutput() ); + + invWriter->Update(); + // Software Guide : EndCodeSnippet + + // Software Guide : BeginLatex + // Figure~\ref{fig:NAPCA_FILTER} shows the result of applying forward + // and reverse NAPCA transformation to a 8 bands Wordlview2 image. + // \begin{figure} + // \center + // \includegraphics[width=0.32\textwidth]{napca-input-pretty.eps} + // \includegraphics[width=0.32\textwidth]{napca-output-pretty.eps} + // \includegraphics[width=0.32\textwidth]{napca-invoutput-pretty.eps} + // \itkcaption[PCA Filter (forward trasnformation)]{Result of applying the + // \doxygen{otb}{NAPCAImageFilter} to an image. From left + // to right: + // original image, color composition with first three principal + // components and output of the + // inverse mode (the input RGB image).} + // \label{fig:PCNAA_FILTER} + // \end{figure} + // + // Software Guide : EndLatex + + // This is for rendering in software guide + typedef otb::PrintableImageFilter<ImageType,ImageType> PrintFilterType; + typedef PrintFilterType::OutputImageType VisuImageType; + typedef otb::ImageFileWriter<VisuImageType> VisuWriterType; + + PrintFilterType::Pointer inputPrintFilter = PrintFilterType::New(); + PrintFilterType::Pointer outputPrintFilter = PrintFilterType::New(); + PrintFilterType::Pointer invertOutputPrintFilter = PrintFilterType::New(); + VisuWriterType::Pointer inputVisuWriter = VisuWriterType::New(); + VisuWriterType::Pointer outputVisuWriter = VisuWriterType::New(); + VisuWriterType::Pointer invertOutputVisuWriter = VisuWriterType::New(); + + inputPrintFilter->SetInput(reader->GetOutput()); + inputPrintFilter->SetChannel(5); + inputPrintFilter->SetChannel(3); + inputPrintFilter->SetChannel(2); + outputPrintFilter->SetInput(napcafilter->GetOutput()); + outputPrintFilter->SetChannel(1); + outputPrintFilter->SetChannel(2); + outputPrintFilter->SetChannel(3); + invertOutputPrintFilter->SetInput(invFilter->GetOutput()); + invertOutputPrintFilter->SetChannel(5); + invertOutputPrintFilter->SetChannel(3); + invertOutputPrintFilter->SetChannel(2); + + inputVisuWriter->SetInput(inputPrintFilter->GetOutput()); + outputVisuWriter->SetInput(outputPrintFilter->GetOutput()); + invertOutputVisuWriter->SetInput(invertOutputPrintFilter->GetOutput()); + + inputVisuWriter->SetFileName(inpretty); + outputVisuWriter->SetFileName(outpretty); + invertOutputVisuWriter->SetFileName(invoutpretty); + + inputVisuWriter->Update(); + outputVisuWriter->Update(); + invertOutputVisuWriter->Update(); + + return EXIT_SUCCESS; +} diff --git a/Examples/DimensionReduction/PCAExample.cxx b/Examples/DimensionReduction/PCAExample.cxx index 66cf72cd50e26df4d7976bda24769f3f784d1125..aa3bf14cf6cb0d628b3346c69fe8e72c0cdebdd1 100644 --- a/Examples/DimensionReduction/PCAExample.cxx +++ b/Examples/DimensionReduction/PCAExample.cxx @@ -19,14 +19,12 @@ #include "otbVectorImage.h" #include "otbImageFileReader.h" #include "otbImageFileWriter.h" -#include "otbMultiToMonoChannelExtractROI.h" -#include "itkRescaleIntensityImageFilter.h" -#include "otbVectorRescaleIntensityImageFilter.h" +#include "otbPrintableImageFilter.h" // Software Guide : BeginCommandLineArgs -// INPUTS: {ROI_QB_MUL_1.png} -// OUTPUTS: {PCAOutput.tif}, {InversePCAOutput.tif}, {InversePCAOutput1.png}, {PCAOutput1.png}, {PCAOutput2.png}, {PCAOutput3.png} -// 3 +// INPUTS: {wv2_cannes_8bands.tif} +// OUTPUTS: {PCAOutput.tif}, {InversePCAOutput.tif}, {input-pretty.png}, {output-pretty.png}, {invoutput-pretty.png} +// 8 // Software Guide : EndCommandLineArgs // Software Guide : BeginLatex @@ -52,7 +50,11 @@ int main(int argc, char* argv[]) const char * inputFileName = argv[1]; const char * outputFilename = argv[2]; const char * outputInverseFilename = argv[3]; - const unsigned int numberOfPrincipalComponentsRequired(atoi(argv[8])); + const unsigned int numberOfPrincipalComponentsRequired(atoi(argv[7])); + const char * inpretty = argv[4]; + const char * outpretty = argv[5]; + const char * invoutpretty = argv[6]; + // Software Guide : BeginLatex // @@ -155,75 +157,59 @@ int main(int argc, char* argv[]) // Software Guide : BeginLatex // Figure~\ref{fig:PCA_FILTER} shows the result of applying forward - // and reverse PCA transformation to a 3 band RGB image. + // and reverse PCA transformation to a 8 bands Wordlview2 image. // \begin{figure} // \center - // \includegraphics[width=0.25\textwidth]{ROI_QB_MUL_1.eps} - // \includegraphics[width=0.25\textwidth]{PCAOutput1.eps} - // \includegraphics[width=0.25\textwidth]{PCAOutput2.eps} - // \includegraphics[width=0.25\textwidth]{PCAOutput3.eps} - // \includegraphics[width=0.25\textwidth]{InversePCAOutput1.eps} + // \includegraphics[width=0.32\textwidth]{input-pretty.eps} + // \includegraphics[width=0.32\textwidth]{output-pretty.eps} + // \includegraphics[width=0.32\textwidth]{invoutput-pretty.eps} // \itkcaption[PCA Filter (forward trasnformation)]{Result of applying the // \doxygen{otb}{PCAImageFilter} to an image. From left - // to right and top to bottom: - // original image, first PC, second PC, third PC and output of the + // to right: + // original image, color composition with first three principal + // components and output of the // inverse mode (the input RGB image).} // \label{fig:PCA_FILTER} // \end{figure} // // Software Guide : EndLatex - typedef otb::Image<PixelType, Dimension> MonoImageType; - - typedef otb::MultiToMonoChannelExtractROI<PixelType, PixelType> - ExtractROIFilterType; - - typedef otb::Image<unsigned char, 2> OutputImageType; - typedef otb::VectorImage<unsigned char, 2> OutputPrettyImageType; - typedef otb::ImageFileWriter<OutputImageType> WriterType2; - typedef otb::ImageFileWriter<OutputPrettyImageType> WriterType3; - - typedef itk::RescaleIntensityImageFilter<MonoImageType, - OutputImageType> RescalerType; - - typedef otb::VectorRescaleIntensityImageFilter<ImageType, - OutputPrettyImageType> RescalerType2; - - typedef ImageType::PixelType VectorPixelType; - - for (unsigned int cpt = 0; cpt < numberOfPrincipalComponentsRequired; cpt++) - { - ExtractROIFilterType::Pointer extractROIFilter = ExtractROIFilterType::New(); - RescalerType::Pointer rescaler = RescalerType::New(); - WriterType2::Pointer writer2 = WriterType2::New(); - - extractROIFilter->SetInput(pcafilter->GetOutput()); - extractROIFilter->SetChannel(cpt + 1); - - rescaler->SetInput(extractROIFilter->GetOutput()); - rescaler->SetOutputMinimum(0); - rescaler->SetOutputMaximum(255); - - writer2->SetInput(rescaler->GetOutput()); - writer2->SetFileName(argv[cpt + 5]); - writer2->Update(); - } - - WriterType3::Pointer writerInverse = WriterType3::New(); - RescalerType2::Pointer rescalerInverse = RescalerType2::New(); - rescalerInverse->SetInput(invFilter->GetOutput()); - - VectorPixelType minimum, maximum; - minimum.SetSize(3); - maximum.SetSize(3); - minimum.Fill(0); - maximum.Fill(255); - rescalerInverse->SetOutputMinimum(minimum); - rescalerInverse->SetOutputMaximum(maximum); - - writerInverse->SetInput(rescalerInverse->GetOutput()); - writerInverse->SetFileName(argv[4]); - writerInverse->Update(); + // This is for rendering in software guide + typedef otb::PrintableImageFilter<ImageType,ImageType> PrintFilterType; + typedef PrintFilterType::OutputImageType VisuImageType; + typedef otb::ImageFileWriter<VisuImageType> VisuWriterType; + + PrintFilterType::Pointer inputPrintFilter = PrintFilterType::New(); + PrintFilterType::Pointer outputPrintFilter = PrintFilterType::New(); + PrintFilterType::Pointer invertOutputPrintFilter = PrintFilterType::New(); + VisuWriterType::Pointer inputVisuWriter = VisuWriterType::New(); + VisuWriterType::Pointer outputVisuWriter = VisuWriterType::New(); + VisuWriterType::Pointer invertOutputVisuWriter = VisuWriterType::New(); + + inputPrintFilter->SetInput(reader->GetOutput()); + inputPrintFilter->SetChannel(5); + inputPrintFilter->SetChannel(3); + inputPrintFilter->SetChannel(2); + outputPrintFilter->SetInput(pcafilter->GetOutput()); + outputPrintFilter->SetChannel(1); + outputPrintFilter->SetChannel(2); + outputPrintFilter->SetChannel(3); + invertOutputPrintFilter->SetInput(invFilter->GetOutput()); + invertOutputPrintFilter->SetChannel(5); + invertOutputPrintFilter->SetChannel(3); + invertOutputPrintFilter->SetChannel(2); + + inputVisuWriter->SetInput(inputPrintFilter->GetOutput()); + outputVisuWriter->SetInput(outputPrintFilter->GetOutput()); + invertOutputVisuWriter->SetInput(invertOutputPrintFilter->GetOutput()); + + inputVisuWriter->SetFileName(inpretty); + outputVisuWriter->SetFileName(outpretty); + invertOutputVisuWriter->SetFileName(invoutpretty); + + inputVisuWriter->Update(); + outputVisuWriter->Update(); + invertOutputVisuWriter->Update(); return EXIT_SUCCESS; }