diff --git a/CMakeExternals/MITKData.cmake b/CMakeExternals/MITKData.cmake index 60ece04036..0fc7c8ae2d 100644 --- a/CMakeExternals/MITKData.cmake +++ b/CMakeExternals/MITKData.cmake @@ -1,35 +1,35 @@ #----------------------------------------------------------------------------- # MITK Data #----------------------------------------------------------------------------- # Sanity checks if(DEFINED MITK_DATA_DIR AND NOT EXISTS ${MITK_DATA_DIR}) message(FATAL_ERROR "MITK_DATA_DIR variable is defined but corresponds to non-existing directory") endif() set(proj MITK-Data) set(proj_DEPENDENCIES) set(MITK-Data_DEPENDS ${proj}) if(BUILD_TESTING) - set(revision_tag e44055a4) + set(revision_tag 0acad502) # ^^^^^^^^ these are just to check correct length of hash part ExternalProject_Add(${proj} URL ${MITK_THIRDPARTY_DOWNLOAD_PREFIX_URL}/MITK-Data_${revision_tag}.tar.gz UPDATE_COMMAND "" CONFIGURE_COMMAND "" BUILD_COMMAND "" INSTALL_COMMAND "" DEPENDS ${proj_DEPENDENCIES} ) set(MITK_DATA_DIR ${ep_source_dir}/${proj}) else() mitkMacroEmptyExternalProject(${proj} "${proj_DEPENDENCIES}") endif(BUILD_TESTING) diff --git a/Modules/ImageStatistics/Testing/mitkImageStatisticsCalculatorTest.cpp b/Modules/ImageStatistics/Testing/mitkImageStatisticsCalculatorTest.cpp index 0e3c62345e..9f57aa9705 100644 --- a/Modules/ImageStatistics/Testing/mitkImageStatisticsCalculatorTest.cpp +++ b/Modules/ImageStatistics/Testing/mitkImageStatisticsCalculatorTest.cpp @@ -1,496 +1,409 @@ /*=================================================================== 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 "mitkStandardFileLocations.h" -#include "mitkDicomSeriesReader.h" +#include "mitkTestFixture.h" #include "mitkTestingMacros.h" + #include "mitkImageStatisticsCalculator.h" #include "mitkPlanarPolygon.h" #include "mitkDicomSeriesReader.h" -#include - -#include "vtkStreamingDemandDrivenPipeline.h" - -#include -#include - -//#include /** -* \brief Test class for mitkImageStatisticsCalculator -* -* This test covers: -* - instantiation of an ImageStatisticsCalculator class -* - correctness of statistics when using PlanarFigures for masking -*/ -class mitkImageStatisticsCalculatorTestClass + * \brief Test class for mitkImageStatisticsCalculator + * + * This test covers: + * - instantiation of an ImageStatisticsCalculator class + * - correctness of statistics when using PlanarFigures for masking + */ +class mitkImageStatisticsCalculatorTestSuite : public mitk::TestFixture { + CPPUNIT_TEST_SUITE(mitkImageStatisticsCalculatorTestSuite); + MITK_TEST(TestUninitializedImage); + MITK_TEST(TestCase1); + MITK_TEST(TestCase2); + MITK_TEST(TestCase3); + MITK_TEST(TestCase4); + MITK_TEST(TestCase5); + MITK_TEST(TestCase6); + MITK_TEST(TestCase7); + MITK_TEST(TestCase8); + MITK_TEST(TestCase9); + MITK_TEST(TestCase10); + MITK_TEST(TestCase11); + MITK_TEST(TestCase12); + CPPUNIT_TEST_SUITE_END(); public: - struct testCase - { - int id; - mitk::PlanarFigure::Pointer figure; - double mean; - double sd; - }; + void setUp(); + void TestUninitializedImage(); - // calculate statistics for the given image and planarpolygon - static const mitk::ImageStatisticsCalculator::Statistics TestStatistics( mitk::Image::Pointer image, mitk::PlanarFigure::Pointer polygon ) - { - mitk::ImageStatisticsCalculator::Pointer statisticsCalculator = mitk::ImageStatisticsCalculator::New(); - statisticsCalculator->SetImage( image ); - statisticsCalculator->SetMaskingModeToPlanarFigure(); - statisticsCalculator->SetPlanarFigure( polygon ); + void TestCase1(); + void TestCase2(); + void TestCase3(); + void TestCase4(); + void TestCase5(); + void TestCase6(); + void TestCase7(); + void TestCase8(); + void TestCase9(); + void TestCase10(); + void TestCase11(); + void TestCase12(); - statisticsCalculator->ComputeStatistics(); +private: - return statisticsCalculator->GetStatistics(); - } + mitk::Image::Pointer m_Image; + mitk::Geometry2D::Pointer m_Geometry; + // calculate statistics for the given image and planarpolygon + const mitk::ImageStatisticsCalculator::Statistics ComputeStatistics( mitk::Image::Pointer image, + mitk::PlanarFigure::Pointer polygon ); + void VerifyStatistics(const mitk::ImageStatisticsCalculator::Statistics& stats, + double testMean, double testSD); +}; - // returns a vector of defined test-cases - static std::vector InitializeTestCases( mitk::Geometry2D::Pointer geom ) +void mitkImageStatisticsCalculatorTestSuite::setUp() +{ + std::string filename = this->GetTestDataFilePath("ImageStatistics/testimage.dcm"); + if (filename.empty()) { - std::vector testCases; - - { - /***************************** - * one whole white pixel - * -> mean of 255 expected - ******************************/ - mitk::PlanarPolygon::Pointer figure1 = mitk::PlanarPolygon::New(); - figure1->SetGeometry2D( geom ); - mitk::Point2D pnt1; pnt1[0] = 10.5 ; pnt1[1] = 3.5; - figure1->PlaceFigure( pnt1 ); - - mitk::Point2D pnt2; pnt2[0] = 9.5; pnt2[1] = 3.5; - figure1->SetControlPoint( 1, pnt2, true ); - mitk::Point2D pnt3; pnt3[0] = 9.5; pnt3[1] = 4.5; - figure1->SetControlPoint( 2, pnt3, true ); - mitk::Point2D pnt4; pnt4[0] = 10.5; pnt4[1] = 4.5; - figure1->SetControlPoint( 3, pnt4, true ); - figure1->GetPolyLine(0); - - testCase test; - test.id = testCases.size(); - test.figure = figure1; - test.mean = 255.0; - test.sd = 0.0; - - testCases.push_back( test ); - } - - { - /***************************** - * half pixel in x-direction (white) - * -> mean of 255 expected - ******************************/ - mitk::PlanarPolygon::Pointer figure1 = mitk::PlanarPolygon::New(); - figure1->SetGeometry2D( geom ); - mitk::Point2D pnt1; pnt1[0] = 10.0 ; pnt1[1] = 3.5; - figure1->PlaceFigure( pnt1 ); - - mitk::Point2D pnt2; pnt2[0] = 9.5; pnt2[1] = 3.5; - figure1->SetControlPoint( 1, pnt2, true ); - mitk::Point2D pnt3; pnt3[0] = 9.5; pnt3[1] = 4.5; - figure1->SetControlPoint( 2, pnt3, true ); - mitk::Point2D pnt4; pnt4[0] = 10.0; pnt4[1] = 4.5; - figure1->SetControlPoint( 3, pnt4, true ); - figure1->GetPolyLine(0); - - testCase test; - test.id = testCases.size(); - test.figure = figure1; - test.mean = 255.0; - test.sd = 0.0; - - testCases.push_back( test ); - } - - { - /***************************** - * half pixel in diagonal-direction (white) - * -> mean of 255 expected - ******************************/ - mitk::PlanarPolygon::Pointer figure1 = mitk::PlanarPolygon::New(); - figure1->SetGeometry2D( geom ); - mitk::Point2D pnt1; pnt1[0] = 10.5 ; pnt1[1] = 3.5; - figure1->PlaceFigure( pnt1 ); - - mitk::Point2D pnt2; pnt2[0] = 9.5; pnt2[1] = 3.5; - figure1->SetControlPoint( 1, pnt2, true ); - mitk::Point2D pnt3; pnt3[0] = 9.5; pnt3[1] = 4.5; - figure1->SetControlPoint( 2, pnt3, true ); - figure1->GetPolyLine(0); - - testCase test; - test.id = testCases.size(); - test.figure = figure1; - test.mean = 255.0; - test.sd = 0.0; - - testCases.push_back( test ); - } - - - { - /***************************** - * one pixel (white) + 2 half pixels (white) + 1 half pixel (black) - * -> mean of 191.25 expected - ******************************/ - mitk::PlanarPolygon::Pointer figure1 = mitk::PlanarPolygon::New(); - figure1->SetGeometry2D( geom ); - mitk::Point2D pnt1; pnt1[0] = 1.1; pnt1[1] = 1.1; - figure1->PlaceFigure( pnt1 ); - - mitk::Point2D pnt2; pnt2[0] = 2.0; pnt2[1] = 2.0; - figure1->SetControlPoint( 1, pnt2, true ); - mitk::Point2D pnt3; pnt3[0] = 3.0; pnt3[1] = 1.0; - figure1->SetControlPoint( 2, pnt3, true ); - mitk::Point2D pnt4; pnt4[0] = 2.0; pnt4[1] = 0.0; - figure1->SetControlPoint( 3, pnt4, true ); - figure1->GetPolyLine(0); - - testCase test; - test.id = testCases.size(); - test.figure = figure1; - test.mean = 191.25; - test.sd = 127.5; - - testCases.push_back( test ); - } - - - { - /***************************** - * whole pixel (white) + half pixel (gray) in x-direction - * -> mean of 191.5 expected - ******************************/ - mitk::PlanarPolygon::Pointer figure1 = mitk::PlanarPolygon::New(); - figure1->SetGeometry2D( geom ); - mitk::Point2D pnt1; pnt1[0] = 11.0; pnt1[1] = 3.5; - figure1->PlaceFigure( pnt1 ); - - mitk::Point2D pnt2; pnt2[0] = 9.5; pnt2[1] = 3.5; - figure1->SetControlPoint( 1, pnt2, true ); - mitk::Point2D pnt3; pnt3[0] = 9.5; pnt3[1] = 4.5; - figure1->SetControlPoint( 2, pnt3, true ); - mitk::Point2D pnt4; pnt4[0] = 11.0; pnt4[1] = 4.5; - figure1->SetControlPoint( 3, pnt4, true ); - figure1->GetPolyLine(0); - - testCase test; - test.id = testCases.size(); - test.figure = figure1; - test.mean = 191.50; - test.sd = 89.80; - - testCases.push_back( test ); - } - - { - /***************************** - * quarter pixel (black) + whole pixel (white) + half pixel (gray) in x-direction - * -> mean of 191.5 expected - ******************************/ - mitk::PlanarPolygon::Pointer figure1 = mitk::PlanarPolygon::New(); - figure1->SetGeometry2D( geom ); - mitk::Point2D pnt1; pnt1[0] = 11.0; pnt1[1] = 3.5; - figure1->PlaceFigure( pnt1 ); - - mitk::Point2D pnt2; pnt2[0] = 9.25; pnt2[1] = 3.5; - figure1->SetControlPoint( 1, pnt2, true ); - mitk::Point2D pnt3; pnt3[0] = 9.25; pnt3[1] = 4.5; - figure1->SetControlPoint( 2, pnt3, true ); - mitk::Point2D pnt4; pnt4[0] = 11.0; pnt4[1] = 4.5; - figure1->SetControlPoint( 3, pnt4, true ); - figure1->GetPolyLine(0); - - testCase test; - test.id = testCases.size(); - test.figure = figure1; - test.mean = 191.5; - test.sd = 89.80; - - testCases.push_back( test ); - } - - { - /***************************** - * half pixel (black) + whole pixel (white) + half pixel (gray) in x-direction - * -> mean of 127.66 expected - ******************************/ - mitk::PlanarPolygon::Pointer figure1 = mitk::PlanarPolygon::New(); - figure1->SetGeometry2D( geom ); - mitk::Point2D pnt1; pnt1[0] = 11.0; pnt1[1] = 3.5; - figure1->PlaceFigure( pnt1 ); - - mitk::Point2D pnt2; pnt2[0] = 9.0; pnt2[1] = 3.5; - figure1->SetControlPoint( 1, pnt2, true ); - mitk::Point2D pnt3; pnt3[0] = 9.0; pnt3[1] = 4.0; - figure1->SetControlPoint( 2, pnt3, true ); - mitk::Point2D pnt4; pnt4[0] = 11.0; pnt4[1] = 4.0; - figure1->SetControlPoint( 3, pnt4, true ); - figure1->GetPolyLine(0); - - testCase test; - test.id = testCases.size(); - test.figure = figure1; - test.mean = 127.66; - test.sd = 127.5; - - testCases.push_back( test ); - } - - - { - /***************************** - * whole pixel (gray) - * -> mean of 128 expected - ******************************/ - mitk::PlanarPolygon::Pointer figure2 = mitk::PlanarPolygon::New(); - figure2->SetGeometry2D( geom ); - mitk::Point2D pnt1; pnt1[0] = 11.5; pnt1[1] = 10.5; - figure2->PlaceFigure( pnt1 ); - - mitk::Point2D pnt2; pnt2[0] = 11.5; pnt2[1] = 11.5; - figure2->SetControlPoint( 1, pnt2, true ); - mitk::Point2D pnt3; pnt3[0] = 12.5; pnt3[1] = 11.5; - figure2->SetControlPoint( 2, pnt3, true ); - mitk::Point2D pnt4; pnt4[0] = 12.5; pnt4[1] = 10.5; - figure2->SetControlPoint( 3, pnt4, true ); - figure2->GetPolyLine(0); - - testCase test; - test.id = testCases.size(); - test.figure = figure2; - test.mean = 128.0; - test.sd = 0.0; - - testCases.push_back( test ); - } - - { - /***************************** - * whole pixel (gray) + half pixel (white) in y-direction - * -> mean of 191.5 expected - ******************************/ - mitk::PlanarPolygon::Pointer figure2 = mitk::PlanarPolygon::New(); - figure2->SetGeometry2D( geom ); - mitk::Point2D pnt1; pnt1[0] = 11.5; pnt1[1] = 10.5; - figure2->PlaceFigure( pnt1 ); - - mitk::Point2D pnt2; pnt2[0] = 11.5; pnt2[1] = 12.0; - figure2->SetControlPoint( 1, pnt2, true ); - mitk::Point2D pnt3; pnt3[0] = 12.5; pnt3[1] = 12.0; - figure2->SetControlPoint( 2, pnt3, true ); - mitk::Point2D pnt4; pnt4[0] = 12.5; pnt4[1] = 10.5; - figure2->SetControlPoint( 3, pnt4, true ); - figure2->GetPolyLine(0); - - testCase test; - test.id = testCases.size(); - test.figure = figure2; - test.mean = 191.5; - test.sd = 89.80; - - testCases.push_back( test ); - } - - { - /***************************** - * 2 whole pixel (white) + 2 whole pixel (black) in y-direction - * -> mean of 127.66 expected - ******************************/ - mitk::PlanarPolygon::Pointer figure2 = mitk::PlanarPolygon::New(); - figure2->SetGeometry2D( geom ); - mitk::Point2D pnt1; pnt1[0] = 11.5; pnt1[1] = 10.5; - figure2->PlaceFigure( pnt1 ); - - mitk::Point2D pnt2; pnt2[0] = 11.5; pnt2[1] = 13.5; - figure2->SetControlPoint( 1, pnt2, true ); - mitk::Point2D pnt3; pnt3[0] = 12.5; pnt3[1] = 13.5; - figure2->SetControlPoint( 2, pnt3, true ); - mitk::Point2D pnt4; pnt4[0] = 12.5; pnt4[1] = 10.5; - figure2->SetControlPoint( 3, pnt4, true ); - figure2->GetPolyLine(0); - - testCase test; - test.id = testCases.size(); - test.figure = figure2; - test.mean = 127.66; - test.sd = 127.5; - - testCases.push_back( test ); - } - - - { - /***************************** - * 9 whole pixels (white) + 3 half pixels (white) - * + 3 whole pixel (black) [ + 3 slightly less than half pixels (black)] - * -> mean of 204.0 expected - ******************************/ - mitk::PlanarPolygon::Pointer figure2 = mitk::PlanarPolygon::New(); - figure2->SetGeometry2D( geom ); - mitk::Point2D pnt1; pnt1[0] = 0.5; pnt1[1] = 0.5; - figure2->PlaceFigure( pnt1 ); - - mitk::Point2D pnt2; pnt2[0] = 3.5; pnt2[1] = 3.5; - figure2->SetControlPoint( 1, pnt2, true ); - mitk::Point2D pnt3; pnt3[0] = 8.4999; pnt3[1] = 3.5; - figure2->SetControlPoint( 2, pnt3, true ); - mitk::Point2D pnt4; pnt4[0] = 5.4999; pnt4[1] = 0.5; - figure2->SetControlPoint( 3, pnt4, true ); - figure2->GetPolyLine(0); - - testCase test; - test.id = testCases.size(); - test.figure = figure2; - test.mean = 204.0; - test.sd = 105.58; - - testCases.push_back( test ); - } - - { - /***************************** - * half pixel (white) + whole pixel (white) + half pixel (black) - * -> mean of 212.66 expected - ******************************/ - mitk::PlanarPolygon::Pointer figure2 = mitk::PlanarPolygon::New(); - figure2->SetGeometry2D( geom ); - mitk::Point2D pnt1; pnt1[0] = 9.5; pnt1[1] = 0.5; - figure2->PlaceFigure( pnt1 ); - - mitk::Point2D pnt2; pnt2[0] = 9.5; pnt2[1] = 2.5; - figure2->SetControlPoint( 1, pnt2, true ); - mitk::Point2D pnt3; pnt3[0] = 11.5; pnt3[1] = 2.5; - figure2->SetControlPoint( 2, pnt3, true ); - figure2->GetPolyLine(0); - - testCase test; - test.id = testCases.size(); - test.figure = figure2; - test.mean = 212.66; - test.sd = 73.32; - - testCases.push_back( test ); - } - return testCases; + MITK_TEST_FAILED_MSG( << "Could not find test file" ) } - // loads the test image - static mitk::Image::Pointer GetTestImage() - { - mitk::StandardFileLocations::Pointer locator = mitk::StandardFileLocations::GetInstance(); + mitk::DicomSeriesReader::StringContainer file; + file.push_back( filename ); - const std::string filename = locator->FindFile("testimage.dcm", "Modules/MitkExt/Testing/Data"); - if (filename.empty()) - { - MITK_ERROR << "Could not find test file"; - return NULL; - } - else - { - MITK_INFO << "Found testimage.dcm"; - } + mitk::DicomSeriesReader* reader = new mitk::DicomSeriesReader; + mitk::DataNode::Pointer node = reader->LoadDicomSeries( file, false, false ); + m_Image = dynamic_cast( node->GetData() ); - itk::FilenamesContainer file; - file.push_back( filename ); + MITK_TEST_CONDITION_REQUIRED( m_Image.IsNotNull(), "Loading test image" ) - mitk::DicomSeriesReader* reader = new mitk::DicomSeriesReader; + m_Geometry = m_Image->GetSlicedGeometry()->GetGeometry2D(0); + MITK_TEST_CONDITION_REQUIRED( m_Geometry.IsNotNull(), "Getting image geometry" ) +} - mitk::DataNode::Pointer node = reader->LoadDicomSeries( file, false, false ); - mitk::Image::Pointer image = dynamic_cast( node->GetData() ); +void mitkImageStatisticsCalculatorTestSuite::TestCase1() +{ + /***************************** + * one whole white pixel + * -> mean of 255 expected + ******************************/ + mitk::PlanarPolygon::Pointer figure1 = mitk::PlanarPolygon::New(); + figure1->SetGeometry2D( m_Geometry ); + mitk::Point2D pnt1; pnt1[0] = 10.5 ; pnt1[1] = 3.5; + figure1->PlaceFigure( pnt1 ); + + mitk::Point2D pnt2; pnt2[0] = 9.5; pnt2[1] = 3.5; + figure1->SetControlPoint( 1, pnt2, true ); + mitk::Point2D pnt3; pnt3[0] = 9.5; pnt3[1] = 4.5; + figure1->SetControlPoint( 2, pnt3, true ); + mitk::Point2D pnt4; pnt4[0] = 10.5; pnt4[1] = 4.5; + figure1->SetControlPoint( 3, pnt4, true ); + figure1->GetPolyLine(0); + + this->VerifyStatistics(ComputeStatistics(m_Image, figure1.GetPointer()), 255.0, 0.0); +} - return image; - } +void mitkImageStatisticsCalculatorTestSuite::TestCase2() +{ + /***************************** + * half pixel in x-direction (white) + * -> mean of 255 expected + ******************************/ + mitk::PlanarPolygon::Pointer figure1 = mitk::PlanarPolygon::New(); + figure1->SetGeometry2D( m_Geometry ); + mitk::Point2D pnt1; pnt1[0] = 10.0 ; pnt1[1] = 3.5; + figure1->PlaceFigure( pnt1 ); + + mitk::Point2D pnt2; pnt2[0] = 9.5; pnt2[1] = 3.5; + figure1->SetControlPoint( 1, pnt2, true ); + mitk::Point2D pnt3; pnt3[0] = 9.5; pnt3[1] = 4.5; + figure1->SetControlPoint( 2, pnt3, true ); + mitk::Point2D pnt4; pnt4[0] = 10.0; pnt4[1] = 4.5; + figure1->SetControlPoint( 3, pnt4, true ); + figure1->GetPolyLine(0); + + this->VerifyStatistics(ComputeStatistics(m_Image, figure1.GetPointer()), 255.0, 0.0); +} -}; +void mitkImageStatisticsCalculatorTestSuite::TestCase3() +{ + /***************************** + * half pixel in diagonal-direction (white) + * -> mean of 255 expected + ******************************/ + mitk::PlanarPolygon::Pointer figure1 = mitk::PlanarPolygon::New(); + figure1->SetGeometry2D( m_Geometry ); + mitk::Point2D pnt1; pnt1[0] = 10.5 ; pnt1[1] = 3.5; + figure1->PlaceFigure( pnt1 ); + + mitk::Point2D pnt2; pnt2[0] = 9.5; pnt2[1] = 3.5; + figure1->SetControlPoint( 1, pnt2, true ); + mitk::Point2D pnt3; pnt3[0] = 9.5; pnt3[1] = 4.5; + figure1->SetControlPoint( 2, pnt3, true ); + figure1->GetPolyLine(0); + + this->VerifyStatistics(ComputeStatistics(m_Image, figure1.GetPointer()), 255.0, 0.0); +} +void mitkImageStatisticsCalculatorTestSuite::TestCase4() +{ + /***************************** + * one pixel (white) + 2 half pixels (white) + 1 half pixel (black) + * -> mean of 191.25 expected + ******************************/ + mitk::PlanarPolygon::Pointer figure1 = mitk::PlanarPolygon::New(); + figure1->SetGeometry2D( m_Geometry ); + mitk::Point2D pnt1; pnt1[0] = 1.1; pnt1[1] = 1.1; + figure1->PlaceFigure( pnt1 ); + + mitk::Point2D pnt2; pnt2[0] = 2.0; pnt2[1] = 2.0; + figure1->SetControlPoint( 1, pnt2, true ); + mitk::Point2D pnt3; pnt3[0] = 3.0; pnt3[1] = 1.0; + figure1->SetControlPoint( 2, pnt3, true ); + mitk::Point2D pnt4; pnt4[0] = 2.0; pnt4[1] = 0.0; + figure1->SetControlPoint( 3, pnt4, true ); + figure1->GetPolyLine(0); + + this->VerifyStatistics(ComputeStatistics(m_Image, figure1.GetPointer()), 191.25, 127.5); +} -int TestUnitilizedImage() +void mitkImageStatisticsCalculatorTestSuite::TestCase5() { /***************************** - * loading uninitialized image to datastorage - ******************************/ - std::cout << "Testing loading uninitialized image to datastorage:"; - try{ - MITK_TEST_FOR_EXCEPTION_BEGIN(mitk::Exception) - mitk::Image::Pointer image = mitk::Image::New(); - mitk::DataNode::Pointer node = mitk::DataNode::New(); - node->SetData(image); - - mitk::ImageStatisticsCalculator::Pointer is = mitk::ImageStatisticsCalculator::New(); - is->ComputeStatistics(); - MITK_TEST_FOR_EXCEPTION_END(mitk::Exception) - } - catch (const mitk::Exception&) - { - std::cout << "Success: Loading uninitialized image has thrown an exception." << std::endl; - } + * whole pixel (white) + half pixel (gray) in x-direction + * -> mean of 191.5 expected + ******************************/ + mitk::PlanarPolygon::Pointer figure1 = mitk::PlanarPolygon::New(); + figure1->SetGeometry2D( m_Geometry ); + mitk::Point2D pnt1; pnt1[0] = 11.0; pnt1[1] = 3.5; + figure1->PlaceFigure( pnt1 ); + + mitk::Point2D pnt2; pnt2[0] = 9.5; pnt2[1] = 3.5; + figure1->SetControlPoint( 1, pnt2, true ); + mitk::Point2D pnt3; pnt3[0] = 9.5; pnt3[1] = 4.5; + figure1->SetControlPoint( 2, pnt3, true ); + mitk::Point2D pnt4; pnt4[0] = 11.0; pnt4[1] = 4.5; + figure1->SetControlPoint( 3, pnt4, true ); + figure1->GetPolyLine(0); + + this->VerifyStatistics(ComputeStatistics(m_Image, figure1.GetPointer()), 191.50, 89.80); +} + +void mitkImageStatisticsCalculatorTestSuite::TestCase6() +{ + /***************************** + * quarter pixel (black) + whole pixel (white) + half pixel (gray) in x-direction + * -> mean of 191.5 expected + ******************************/ + mitk::PlanarPolygon::Pointer figure1 = mitk::PlanarPolygon::New(); + figure1->SetGeometry2D( m_Geometry ); + mitk::Point2D pnt1; pnt1[0] = 11.0; pnt1[1] = 3.5; + figure1->PlaceFigure( pnt1 ); + + mitk::Point2D pnt2; pnt2[0] = 9.25; pnt2[1] = 3.5; + figure1->SetControlPoint( 1, pnt2, true ); + mitk::Point2D pnt3; pnt3[0] = 9.25; pnt3[1] = 4.5; + figure1->SetControlPoint( 2, pnt3, true ); + mitk::Point2D pnt4; pnt4[0] = 11.0; pnt4[1] = 4.5; + figure1->SetControlPoint( 3, pnt4, true ); + figure1->GetPolyLine(0); + + this->VerifyStatistics(ComputeStatistics(m_Image, figure1.GetPointer()), 191.5, 89.80); +} + +void mitkImageStatisticsCalculatorTestSuite::TestCase7() +{ + /***************************** + * half pixel (black) + whole pixel (white) + half pixel (gray) in x-direction + * -> mean of 127.66 expected + ******************************/ + mitk::PlanarPolygon::Pointer figure1 = mitk::PlanarPolygon::New(); + figure1->SetGeometry2D( m_Geometry ); + mitk::Point2D pnt1; pnt1[0] = 11.0; pnt1[1] = 3.5; + figure1->PlaceFigure( pnt1 ); + + mitk::Point2D pnt2; pnt2[0] = 9.0; pnt2[1] = 3.5; + figure1->SetControlPoint( 1, pnt2, true ); + mitk::Point2D pnt3; pnt3[0] = 9.0; pnt3[1] = 4.0; + figure1->SetControlPoint( 2, pnt3, true ); + mitk::Point2D pnt4; pnt4[0] = 11.0; pnt4[1] = 4.0; + figure1->SetControlPoint( 3, pnt4, true ); + figure1->GetPolyLine(0); + + this->VerifyStatistics(ComputeStatistics(m_Image, figure1.GetPointer()), 127.66, 127.5); +} - return 0; +void mitkImageStatisticsCalculatorTestSuite::TestCase8() +{ + /***************************** + * whole pixel (gray) + * -> mean of 128 expected + ******************************/ + mitk::PlanarPolygon::Pointer figure2 = mitk::PlanarPolygon::New(); + figure2->SetGeometry2D( m_Geometry ); + mitk::Point2D pnt1; pnt1[0] = 11.5; pnt1[1] = 10.5; + figure2->PlaceFigure( pnt1 ); + + mitk::Point2D pnt2; pnt2[0] = 11.5; pnt2[1] = 11.5; + figure2->SetControlPoint( 1, pnt2, true ); + mitk::Point2D pnt3; pnt3[0] = 12.5; pnt3[1] = 11.5; + figure2->SetControlPoint( 2, pnt3, true ); + mitk::Point2D pnt4; pnt4[0] = 12.5; pnt4[1] = 10.5; + figure2->SetControlPoint( 3, pnt4, true ); + figure2->GetPolyLine(0); + + this->VerifyStatistics(ComputeStatistics(m_Image, figure2.GetPointer()), 128.0, 0.0); } -int mitkImageStatisticsCalculatorTest(int, char* []) +void mitkImageStatisticsCalculatorTestSuite::TestCase9() { - // always start with this! - MITK_TEST_BEGIN("mitkImageStatisticsCalculatorTest") + /***************************** + * whole pixel (gray) + half pixel (white) in y-direction + * -> mean of 191.5 expected + ******************************/ + mitk::PlanarPolygon::Pointer figure2 = mitk::PlanarPolygon::New(); + figure2->SetGeometry2D( m_Geometry ); + mitk::Point2D pnt1; pnt1[0] = 11.5; pnt1[1] = 10.5; + figure2->PlaceFigure( pnt1 ); + + mitk::Point2D pnt2; pnt2[0] = 11.5; pnt2[1] = 12.0; + figure2->SetControlPoint( 1, pnt2, true ); + mitk::Point2D pnt3; pnt3[0] = 12.5; pnt3[1] = 12.0; + figure2->SetControlPoint( 2, pnt3, true ); + mitk::Point2D pnt4; pnt4[0] = 12.5; pnt4[1] = 10.5; + figure2->SetControlPoint( 3, pnt4, true ); + figure2->GetPolyLine(0); + + this->VerifyStatistics(ComputeStatistics(m_Image, figure2.GetPointer()), 191.5, 89.80); +} - //QCoreApplication app(argc, argv); +void mitkImageStatisticsCalculatorTestSuite::TestCase10() +{ + /***************************** + * 2 whole pixel (white) + 2 whole pixel (black) in y-direction + * -> mean of 127.66 expected + ******************************/ + mitk::PlanarPolygon::Pointer figure2 = mitk::PlanarPolygon::New(); + figure2->SetGeometry2D( m_Geometry ); + mitk::Point2D pnt1; pnt1[0] = 11.5; pnt1[1] = 10.5; + figure2->PlaceFigure( pnt1 ); + + mitk::Point2D pnt2; pnt2[0] = 11.5; pnt2[1] = 13.5; + figure2->SetControlPoint( 1, pnt2, true ); + mitk::Point2D pnt3; pnt3[0] = 12.5; pnt3[1] = 13.5; + figure2->SetControlPoint( 2, pnt3, true ); + mitk::Point2D pnt4; pnt4[0] = 12.5; pnt4[1] = 10.5; + figure2->SetControlPoint( 3, pnt4, true ); + figure2->GetPolyLine(0); + + this->VerifyStatistics(ComputeStatistics(m_Image, figure2.GetPointer()), 127.66, 127.5); +} - mitk::Image::Pointer image = mitkImageStatisticsCalculatorTestClass::GetTestImage(); - MITK_TEST_CONDITION_REQUIRED( image.IsNotNull(), "Loading test image" ); +void mitkImageStatisticsCalculatorTestSuite::TestCase11() +{ + /***************************** + * 9 whole pixels (white) + 3 half pixels (white) + * + 3 whole pixel (black) [ + 3 slightly less than half pixels (black)] + * -> mean of 204.0 expected + ******************************/ + mitk::PlanarPolygon::Pointer figure2 = mitk::PlanarPolygon::New(); + figure2->SetGeometry2D( m_Geometry ); + mitk::Point2D pnt1; pnt1[0] = 0.5; pnt1[1] = 0.5; + figure2->PlaceFigure( pnt1 ); + + mitk::Point2D pnt2; pnt2[0] = 3.5; pnt2[1] = 3.5; + figure2->SetControlPoint( 1, pnt2, true ); + mitk::Point2D pnt3; pnt3[0] = 8.4999; pnt3[1] = 3.5; + figure2->SetControlPoint( 2, pnt3, true ); + mitk::Point2D pnt4; pnt4[0] = 5.4999; pnt4[1] = 0.5; + figure2->SetControlPoint( 3, pnt4, true ); + figure2->GetPolyLine(0); + + this->VerifyStatistics(ComputeStatistics(m_Image, figure2.GetPointer()), 204.0, 105.58 ); +} - mitk::Geometry2D::Pointer geom = image->GetSlicedGeometry()->GetGeometry2D(0); +void mitkImageStatisticsCalculatorTestSuite::TestCase12() +{ + /***************************** + * half pixel (white) + whole pixel (white) + half pixel (black) + * -> mean of 212.66 expected + ******************************/ + mitk::PlanarPolygon::Pointer figure2 = mitk::PlanarPolygon::New(); + figure2->SetGeometry2D( m_Geometry ); + mitk::Point2D pnt1; pnt1[0] = 9.5; pnt1[1] = 0.5; + figure2->PlaceFigure( pnt1 ); + + mitk::Point2D pnt2; pnt2[0] = 9.5; pnt2[1] = 2.5; + figure2->SetControlPoint( 1, pnt2, true ); + mitk::Point2D pnt3; pnt3[0] = 11.5; pnt3[1] = 2.5; + figure2->SetControlPoint( 2, pnt3, true ); + figure2->GetPolyLine(0); + + this->VerifyStatistics(ComputeStatistics(m_Image, figure2.GetPointer()), 212.66, 73.32); +} - std::vector allTestCases = - mitkImageStatisticsCalculatorTestClass::InitializeTestCases( geom ); +const mitk::ImageStatisticsCalculator::Statistics +mitkImageStatisticsCalculatorTestSuite::ComputeStatistics( mitk::Image::Pointer image, mitk::PlanarFigure::Pointer polygon ) +{ + mitk::ImageStatisticsCalculator::Pointer statisticsCalculator = mitk::ImageStatisticsCalculator::New(); + statisticsCalculator->SetImage( image ); + statisticsCalculator->SetMaskingModeToPlanarFigure(); + statisticsCalculator->SetPlanarFigure( polygon ); + statisticsCalculator->ComputeStatistics(); - for ( std::vector::size_type i=0; iGetStatistics(); +} - TestUnitilizedImage(); +void mitkImageStatisticsCalculatorTestSuite::VerifyStatistics(const mitk::ImageStatisticsCalculator::Statistics& stats, + double testMean, double testSD) +{ + int tmpMean = stats.Mean * 100; + double calculatedMean = tmpMean / 100.0; + MITK_TEST_CONDITION( calculatedMean == testMean, + "Calculated mean grayvalue '" << calculatedMean << + "' is equal to the desired value '" << testMean << "'" ); + + int tmpSD = stats.Sigma * 100; + double calculatedSD = tmpSD / 100.0; + MITK_TEST_CONDITION( calculatedSD == testSD, + "Calculated grayvalue sd '" << calculatedSD << + "' is equal to the desired value '" << testSD <<"'" ); +} - MITK_TEST_END() +void mitkImageStatisticsCalculatorTestSuite::TestUninitializedImage() +{ + /***************************** + * loading uninitialized image to datastorage + ******************************/ + MITK_TEST_FOR_EXCEPTION_BEGIN(mitk::Exception) + mitk::Image::Pointer image = mitk::Image::New(); + mitk::DataNode::Pointer node = mitk::DataNode::New(); + node->SetData(image); + + mitk::ImageStatisticsCalculator::Pointer is = mitk::ImageStatisticsCalculator::New(); + is->ComputeStatistics(); + MITK_TEST_FOR_EXCEPTION_END(mitk::Exception) } +MITK_TEST_SUITE_REGISTRATION(mitkImageStatisticsCalculator)