Skip to content

Commit

Permalink
ENH: test for comparing output with stream driven imaging (SDI) and w…
Browse files Browse the repository at this point in the history
…ithout

based on itkWarpImageFilterTest2.cxx and itkResampleImageTest.cxx
  • Loading branch information
romangrothausmann committed Jan 23, 2019
1 parent ff8e5df commit a6682b0
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 26 deletions.
5 changes: 3 additions & 2 deletions Modules/Filtering/ImageGrid/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ itkResampleImageTest3.cxx
itkResampleImageTest4.cxx
itkResampleImageTest5.cxx
itkResampleImageTest6.cxx
itkResampleImageTest7.cxx
itkResamplePhasedArray3DSpecialCoordinatesImageTest.cxx
itkPushPopTileImageFilterTest.cxx
itkShrinkImageStreamingTest.cxx
Expand Down Expand Up @@ -283,8 +284,8 @@ itk_add_test(NAME itkResampleImageTest6
--compare DATA{Baseline/ResampleImageTest6.png}
${ITK_TEST_OUTPUT_DIR}/ResampleImageTest6.png
itkResampleImageTest6 10 ${ITK_TEST_OUTPUT_DIR}/ResampleImageTest6.png)
itk_add_test(NAME itkResampleImageTest4
COMMAND ITKImageGridTestDriver itkResampleImageTest4)
itk_add_test(NAME itkResampleImageTest7
COMMAND ITKImageGridTestDriver itkResampleImageTest7)
itk_add_test(NAME itkResamplePhasedArray3DSpecialCoordinatesImageTest
COMMAND ITKImageGridTestDriver itkResamplePhasedArray3DSpecialCoordinatesImageTest)
itk_add_test(NAME itkPushPopTileImageFilterTest
Expand Down
112 changes: 88 additions & 24 deletions Modules/Filtering/ImageGrid/test/itkResampleImageTest7.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@

#include "itkAffineTransform.h"
#include "itkResampleImageFilter.h"
#include "itkTimeProbe.h"
#include "itkPipelineMonitorImageFilter.h"
#include "itkStreamingImageFilter.h"
#include "itkTestingMacros.h"
#include "itkMath.h"

int itkResampleImageTest4(int argc, char * argv [] )
/* itkResampleImageFilter output compared to streamed output
*/

int itkResampleImageTest7(int argc, char * argv [] )
{

constexpr unsigned int NDimensions = 2;
Expand All @@ -42,13 +47,6 @@ int itkResampleImageTest4(int argc, char * argv [] )

using InterpolatorType = itk::LinearInterpolateImageFunction<ImageType,CoordRepType>;


float scaling = 10.0;
if (argc > 1)
{
scaling = std::stod( argv[1] );
}

// Create and configure an image
ImagePointerType image = ImageType::New();
ImageIndexType index = {{0, 0}};
Expand All @@ -60,13 +58,6 @@ int itkResampleImageTest4(int argc, char * argv [] )
image->SetBufferedRegion( region );
image->Allocate();

auto newDims = static_cast<unsigned int>( 64*scaling );
ImageSizeType osize = {{newDims, newDims}};

ImageType::SpacingType spacing;
spacing[0] = size[0] / static_cast<double>(osize[0]);
spacing[1] = size[1] / static_cast<double>(osize[1]);

// Fill image with a ramp
itk::ImageRegionIteratorWithIndex<ImageType> iter(image, region);
PixelType value;
Expand Down Expand Up @@ -96,8 +87,8 @@ int itkResampleImageTest4(int argc, char * argv [] )
resample->SetInput(image);
TEST_SET_GET_VALUE( image, resample->GetInput() );

resample->SetSize(osize);
TEST_SET_GET_VALUE( osize, resample->GetSize() );
resample->SetSize(size);
TEST_SET_GET_VALUE( size, resample->GetSize() );

resample->SetTransform(aff);
TEST_SET_GET_VALUE( aff, resample->GetTransform() );
Expand All @@ -114,16 +105,89 @@ int itkResampleImageTest4(int argc, char * argv [] )
resample->SetOutputOrigin( origin );
TEST_SET_GET_VALUE( origin, resample->GetOutputOrigin() );

ImageType::SpacingType spacing;
spacing.Fill( 1.0 );
resample->SetOutputSpacing( spacing );
TEST_SET_GET_VALUE( spacing, resample->GetOutputSpacing() );

// Run the resampling filter
itk::TimeProbe clock;
clock.Start();
resample->Update();
clock.Stop();
using MonitorFilter = itk::PipelineMonitorImageFilter<ImageType>;
MonitorFilter::Pointer monitor = MonitorFilter::New();

using StreamerType = itk::StreamingImageFilter<ImageType,ImageType>;
StreamerType::Pointer streamer = StreamerType::New();

std::cout << "Test with normal AffineTransform." << std::endl;
monitor->SetInput( resample->GetOutput() );
streamer->SetInput( monitor->GetOutput() );

unsigned char numStreamDiv;

// Run the resampling filter without streaming, i.e. 1 StreamDivisions
numStreamDiv= 1; // do not split, i.e. do not stream
try
{
streamer->SetNumberOfStreamDivisions(numStreamDiv);
streamer->Update();
}
catch( itk::ExceptionObject & excp )
{
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}

if (!monitor->VerifyAllInputCanStream(numStreamDiv))
{
std::cout << "Avoiding streaming failed to execute as expected!" << std::endl;
std::cout << monitor;
return EXIT_FAILURE;
}

ImagePointerType outputNoSDI= streamer->GetOutput(); // save output for later comparison
outputNoSDI->DisconnectPipeline(); // disconnect to create new output

// Run the resampling filter with streaming
numStreamDiv= 8; // split into numStream pieces for streaming.
try
{
streamer->SetNumberOfStreamDivisions(numStreamDiv);
streamer->Update();
}
catch( itk::ExceptionObject & excp )
{
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}

std::cout << "Resampling from " << size << " to " << osize << " took " << clock.GetMean() << " s" << std::endl;
if (!monitor->VerifyAllInputCanStream(numStreamDiv))
{
std::cout << "Streaming failed to execute as expected!" << std::endl;
std::cout << monitor;
return EXIT_FAILURE;
}

ImagePointerType outputSDI= streamer->GetOutput();
outputSDI->DisconnectPipeline();

itk::ImageRegionIterator<ImageType>
itNoSDI(outputNoSDI, outputNoSDI->GetLargestPossibleRegion()),
itSDI(outputSDI, outputSDI->GetLargestPossibleRegion());
for(itNoSDI.GoToBegin(), itSDI.GoToBegin();
!itNoSDI.IsAtEnd() && !itSDI.IsAtEnd();
++itNoSDI, ++itSDI)
{
if(itk::Math::NotAlmostEquals( itNoSDI.Value(), itSDI.Value() ))
{
std::cout << "Pixels differ "
<< itNoSDI.Value() << " "
<< itSDI.Value() << std::endl;
return EXIT_FAILURE;
}
}
if(itNoSDI.IsAtEnd() != itSDI.IsAtEnd())
{
std::cout << "Iterators don't agree on end of image" << std::endl;
return EXIT_FAILURE;
}

std::cout << "Test passed." << std::endl;
return EXIT_SUCCESS;
Expand Down

0 comments on commit a6682b0

Please sign in to comment.