diff --git a/Modules/ToFProcessing/Testing/mitkToFDistanceImageToSurfaceFilterTest.cpp b/Modules/ToFProcessing/Testing/mitkToFDistanceImageToSurfaceFilterTest.cpp index efb0949ca7..5ba67156fe 100644 --- a/Modules/ToFProcessing/Testing/mitkToFDistanceImageToSurfaceFilterTest.cpp +++ b/Modules/ToFProcessing/Testing/mitkToFDistanceImageToSurfaceFilterTest.cpp @@ -1,222 +1,223 @@ /*=================================================================== 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 #include //#include #include +#include #include #include #include #include //#include //#include //#include #include #include #include /**Documentation * test for the class "ToFDistanceImageToSurfaceFilter". */ typedef mitk::ToFProcessingCommon::ToFPoint2D ToFPoint2D; typedef mitk::ToFProcessingCommon::ToFPoint3D ToFPoint3D; typedef mitk::ToFProcessingCommon::ToFScalarType ToFScalarType; int mitkToFDistanceImageToSurfaceFilterTest(int /* argc */, char* /*argv*/[]) { MITK_TEST_BEGIN("ToFDistanceImageToSurfaceFilter"); mitk::ToFDistanceImageToSurfaceFilter::Pointer filter = mitk::ToFDistanceImageToSurfaceFilter::New(); // create test image unsigned int dimX =204; unsigned int dimY =204; - mitk::Image::Pointer image = mitk::ToFTestingCommon::CreateTestImage(dimX,dimY); + mitk::Image::Pointer image = mitk::ImageGenerator::GenerateRandomImage(dimX,dimY); //initialize intrinsic parameters with some arbitrary values ToFScalarType focalLengthX = 295.78960; ToFScalarType focalLengthY = 296.348535; ToFScalarType k1=-0.36,k2=-0.14,p1=0.001,p2=-0.00; ToFPoint2D principalPoint; principalPoint[0] = 103.576546; principalPoint[1] = 100.1532; mitk::CameraIntrinsics::Pointer cameraIntrinsics = mitk::CameraIntrinsics::New(); cameraIntrinsics->SetFocalLength(focalLengthX,focalLengthY); cameraIntrinsics->SetPrincipalPoint(principalPoint[0],principalPoint[1]); cameraIntrinsics->SetDistorsionCoeffs(k1,k2,p1,p2); // test SetCameraIntrinsics() filter->SetCameraIntrinsics(cameraIntrinsics); MITK_TEST_CONDITION_REQUIRED((focalLengthX==filter->GetCameraIntrinsics()->GetFocalLengthX()),"Testing SetCameraIntrinsics with focalLength"); ToFPoint2D pp; pp[0] = filter->GetCameraIntrinsics()->GetPrincipalPointX(); pp[1] = filter->GetCameraIntrinsics()->GetPrincipalPointY(); MITK_TEST_CONDITION_REQUIRED(mitk::Equal(principalPoint,pp),"Testing SetCameraIntrinsics with principalPoint()"); // test SetInterPixelDistance() ToFPoint2D interPixelDistance; interPixelDistance[0] = 0.04564; interPixelDistance[1] = 0.0451564; filter->SetInterPixelDistance(interPixelDistance); ToFPoint2D ipD = filter->GetInterPixelDistance(); MITK_TEST_CONDITION_REQUIRED(mitk::Equal(ipD,interPixelDistance),"Testing Set/GetInterPixelDistance()"); filter->SetReconstructionMode(false); // test Set/GetInput() filter->SetInput(image); MITK_TEST_CONDITION_REQUIRED((image==filter->GetInput()),"Testing Set/GetInput()"); // test filter without subset MITK_INFO<<"Test filter "; // calculate focal length considering inter pixel distance ToFScalarType focalLength = (focalLengthX*interPixelDistance[0]+focalLengthY*interPixelDistance[1])/2.0; vtkSmartPointer expectedResult = vtkSmartPointer::New(); expectedResult->SetDataTypeToDouble(); unsigned int counter = 0; double* point = new double[3]; // MITK_INFO<<"Test"; // MITK_INFO<<"focal: "<GetPixelValueByIndex(index); ToFPoint3D coordinate = mitk::ToFProcessingCommon::IndexToCartesianCoordinatesWithInterpixdist(i,j,distance,focalLength,interPixelDistance,principalPoint); // if ((i==0)&&(j==0)) // { // MITK_INFO<<"Distance test: "<InsertPoint(pointID,point); } counter++; } } filter->Update(); mitk::Surface::Pointer resultSurface = filter->GetOutput(); vtkSmartPointer result = vtkSmartPointer::New(); result->SetDataTypeToDouble(); result = resultSurface->GetVtkPolyData()->GetPoints(); MITK_TEST_CONDITION_REQUIRED((expectedResult->GetNumberOfPoints()==result->GetNumberOfPoints()),"Test if number of points in surface is equal"); bool pointSetsEqual = true; for (unsigned int i=0; iGetNumberOfPoints(); i++) { double* expected = expectedResult->GetPoint(i); double* res = result->GetPoint(i); ToFPoint3D expectedPoint; expectedPoint[0] = expected[0]; expectedPoint[1] = expected[1]; expectedPoint[2] = expected[2]; ToFPoint3D resultPoint; resultPoint[0] = res[0]; resultPoint[1] = res[1]; resultPoint[2] = res[2]; if (!mitk::Equal(expectedPoint,resultPoint)) { // MITK_INFO << i; MITK_INFO<<"expected: "<GetNumberOfPoints(); i++) { double* expected = expectedResult->GetPoint(i); double* res = result->GetPoint(i); ToFPoint3D expectedPoint; expectedPoint[0] = expected[0]; expectedPoint[1] = expected[1]; expectedPoint[2] = expected[2]; ToFPoint3D resultPoint; resultPoint[0] = res[0]; resultPoint[1] = res[1]; resultPoint[2] = res[2]; ToFPoint3D expectedPointBackward = mitk::ToFProcessingCommon::CartesianToIndexCoordinatesWithInterpixdist(expectedPoint,focalLength,interPixelDistance,principalPoint); ToFPoint3D resultPointBackward = mitk::ToFProcessingCommon::CartesianToIndexCoordinatesWithInterpixdist(resultPoint,focalLength,interPixelDistance,principalPoint); if (!mitk::Equal(expectedPointBackward,resultPointBackward)) { // MITK_INFO << i; // MITK_INFO<<"expected: "<GetNumberOfPoints(); i++) { double* res = result->GetPoint(i); ToFPoint3D resultPoint; resultPoint[0] = res[0]; resultPoint[1] = res[1]; resultPoint[2] = res[2]; ToFPoint3D resultPointBackward = mitk::ToFProcessingCommon::CartesianToIndexCoordinatesWithInterpixdist(resultPoint,focalLength,interPixelDistance,principalPoint); mitk::Index3D pixelIndex; pixelIndex[0] = (int) (resultPointBackward[0]+0.5); pixelIndex[1] = (int) (resultPointBackward[1]+0.5); pixelIndex[2] = 0; if (!mitk::Equal(image->GetPixelValueByIndex(pixelIndex),resultPointBackward[2])) { // MITK_INFO<<"expected: "<< image->GetPixelValueByIndex(pixelIndex); // MITK_INFO<<"result: "<< resultPoint; compareToInput = false; } } MITK_TEST_CONDITION_REQUIRED(compareToInput,"Testing backward transformation compared to original image"); //clean up delete point; // expectedResult->Delete(); MITK_TEST_END(); } diff --git a/Modules/ToFProcessing/mitkToFTestingCommon.h b/Modules/ToFProcessing/mitkToFTestingCommon.h index a7ef8051ab..f9edea29e2 100644 --- a/Modules/ToFProcessing/mitkToFTestingCommon.h +++ b/Modules/ToFProcessing/mitkToFTestingCommon.h @@ -1,135 +1,96 @@ /*=================================================================== 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. ===================================================================*/ #ifndef mitkToFTestingCOMMON_H #define mitkToFTestingCOMMON_H #include "mitkToFProcessingExports.h" #include "mitkVector.h" #include #include "mitkToFProcessingCommon.h" #include "mitkPicFileReader.h" #include #include #include #include #include #include #include namespace mitk { class mitkToFProcessing_EXPORT ToFTestingCommon { public: -//creates a test image with given dimension filled with random values -inline static mitk::Image::Pointer CreateTestImage(unsigned int dimX = 100, unsigned int dimY = 100) -{ - typedef itk::Image ItkImageType2D; - typedef itk::ImageRegionIterator ItkImageRegionIteratorType2D; - - ItkImageType2D::Pointer image = ItkImageType2D::New(); - ItkImageType2D::IndexType start; - start[0] = 0; - start[1] = 0; - ItkImageType2D::SizeType size; - size[0] = dimX; - size[1] = dimY; - ItkImageType2D::RegionType region; - region.SetSize(size); - region.SetIndex( start); - ItkImageType2D::SpacingType spacing; - spacing[0] = 1.0; - spacing[1] = 1.0; - - image->SetRegions( region ); - image->SetSpacing ( spacing ); - image->Allocate(); - - //Correlate inten values to PixelIndex// - ItkImageRegionIteratorType2D imageIterator(image,image->GetLargestPossibleRegion()); - imageIterator.GoToBegin(); - itk::Statistics::MersenneTwisterRandomVariateGenerator::Pointer randomGenerator = itk::Statistics::MersenneTwisterRandomVariateGenerator::New(); - while (!imageIterator.IsAtEnd()) - { - double pixelValue = randomGenerator->GetUniformVariate(0.0,1000.0); - imageIterator.Set(pixelValue); - ++imageIterator; - } - mitk::Image::Pointer mitkImage = mitk::Image::New(); - mitk::CastToMitkImage(image,mitkImage); - return mitkImage; -} - //loads an image from file inline static mitk::Image::Pointer LoadImage( std::string filename ) { mitk::PicFileReader::Pointer reader = mitk::PicFileReader::New(); reader->SetFileName ( filename.c_str() ); reader->Update(); if ( reader->GetOutput() == NULL ) itkGenericExceptionMacro("File "<GetOutput(); return image; } //loads a surface from file inline static mitk::Surface::Pointer LoadSurface( std::string filename ) { mitk::STLFileReader::Pointer reader = mitk::STLFileReader::New(); reader->SetFileName( filename.c_str() ); reader->Update(); if ( reader->GetOutput() == NULL ) itkGenericExceptionMacro("File "<< filename <<" could not be read!"); mitk::Surface::Pointer surface = reader->GetOutput(); return surface; } inline static bool SaveImage( mitk::Image* data, std::string filename ) { std::string extension = itksys::SystemTools::GetFilenameLastExtension( filename ); if (extension == ".gz") { filename.assign( filename, 0, filename.length() - 7 ); // remove last 7 characters (.pic.gz) } else if (extension == ".pic") { filename.assign( filename, 0, filename.length() - 4 ); // remove last 4 characters } try { mitk::ImageWriter::Pointer imageWriter = mitk::ImageWriter::New(); imageWriter->SetInput(data); imageWriter->SetFileName(filename.c_str()); imageWriter->SetExtension(".pic.gz"); imageWriter->Write(); } catch ( std::exception& e ) { std::cerr << "Error during attempt to write '" << filename << "'.pic Exception says:" << std::endl; std::cerr << e.what() << std::endl; return false; } return true; } }; } #endif