diff --git a/Examples/FeatureExtraction/CMakeLists.txt b/Examples/FeatureExtraction/CMakeLists.txt index 514468efc3110139318078c59fe7093890fbf62f..d164248aa189435645b343b3277c66e5f0d1a896 100644 --- a/Examples/FeatureExtraction/CMakeLists.txt +++ b/Examples/FeatureExtraction/CMakeLists.txt @@ -57,6 +57,10 @@ ADD_EXECUTABLE(SeamCarvingExample SeamCarvingExample.cxx) TARGET_LINK_LIBRARIES(SeamCarvingExample OTBIO OTBCommon OTBFeatureExtraction ITKCommon ITKBasicFilters) +ADD_EXECUTABLE(SeamCarvingOtherExample SeamCarvingOtherExample.cxx) +TARGET_LINK_LIBRARIES(SeamCarvingOtherExample OTBIO OTBCommon OTBFeatureExtraction +ITKCommon ITKBasicFilters) + IF( NOT OTB_DISABLE_CXX_TESTING AND NOT OTB_DISABLE_CXX_EXAMPLES_TESTING ) SET(BASELINE ${OTB_DATA_ROOT}/Baseline/Examples/FeatureExtraction) @@ -213,6 +217,28 @@ ADD_TEST(ExtractRoadExampleTest ${EXE_TESTS} 337 557 432 859 1.0 0.00005 1.0 0.39269 1.0 10.0 25. ) +# ------- SeamCarvingExamplesTest---------- + +ADD_TEST(SeamCarvingExampleTest ${EXE_TESTS} + --compare-image ${TOL} + ${BASELINE}/SeamCarvingExampleOutput.png + ${TEMP}/SeamCarvingExampleOutput.png + SeamCarvingExampleTest + ${INPUTDATA}/QB_Suburb.png + ${TEMP}/SeamCarvingExampleOutput.png + 50 +) + +ADD_TEST(SeamCarvingOtherExampleTest ${EXE_TESTS} + --compare-image ${TOL} + ${BASELINE}/SeamCarvingOtherExampleOutput.png + ${TEMP}/SeamCarvingOtherExampleOutput.png + SeamCarvingOtherExampleTest + ${INPUTDATA}/QB_Suburb.png + ${TEMP}/SeamCarvingOtherExampleOutput.png + 50 +) + INCLUDE_DIRECTORIES("${OTBTesting_BINARY_DIR}") ADD_EXECUTABLE(otbFeatureExtractionExamplesTests otbFeatureExtractionExamplesTests.cxx) diff --git a/Examples/FeatureExtraction/SeamCarvingExample.cxx b/Examples/FeatureExtraction/SeamCarvingExample.cxx index f36de07050e0bc5b921fcc222cd98bb304cff8c6..ddca8100abc0679cdeebaf99c53f72a4ce6a2a74 100644 --- a/Examples/FeatureExtraction/SeamCarvingExample.cxx +++ b/Examples/FeatureExtraction/SeamCarvingExample.cxx @@ -39,7 +39,7 @@ // Software Guide : BeginCommandLineArgs // INPUTS: {QB_Suburb.png} // OUTPUTS: {SeamCarvingExampleOutput.png} -// 100 +// 50 // Software Guide : EndCommandLineArgs @@ -154,8 +154,6 @@ int main(int argc, char ** argv) for (int i=0; i<iteration; i++) { - std::cout << "Iterations: " << i << std::endl; - gradient->SetInput( duplicator->GetOutput() ); // Software Guide : EndCodeSnippet diff --git a/Examples/FeatureExtraction/SeamCarvingOtherExample.cxx b/Examples/FeatureExtraction/SeamCarvingOtherExample.cxx new file mode 100644 index 0000000000000000000000000000000000000000..7bdd21d2d9465a575b867f5ccf407d9a9ed5c84f --- /dev/null +++ b/Examples/FeatureExtraction/SeamCarvingOtherExample.cxx @@ -0,0 +1,210 @@ +/*========================================================================= + + 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. + +=========================================================================*/ +#if defined(_MSC_VER) +#pragma warning ( disable : 4786 ) +#endif + +#ifdef __BORLANDC__ +#define ITK_LEAN_AND_MEAN +#endif + + +// Software Guide : BeginLatex +// +// This example illustrate the use of the \doxygen{otb}{AddCarvingPathFilter}, +// the opposite of the \doxygen{otb}{RemoveCarvingPathFilter}. +// +// Here, we use this filter combined with the \doxygen{otb}{DrawPathFilter} to +// output the image with the removed seam in white. +// +// Most of the code is similar to the previous example. +// +// Software Guide : EndLatex + +// Software Guide : BeginCommandLineArgs +// INPUTS: {QB_Suburb.png} +// OUTPUTS: {SeamCarvingOtherExampleOutput.png} +// 50 +// Software Guide : EndCommandLineArgs + + +#include "otbImage.h" +#include "itkPolyLineParametricPath.h" +#include "otbImageFileReader.h" +#include "otbImageFileWriter.h" +#include "itkRescaleIntensityImageFilter.h" +#include "itkGradientMagnitudeImageFilter.h" + +#include "otbDrawPathFilter.h" +#include "otbImageToCarvingPathFilter.h" +#include "otbRemoveCarvingPathFilter.h" +#include "otbAddCarvingPathFilter.h" + +#include "itkImageDuplicator.h" +#include "otbObjectList.h" + +int main(int argc, char ** argv) +{ + + typedef float InputPixelType; + typedef unsigned char OutputPixelType; + const unsigned int Dimension = 2; + + typedef otb::Image< InputPixelType, Dimension > ImageType; + typedef otb::Image< OutputPixelType, Dimension > OutputImageType; + typedef itk::PolyLineParametricPath<Dimension> PathType; + + // Software Guide : BeginLatex + // + // We need to define a list to keep the path in memory until the end of + // the seam carving process. This is done using an \doxygen{otb}{ObjectList} + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + typedef otb::ObjectList<PathType> PathListType; + PathListType::Pointer pathList = PathListType::New(); + // Software Guide : EndCodeSnippet + + typedef otb::ImageFileReader< ImageType > ReaderType; + typedef otb::ImageFileWriter< OutputImageType > WriterType; + typedef itk::RescaleIntensityImageFilter + <ImageType, OutputImageType> RescalerType; + + ReaderType::Pointer reader = ReaderType::New(); + WriterType::Pointer writer = WriterType::New(); + RescalerType::Pointer rescaler = RescalerType::New(); + + const char * filenamereader = argv[1]; + reader->SetFileName( filenamereader ); + + const char * filenamewriter = argv[2]; + writer->SetFileName( filenamewriter ); + + int iteration = atoi(argv[3]); + + // Software Guide : BeginLatex + // + // We instanciate the different filters of the pipeline as before. + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + typedef itk::GradientMagnitudeImageFilter< ImageType, ImageType> GradientType; + GradientType::Pointer gradient = GradientType::New(); + + typedef otb::ImageToCarvingPathFilter<ImageType, PathType> CarvingFilterType; + CarvingFilterType::Pointer carvingFilter = CarvingFilterType::New(); + + typedef otb::DrawPathFilter + <ImageType, PathType, ImageType> DrawPathFilterType; + DrawPathFilterType::Pointer drawPathFilter = DrawPathFilterType::New(); + + typedef otb::RemoveCarvingPathFilter + <ImageType, PathType, ImageType> RemoveCarvingPathFilterType; + RemoveCarvingPathFilterType::Pointer removeCarvingPath = RemoveCarvingPathFilterType::New(); + + typedef otb::AddCarvingPathFilter + <ImageType, PathType, ImageType> AddCarvingPathFilterType; + AddCarvingPathFilterType::Pointer addCarvingPath = AddCarvingPathFilterType::New(); + + + typedef itk::ImageDuplicator< ImageType > duplicatorType; + duplicatorType::Pointer duplicator = duplicatorType::New(); + reader->Update(); + duplicator->SetInputImage(reader->GetOutput()); + duplicator->Update(); + // Software Guide : EndCodeSnippet + + + // Software Guide : BeginLatex + // + // The loop to shorten the image is similar to the previous one. Here we + // decide to remove alternatively one vertical and one horizontal seam. At + // each iteration, we save the seam on the list using the \code{PushBack()} + // method. + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + for (int i=0; i<iteration; i++) + { + + gradient->SetInput( duplicator->GetOutput() ); + + carvingFilter->SetInput( gradient->GetOutput() ); + carvingFilter->SetDirection(i%2); + + removeCarvingPath->SetInput( duplicator->GetOutput() ); + removeCarvingPath->SetPathInput( carvingFilter->GetOutput() ); + removeCarvingPath->SetDirection(i%2); + removeCarvingPath->UpdateLargestPossibleRegion(); + + + pathList->PushBack(carvingFilter->GetOutput()); + carvingFilter->GetOutput()->DisconnectPipeline(); + + + duplicator->SetInputImage(removeCarvingPath->GetOutput()); + duplicator->Update(); + + } + // Software Guide : EndCodeSnippet + + + // Software Guide : BeginLatex + // + // The next loop will put back the seam using the + // \doxygen{otb}{AddCarvingPathFilter} and drawing it with the + // \doxygen{otb}{DrawPathFilter}. + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + for(int i=iteration-1; i>=0; i--) + { + + addCarvingPath->SetInput( duplicator->GetOutput() ); + addCarvingPath->SetPathInput( pathList->GetNthElement(i) ); + addCarvingPath->SetDirection(i%2); + addCarvingPath->UpdateLargestPossibleRegion(); + + drawPathFilter->SetInput( addCarvingPath->GetOutput() ); + drawPathFilter->SetPathInput( pathList->GetNthElement(i) ); + drawPathFilter->UpdateLargestPossibleRegion(); + + duplicator->SetInputImage(drawPathFilter->GetOutput()); + duplicator->Update(); + } + // Software Guide : EndCodeSnippet + + // Software Guide : BeginLatex + // + // Finally, the resulting image is saved on an image file as usual + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + rescaler->SetInput( duplicator->GetOutput() ); + writer->SetInput( rescaler->GetOutput() ); + writer->Update(); + // Software Guide : EndCodeSnippet + + +} + diff --git a/Examples/FeatureExtraction/otbFeatureExtractionExamplesTests.cxx b/Examples/FeatureExtraction/otbFeatureExtractionExamplesTests.cxx index 9ac08aac343277b12e9c7b9293c1afb161441e32..f4566d28e27d917454d502d040356cf6990af85e 100644 --- a/Examples/FeatureExtraction/otbFeatureExtractionExamplesTests.cxx +++ b/Examples/FeatureExtraction/otbFeatureExtractionExamplesTests.cxx @@ -36,6 +36,8 @@ REGISTER_TEST(RatioLineDetectorExampleTest); REGISTER_TEST(AlignmentsExampleTest); REGISTER_TEST(ExtractRoadByStepsExampleTest); REGISTER_TEST(ExtractRoadExampleTest); +REGISTER_TEST(SeamCarvingExampleTest); +REGISTER_TEST(SeamCarvingOtherExampleTest); } #undef main @@ -81,3 +83,11 @@ REGISTER_TEST(ExtractRoadExampleTest); #undef main #define main ExtractRoadExampleTest #include "ExtractRoadExample.cxx" + +#undef main +#define main SeamCarvingExampleTest +#include "SeamCarvingExample.cxx" + +#undef main +#define main SeamCarvingOtherExampleTest +#include "SeamCarvingOtherExample.cxx"