Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkLevelWindowManagerCppUnitTest.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "mitkTestingMacros.h"
18 #include <mitkTestFixture.h>
19 
20 #include "mitkLevelWindowManager.h"
22 #include <itkComposeImageFilter.h>
23 #include <itkEventObject.h>
24 #include <itkImageDuplicator.h>
25 #include <itkImageIterator.h>
26 #include <mitkImageCast.h>
27 
28 class mitkLevelWindowManagerCppUnitTestSuite : public mitk::TestFixture
29 {
30  CPPUNIT_TEST_SUITE(mitkLevelWindowManagerCppUnitTestSuite);
31  MITK_TEST(TestMultiComponentRescaling);
32  CPPUNIT_TEST_SUITE_END();
33 
34 private:
35  mitk::Image::Pointer m_mitkMultiComponentImage;
36  mitk::Image::Pointer m_mitkImageComponent1;
37  mitk::Image::Pointer m_mitkImageComponent2;
38 
39 public:
40  void setUp()
41  {
42  typedef itk::Image<double, 3> ImageType;
43  typedef itk::VectorImage<double, 3> VectorImageType;
44  typedef itk::ImageRegionIterator<ImageType> ImageIteratorType;
45  typedef itk::ImageDuplicator<ImageType> DuplicatorType;
46  typedef itk::ComposeImageFilter<ImageType> CompositeFilterType;
47 
48  // generate two images with one component
50  ImageType::IndexType start;
51  start.Fill(0);
52  ImageType::SizeType size;
53  size.Fill(5);
54  ImageType::RegionType region;
55  region.SetSize(size);
56  region.SetIndex(start);
57  imageComponent1->SetRegions(region);
58  imageComponent1->Allocate();
59 
61  duplicator->SetInputImage(imageComponent1);
62  duplicator->Update();
63  ImageType::Pointer imageComponent2 = duplicator->GetOutput();
64 
65  // give them differing data
66  ImageIteratorType iterator1(imageComponent1, imageComponent1->GetLargestPossibleRegion());
67  iterator1.GoToBegin();
68  int i = 0;
69  while (!iterator1.IsAtEnd())
70  {
71  iterator1.Set((double)i);
72  ++iterator1;
73  ++i;
74  }
75 
76  ImageIteratorType iterator2(imageComponent2, imageComponent2->GetLargestPossibleRegion());
77  iterator2.GoToBegin();
78  i = 2000;
79  while (!iterator2.IsAtEnd())
80  {
81  iterator2.Set((double)i);
82  ++iterator2;
83  ++i;
84  }
85 
86  // copy into single VectorImage
88  compositeFilter->SetInput(0, imageComponent1);
89  compositeFilter->SetInput(1, imageComponent2);
90  compositeFilter->Update();
91  itk::VectorImage<double, 3>::Pointer multiComponentImage = compositeFilter->GetOutput();
92 
93  // cast images to mitk
94  mitk::CastToMitkImage(multiComponentImage, m_mitkMultiComponentImage);
95  mitk::CastToMitkImage(imageComponent1, m_mitkImageComponent1);
96  mitk::CastToMitkImage(imageComponent2, m_mitkImageComponent2);
97  }
98 
99  static mitk::LevelWindow getLevelWindowForImage(mitk::Image *image, unsigned component)
100  {
102  manager = mitk::LevelWindowManager::New();
104 
105  try
106  {
107  manager->SetDataStorage(ds);
108  }
109  catch (std::exception &e)
110  {
111  MITK_ERROR << "Exception: " << e.what();
112  }
113 
115  node->SetData(image);
116  ds->Add(node);
117 
118  node->SetBoolProperty("selected", true);
119  node->SetIntProperty("Image.Displayed Component", component);
120 
121  mitk::LevelWindow levelWindow;
122  node->GetLevelWindow(levelWindow);
123  return levelWindow; // node is an image node because of predicates
124  }
125 
126  void TestMultiComponentRescaling()
127  {
128  // compute level windows for the three images
129  mitk::LevelWindow imageComponent1LevelWindow = getLevelWindowForImage(m_mitkImageComponent1, 0);
130  mitk::LevelWindow imageComponent2LevelWindow = getLevelWindowForImage(m_mitkImageComponent2, 0);
131  // calculate level window for second component in multi-component image
132  mitk::LevelWindow multiComponentImageLevelWindow = getLevelWindowForImage(m_mitkMultiComponentImage, 1);
133 
134  // compare level window boundaries. the multicomponent image level window
135  // should match the second image, since the second component was selected
136  CPPUNIT_ASSERT_EQUAL_MESSAGE("default lower bounds equal",
137  imageComponent2LevelWindow.GetDefaultLowerBound(),
138  multiComponentImageLevelWindow.GetDefaultLowerBound());
139 
140  CPPUNIT_ASSERT_EQUAL_MESSAGE("default upper bounds equal",
141  imageComponent2LevelWindow.GetDefaultUpperBound(),
142  multiComponentImageLevelWindow.GetDefaultUpperBound());
143 
144  CPPUNIT_ASSERT_EQUAL_MESSAGE(
145  "range equal", imageComponent2LevelWindow.GetRange(), multiComponentImageLevelWindow.GetRange());
146 
147  CPPUNIT_ASSERT(imageComponent1LevelWindow.GetDefaultLowerBound() !=
148  multiComponentImageLevelWindow.GetDefaultLowerBound());
149 
150  CPPUNIT_ASSERT(imageComponent1LevelWindow.GetDefaultUpperBound() !=
151  multiComponentImageLevelWindow.GetDefaultUpperBound());
152  }
153 };
154 
155 MITK_TEST_SUITE_REGISTRATION(mitkLevelWindowManagerCppUnit)
itk::SmartPointer< Self > Pointer
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
#define MITK_ERROR
Definition: mitkLogMacros.h:24
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
ScalarType GetDefaultLowerBound() const
ScalarType GetDefaultUpperBound() const
The LevelWindow class Class to store level/window values.
itk::VectorImage< float, 3 > VectorImageType
map::core::discrete::Elements< 3 >::InternalImageType ImageType
static Pointer New()
Image class for storing images.
Definition: mitkImage.h:76
Test fixture for parameterized tests.
void CastToMitkImage(const itk::SmartPointer< ItkOutputImageType > &itkimage, itk::SmartPointer< mitk::Image > &mitkoutputimage)
Cast an itk::Image (with a specific type) to an mitk::Image.
Definition: mitkImageCast.h:78
static Pointer New()
ScalarType GetRange() const
returns the size of the grey value range
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.