diff --git a/Core/Code/Testing/files.cmake b/Core/Code/Testing/files.cmake index 353d886d80..0906969728 100644 --- a/Core/Code/Testing/files.cmake +++ b/Core/Code/Testing/files.cmake @@ -1,145 +1,146 @@ # tests with no extra command line parameter set(MODULE_TESTS mitkAccessByItkTest.cpp mitkCoreObjectFactoryTest.cpp mitkMaterialTest.cpp mitkActionTest.cpp mitkDispatcherTest.cpp mitkEnumerationPropertyTest.cpp mitkEventTest.cpp #mitkEventConfigTest.cpp ## needs to be re-written, test indirect since EventConfig is no longer exported as interface Bug 14529 mitkFocusManagerTest.cpp mitkGenericPropertyTest.cpp mitkGeometry3DTest.cpp mitkGeometryDataToSurfaceFilterTest.cpp mitkGlobalInteractionTest.cpp mitkImageDataItemTest.cpp #mitkImageMapper2DTest.cpp mitkImageGeneratorTest.cpp mitkBaseDataTest.cpp #mitkImageToItkTest.cpp mitkImportItkImageTest.cpp + mitkGrabItkImageMemoryTest.cpp mitkInstantiateAccessFunctionTest.cpp mitkInteractorTest.cpp #mitkITKThreadingTest.cpp mitkLevelWindowTest.cpp mitkMessageTest.cpp #mitkPipelineSmartPointerCorrectnessTest.cpp mitkPixelTypeTest.cpp mitkPlaneGeometryTest.cpp mitkPointSetFileIOTest.cpp mitkPointSetTest.cpp mitkPointSetWriterTest.cpp mitkPointSetReaderTest.cpp mitkPointSetInteractorTest.cpp mitkPropertyTest.cpp mitkPropertyListTest.cpp #mitkRegistrationBaseTest.cpp #mitkSegmentationInterpolationTest.cpp mitkSlicedGeometry3DTest.cpp mitkSliceNavigationControllerTest.cpp mitkStateMachineTest.cpp ##mitkStateMachineContainerTest.cpp ## rewrite test, indirect since no longer exported Bug 14529 mitkStateTest.cpp mitkSurfaceTest.cpp mitkSurfaceToSurfaceFilterTest.cpp mitkTimeSlicedGeometryTest.cpp mitkTransitionTest.cpp mitkUndoControllerTest.cpp mitkVtkWidgetRenderingTest.cpp mitkVerboseLimitedLinearUndoTest.cpp mitkWeakPointerTest.cpp mitkTransferFunctionTest.cpp #mitkAbstractTransformGeometryTest.cpp mitkStepperTest.cpp itkTotalVariationDenoisingImageFilterTest.cpp mitkRenderingManagerTest.cpp vtkMitkThickSlicesFilterTest.cpp mitkNodePredicateSourceTest.cpp mitkVectorTest.cpp mitkClippedSurfaceBoundsCalculatorTest.cpp #QmitkRenderingTestHelper.cpp mitkExceptionTest.cpp mitkExtractSliceFilterTest.cpp mitkLogTest.cpp mitkImageDimensionConverterTest.cpp mitkLoggingAdapterTest.cpp mitkUIDGeneratorTest.cpp mitkShaderRepositoryTest.cpp ) # test with image filename as an extra command line parameter set(MODULE_IMAGE_TESTS mitkPlanePositionManagerTest.cpp mitkSurfaceVtkWriterTest.cpp #mitkImageSliceSelectorTest.cpp mitkImageTimeSelectorTest.cpp # mitkVtkPropRendererTest.cpp mitkDataNodeFactoryTest.cpp #mitkSTLFileReaderTest.cpp mitkImageAccessorTest.cpp ) # list of images for which the tests are run set(MODULE_TESTIMAGES # Pic-Factory no more available in Core, test images now in .nrrd format US4DCyl.nrrd Pic3D.nrrd Pic2DplusT.nrrd BallBinary30x30x30.nrrd binary.stl ball.stl ) set(MODULE_CUSTOM_TESTS #mitkLabeledImageToSurfaceFilterTest.cpp #mitkExternalToolsTest.cpp mitkDataStorageTest.cpp mitkDataNodeTest.cpp mitkDicomSeriesReaderTest.cpp mitkDICOMLocaleTest.cpp mitkEventMapperTest.cpp mitkNodeDependentPointSetInteractorTest.cpp mitkStateMachineFactoryTest.cpp mitkPointSetLocaleTest.cpp mitkImageTest.cpp mitkImageWriterTest.cpp mitkImageVtkMapper2DTest.cpp mitkImageVtkMapper2DLevelWindowTest.cpp mitkImageVtkMapper2DOpacityTest.cpp mitkImageVtkMapper2DColorTest.cpp mitkImageVtkMapper2DSwivelTest.cpp mitkImageVtkMapper2DTransferFunctionTest.cpp mitkIOUtilTest.cpp mitkSurfaceVtkMapper3DTest mitkSurfaceVtkMapper3DTexturedSphereTest.cpp mitkVolumeCalculatorTest.cpp mitkLevelWindowManagerTest.cpp ) set(MODULE_RESOURCE_FILES Interactions/AddAndRemovePoints.xml Interactions/globalConfig.xml Interactions/StatemachineTest.xml Interactions/StatemachineConfigTest.xml ) # Create an artificial module initializing class for # the usServiceListenerTest.cpp usFunctionGenerateModuleInit(testdriver_init_file NAME ${MODULE_NAME}TestDriver DEPENDS "Mitk" VERSION "0.1.0" EXECUTABLE ) # Embed the resources set(testdriver_resources ) usFunctionEmbedResources(testdriver_resources EXECUTABLE_NAME ${MODULE_NAME}TestDriver ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/Resources FILES ${MODULE_RESOURCE_FILES} ) set(TEST_CPP_FILES ${testdriver_init_file} ${testdriver_resources}) diff --git a/Core/Code/Testing/mitkGrabItkImageMemoryTest.cpp b/Core/Code/Testing/mitkGrabItkImageMemoryTest.cpp new file mode 100644 index 0000000000..dd11d75021 --- /dev/null +++ b/Core/Code/Testing/mitkGrabItkImageMemoryTest.cpp @@ -0,0 +1,125 @@ +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ + +#include "mitkTestingMacros.h" +#include "mitkITKImageImport.h" + +#include "mitkImagePixelReadAccessor.h" + +#include + +#include "mitkImageAccessByItk.h" + +/** + * An ITK-based filter for thresholding. + * + * The filter represents the typical usage of ITK-like filters inside MITK. It is to be called for an mitk::Image + * by using the AccessByItk macro. The filter executes the binary threshold filter and imports the result into the + * output by using the ImportItkImage method. + * + * @param output mitk::Image to hold the result of the filter + * @param th[] two double values to set the lower/upper threshold + */ +template +static void ItkThresholdFilter( + const itk::Image* image, + mitk::Image::Pointer& output, + const double th[]) +{ + typedef itk::Image InputImageType; + typedef itk::Image OutputImageType; + typedef itk::ThresholdImageFilter< InputImageType > ThresholdFilterType; + + typename ThresholdFilterType::Pointer thresholder = + ThresholdFilterType::New(); + thresholder->SetInput(image); + thresholder->ThresholdOutside(th[0], th[1]); + thresholder->Update(); + + try + { + output = mitk::GrabItkImageMemory(thresholder->GetOutput()); + } + catch(itk::ExceptionObject&) + { + MITK_TEST_FAILED_MSG(<<"Thresholding computation failed"); + } +} + +/** + * Creates an mitk::Image, executes the binary threshold filter through AccessByItk and + * checks whether the image data was correctly imported back to an mitk::Image. + */ +template< typename TPixel> +bool Assert_ItkImportWithinAccessByItkSucceded_ReturnsTrue() +{ + // data for 3x3x3 image + const unsigned int dimensions[3] = {3,3,3}; + TPixel* image_data = new TPixel[27]; + + // ground truth for result check + TPixel* ground_truth = new TPixel[27]; + double threshold[2] = { 90.0, 180.0 }; + + // fill image + for( unsigned int i=0; i<27; i++) + { + image_data[i] = static_cast(i * 10); + + ground_truth[i] = 0; + if( image_data[i] >= threshold[0] && image_data[i] <= threshold[1] ) + ground_truth[i] = static_cast(i * 10); + + } + + mitk::Image::Pointer input = mitk::Image::New(); + input->Initialize( mitk::MakeScalarPixelType(), 3, dimensions ); + input->SetImportVolume( image_data ); + + mitk::Image::Pointer output = mitk::Image::New(); + //output->Initialize(input); + AccessByItk_2(input, ItkThresholdFilter, output, threshold ); + + mitk::ImagePixelReadAccessor< TPixel, 3 > readAccessor( output ); + const TPixel* output_data = readAccessor.GetConstData(); + + bool equal = true; + for( unsigned int i=0; i<27; i++) + { + + equal &= (ground_truth[i] == output_data[i]); + if(!equal) + { + MITK_INFO << " :: At position " << i << " : " <();// "Import successful on 3D short"); + Assert_ItkImportWithinAccessByItkSucceded_ReturnsTrue();// "Import succesfull on float"); + Assert_ItkImportWithinAccessByItkSucceded_ReturnsTrue();// "Import succesfull on uchar"); + Assert_ItkImportWithinAccessByItkSucceded_ReturnsTrue();// "Import succesfull on int"); + +} diff --git a/Core/Code/Testing/mitkImportItkImageTest.cpp b/Core/Code/Testing/mitkImportItkImageTest.cpp index b09adaae7d..4ce062c843 100644 --- a/Core/Code/Testing/mitkImportItkImageTest.cpp +++ b/Core/Code/Testing/mitkImportItkImageTest.cpp @@ -1,339 +1,240 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "mitkTestingMacros.h" #include "mitkITKImageImport.h" #include "mitkImagePixelReadAccessor.h" #include #include -#include - -#include "mitkImageAccessByItk.h" - /** * Create a test image with random pixel values. The image size is determined by the input parameter. * * @param size the number of voxels in each dimension */ template typename itk::Image::Pointer CreateTestImageRandom( short int size ) { typedef typename itk::Image< TPixel, VDimension> ImageType; typedef typename ImageType::Pointer ImagePointer; itk::Size regionSize; regionSize.Fill(size); typename itk::RandomImageSource::Pointer randomImageSource = itk::RandomImageSource::New(); randomImageSource->SetNumberOfThreads(1); // to produce non-random results randomImageSource->SetSize(regionSize); randomImageSource->Update(); return randomImageSource->GetOutput(); } /** * Create a test image with a single pixel value. The image size is determined by the input parameter. * * @param value the pixel value the created image is filled with * @param size the number of voxels in each dimension */ template typename itk::Image::Pointer CreateTestImageFixedValue(size_t size, TPixel value) { typedef typename itk::Image< TPixel, VDimension> ImageType; typedef typename ImageType::Pointer ImagePointer; typename ImageType::RegionType imageRegion; typename ImageType::RegionType::SizeType regionSize; regionSize.Fill(size); typename ImageType::RegionType::IndexType regionIndex; regionIndex.Fill(0); imageRegion.SetSize( regionSize ); imageRegion.SetIndex( regionIndex ); typename ImageType::SpacingType imageSpacing; imageSpacing.Fill(1.0f); typename ImageType::PointType imageOrigin; imageOrigin.Fill(0.0f); ImagePointer itkImage = ImageType::New(); itkImage->SetRegions( imageRegion ); itkImage->SetOrigin( imageOrigin ); itkImage->SetSpacing( imageSpacing ); itkImage->Allocate(); itkImage->FillBuffer( value ); return itkImage; } -/** - * An ITK-based filter for thresholding. - * - * The filter represents the typical usage of ITK-like filters inside MITK. It is to be called for an mitk::Image - * by using the AccessByItk macro. The filter executes the binary threshold filter and imports the result into the - * output by using the ImportItkImage method. - * - * @param output mitk::Image to hold the result of the filter - * @param th[] two double values to set the lower/upper threshold - */ -template -static void ItkThresholdFilter( - const itk::Image* image, - mitk::Image::Pointer& output, - const double th[]) -{ - typedef itk::Image InputImageType; - typedef itk::Image OutputImageType; - typedef itk::ThresholdImageFilter< InputImageType > ThresholdFilterType; - - typename ThresholdFilterType::Pointer thresholder = - ThresholdFilterType::New(); - thresholder->SetInput(image); - thresholder->ThresholdOutside(th[0], th[1]); - thresholder->Update(); - - try - { - output = mitk::GrabItkImageMemory(thresholder->GetOutput()); - } - catch(itk::ExceptionObject&) - { - MITK_TEST_FAILED_MSG(<<"Thresholding computation failed"); - } -} - -/** - * Creates an mitk::Image, executes the binary threshold filter through AccessByItk and - * checks whether the image data was correctly imported back to an mitk::Image. - */ -template< typename TPixel> -bool Assert_ItkImportWithinAccessByItkSucceded_ReturnsTrue() -{ - // data for 3x3x3 image - const unsigned int dimensions[3] = {3,3,3}; - TPixel* image_data = new TPixel[27]; - - // ground truth for result check - TPixel* ground_truth = new TPixel[27]; - double threshold[2] = { 90.0, 180.0 }; - // fill image - for( unsigned int i=0; i<27; i++) - { - image_data[i] = static_cast(i * 10); - - ground_truth[i] = 0; - if( image_data[i] >= threshold[0] && image_data[i] <= threshold[1] ) - ground_truth[i] = static_cast(i * 10); - - } - - mitk::Image::Pointer input = mitk::Image::New(); - input->Initialize( mitk::MakeScalarPixelType(), 3, dimensions ); - input->SetImportVolume( image_data ); - - mitk::Image::Pointer output = mitk::Image::New(); - //output->Initialize(input); - AccessByItk_2(input, ItkThresholdFilter, output, threshold ); - - mitk::ImagePixelReadAccessor< TPixel, 3 > readAccessor( output ); - const TPixel* output_data = readAccessor.GetConstData(); - - bool equal = true; - for( unsigned int i=0; i<27; i++) - { - - equal &= (ground_truth[i] == output_data[i]); - if(!equal) - { - MITK_INFO << " :: At position " << i << " : " < bool Assert_ImageMetaData_AreEqual( typename ImageType::Pointer itkImage, mitk::Image::Pointer mitkImage ) { bool return_value = true; typename ImageType::RegionType itkRegion = itkImage->GetLargestPossibleRegion(); typename ImageType::SizeType itkImageSize = itkRegion.GetSize(); // check dimension for( unsigned int idx=0; idx < mitkImage->GetDimension(); idx++) { return_value &= ( itkImageSize[idx] == mitkImage->GetDimension(idx) ); } MITK_TEST_CONDITION( return_value, " - Dimensions equal!") // check pixel type bool ptype_compare = ( mitkImage->GetPixelType() == mitk::MakePixelType() ); return_value &= ptype_compare; MITK_TEST_CONDITION( ptype_compare, " - Pixel types equal!") mitk::Geometry3D* imageGeometry = mitkImage->GetGeometry(); const mitk::Point3D origin = imageGeometry->GetOrigin(); bool origin_compare = true; for( unsigned int idx=0; idx < 3; idx++) { origin_compare &= ( itkImage->GetOrigin()[idx] == origin[idx] ); } return_value &= origin_compare; MITK_TEST_CONDITION( origin_compare, " - Origin equals!") return return_value; } /** * Generates a random itk image and imports it to mitk image through ImportItkImage and compares the values * voxel-wise afterwards */ template void Assert_ItkImageImportRandomValuesSucceded_ReturnsTrue() { std::stringstream msg; msg << "Current type: (Random Image, " << VDimension << "D):" << typeid(TPixel).name() << "\n"; std::cout << msg.str(); bool assert_value = true; typedef typename itk::Image< TPixel, VDimension> ImageType; typedef typename ImageType::Pointer ImagePointer; ImagePointer itkImage = CreateTestImageRandom(5); mitk::Image::Pointer output_import = mitk::ImportItkImage( itkImage ); itk::ImageRegionConstIteratorWithIndex< ImageType > iter( itkImage, itkImage->GetLargestPossibleRegion() ); iter.GoToBegin(); mitk::ImagePixelReadAccessor< TPixel, VDimension > readAccessor( output_import ); bool difference = false; while( !iter.IsAtEnd() ) { TPixel ref = iter.Get(); TPixel val = readAccessor.GetPixelByIndex( iter.GetIndex() ); difference |= ( ref != val ); if( difference ) { std::cout << iter.GetIndex() << ":" << ref << " ? " << val << "\n"; } ++iter; } assert_value = Assert_ImageMetaData_AreEqual( itkImage, output_import ); MITK_TEST_CONDITION( assert_value && (!difference), "Pixel values are same in voxel-wise comparison." ); } /** * Generates an itk image with fixed pixel value and imports it to mitk image through ImportItkImage * and compares the values voxel-wise afterwards */ template void Assert_ItkImageImportSucceded_ReturnsTrue() { std::stringstream msg; msg << "Current type: " << VDimension << "D):" << typeid(TPixel).name() << "\n"; std::cout << msg.str(); bool assert_value = true; typedef typename itk::Image< TPixel, VDimension> ImageType; typedef typename ImageType::Pointer ImagePointer; ImagePointer itkImage = CreateTestImageFixedValue(5, itk::NumericTraits::min()); mitk::Image::Pointer output_import = mitk::ImportItkImage( itkImage ); itk::ImageRegionConstIteratorWithIndex< ImageType > iter( itkImage, itkImage->GetLargestPossibleRegion() ); iter.GoToBegin(); mitk::ImagePixelReadAccessor< TPixel, VDimension > readAccessor( output_import ); bool difference = false; while( !iter.IsAtEnd() ) { TPixel ref = iter.Get(); TPixel val = readAccessor.GetPixelByIndex( iter.GetIndex() ); difference |= ( ref != val ); if( difference ) { std::cout << iter.GetIndex() << ":" << ref << " ? " << val << "\n"; } ++iter; } assert_value = Assert_ImageMetaData_AreEqual( itkImage, output_import ); MITK_TEST_CONDITION( assert_value && (!difference), "Pixel values are same in voxel-wise comparison." ); } int mitkImportItkImageTest(int /*argc*/, char* /*argv*/[]) { MITK_TEST_BEGIN("ImportItkImageTest") Assert_ItkImageImportSucceded_ReturnsTrue();// "Import succesfull on 3D short"); Assert_ItkImageImportSucceded_ReturnsTrue();// "Import succesfull on float"); Assert_ItkImageImportSucceded_ReturnsTrue();// "Import succesfull on uchar"); Assert_ItkImageImportSucceded_ReturnsTrue();// "Import succesfull on int"); Assert_ItkImageImportRandomValuesSucceded_ReturnsTrue();// "Import succesfull on 3D short"); Assert_ItkImageImportRandomValuesSucceded_ReturnsTrue();// "Import succesfull on float"); Assert_ItkImageImportRandomValuesSucceded_ReturnsTrue();// "Import succesfull on uchar"); Assert_ItkImageImportRandomValuesSucceded_ReturnsTrue();// "Import succesfull on int"); Assert_ItkImageImportRandomValuesSucceded_ReturnsTrue();// "Import succesfull on 3D short"); Assert_ItkImageImportRandomValuesSucceded_ReturnsTrue();// "Import succesfull on float"); Assert_ItkImageImportRandomValuesSucceded_ReturnsTrue();// "Import succesfull on uchar"); Assert_ItkImageImportRandomValuesSucceded_ReturnsTrue();// "Import succesfull on int"); - Assert_ItkImportWithinAccessByItkSucceded_ReturnsTrue();// "Import successful on 3D short"); - Assert_ItkImportWithinAccessByItkSucceded_ReturnsTrue();// "Import succesfull on float"); - Assert_ItkImportWithinAccessByItkSucceded_ReturnsTrue();// "Import succesfull on uchar"); - Assert_ItkImportWithinAccessByItkSucceded_ReturnsTrue();// "Import succesfull on int"); - - MITK_TEST_END() }