diff --git a/Modules/Filtering/ImageManipulation/test/CMakeLists.txt b/Modules/Filtering/ImageManipulation/test/CMakeLists.txt index 48a44bee06e7f2d63cbae46c0a9af2fbb12b46ee..6b26881e637a74650b8cf1d74cf5c2779c7e9cf6 100644 --- a/Modules/Filtering/ImageManipulation/test/CMakeLists.txt +++ b/Modules/Filtering/ImageManipulation/test/CMakeLists.txt @@ -71,6 +71,7 @@ otbFunctionWithNeighborhoodToImageFilterNew.cxx otbEuclideanDistanceMetricWithMissingValue.cxx otbEuclideanDistanceMetricWithMissingValueNew.cxx otbChangeNoDataValueFilter.cxx +otbImageToNoDataMaskFilter.cxx ) add_executable(otbImageManipulationTestDriver ${OTBImageManipulationTests}) @@ -672,4 +673,7 @@ otb_add_test(NAME bfTuEuclideanDistanceMetricWithMissingValueNew COMMAND otbImag otbEuclideanDistanceMetricWithMissingValueNew) otb_add_test(NAME filteringImageManipulationChangeNoDataValueFilter COMMAND otbImageManipulationTestDriver - otbChangeNoDataValueFilter) + otbChangeNoDataValueFilter) + +otb_add_test(NAME filteringImageManipulationImageToNoDataMaskFilter COMMAND otbImageManipulationTestDriver + otbImageToNoDataMaskFilter) diff --git a/Modules/Filtering/ImageManipulation/test/otbImageManipulationTestDriver.cxx b/Modules/Filtering/ImageManipulation/test/otbImageManipulationTestDriver.cxx index fa7931fd8893cf62587eb3ff7292bec2f3315d2c..5fd89db0d90a1a5f1ba324abde1dd9a236884324 100644 --- a/Modules/Filtering/ImageManipulation/test/otbImageManipulationTestDriver.cxx +++ b/Modules/Filtering/ImageManipulation/test/otbImageManipulationTestDriver.cxx @@ -77,4 +77,5 @@ void RegisterTests() REGISTER_TEST(otbEuclideanDistanceMetricWithMissingValue); REGISTER_TEST(otbEuclideanDistanceMetricWithMissingValueNew); REGISTER_TEST(otbChangeNoDataValueFilter); + REGISTER_TEST(otbImageToNoDataMaskFilter); } diff --git a/Modules/Filtering/ImageManipulation/test/otbImageToNoDataMaskFilter.cxx b/Modules/Filtering/ImageManipulation/test/otbImageToNoDataMaskFilter.cxx new file mode 100644 index 0000000000000000000000000000000000000000..109a2ec7d44c4ad2ff420337613260d64ba66b8a --- /dev/null +++ b/Modules/Filtering/ImageManipulation/test/otbImageToNoDataMaskFilter.cxx @@ -0,0 +1,88 @@ +/*========================================================================= + + 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 "otbImageToNoDataMaskFilter.h" +#include "itkImageRegionIterator.h" + +int otbImageToNoDataMaskFilter(int itkNotUsed(argc),char * itkNotUsed(argv) []) +{ + + // Build an image + typedef otb::Image<double> ImageType; + ImageType::Pointer img = ImageType::New(); + + ImageType::SizeType size; + size.Fill(20); + + ImageType::RegionType region; + region.SetSize(size); + + // Fill it with a default value + img->SetRegions(region); + img->Allocate(); + img->FillBuffer(10); + + // Write no-data flags to it + std::vector<bool> flags(1,true); + std::vector<double> values(1,-10.); + otb::WriteNoDataFlags(flags,values,img->GetMetaDataDictionary()); + + // Fill half of the pixels with no-data values + itk::ImageRegionIterator<ImageType> it(img,region); + unsigned int count = 0; + for(it.GoToBegin();!it.IsAtEnd();++it,++count) + { + if (count%2 == 0) + it.Set(-10.); + } + + // Instanciate filter + typedef otb::ImageToNoDataMaskFilter<ImageType,ImageType> FilterType; + FilterType::Pointer filter = FilterType::New(); + + filter->SetInput(img); + filter->SetInsideValue(255); + filter->SetOutsideValue(0); + filter->Update(); + + // Check output + it = itk::ImageRegionIterator<ImageType>(filter->GetOutput(),region); + count = 0; + + bool failed = false; + + for(it.GoToBegin();!it.IsAtEnd();++it,++count) + { + if (count%2 == 0 and it.Get()!=0) + { + std::cerr<<"Pixel should be masked"<<std::endl; + failed = true; + } + else if(count%2 == 1 and it.Get()!=255) + { + std::cerr<<"Pixel should not be masked"<<std::endl; + failed = true; + } + } + + if(failed) + return EXIT_FAILURE; + + return EXIT_SUCCESS; +}