Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkDicomSeriesReaderTest.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 
19 #include <iostream>
20 
21 #include "mitkDicomSeriesReader.h"
22 #include "mitkImage.h"
23 #include "mitkProperties.h"
24 
25 static std::map<std::string, std::map<gdcm::Tag, std::string>> GetTagInformationFromFile(
27 {
28  gdcm::Scanner scanner;
29  std::map<std::string, std::map<gdcm::Tag, std::string>> tagInformations;
30 
31  const gdcm::Tag tagSliceLocation(0x0020, 0x1041); // slice location
32  scanner.AddTag(tagSliceLocation);
33 
34  const gdcm::Tag tagInstanceNumber(0x0020, 0x0013); // (image) instance number
35  scanner.AddTag(tagInstanceNumber);
36 
37  const gdcm::Tag tagSOPInstanceNumber(0x0008, 0x0018); // SOP instance number
38  scanner.AddTag(tagSOPInstanceNumber);
39 
40  // unsigned int slice(0);
41  scanner.Scan(files);
42 
43  // return const_cast<gdcm::Scanner::MappingType&>(scanner.GetMappings());
44  gdcm::Scanner::MappingType &tagValueMappings = const_cast<gdcm::Scanner::MappingType &>(scanner.GetMappings());
45 
46  for (std::vector<std::string>::const_iterator fIter = files.begin(); fIter != files.end(); ++fIter)
47  {
48  std::map<gdcm::Tag, std::string> tags;
49  tags.insert(
50  std::pair<gdcm::Tag, std::string>(tagSliceLocation, tagValueMappings[fIter->c_str()][tagSliceLocation]));
51  tags.insert(
52  std::pair<gdcm::Tag, std::string>(tagInstanceNumber, tagValueMappings[fIter->c_str()][tagInstanceNumber]));
53  tags.insert(
54  std::pair<gdcm::Tag, std::string>(tagSOPInstanceNumber, tagValueMappings[fIter->c_str()][tagSOPInstanceNumber]));
55 
56  tagInformations.insert(std::pair<std::string, std::map<gdcm::Tag, std::string>>(fIter->c_str(), tags));
57  }
58 
59  return tagInformations;
60 }
61 
62 int mitkDicomSeriesReaderTest(int argc, char *argv[])
63 {
64  // always start with this!
65  MITK_TEST_BEGIN("DicomSeriesReader")
66  if (argc < 1)
67  {
68  MITK_ERROR << "No directory given!";
69  return -1;
70  }
71 
72  char *dir;
73  dir = argv[1];
74 
75  // check if DICOMTags have been set as property for mitk::Image
77  std::list<mitk::Image::Pointer> images;
78  std::map<mitk::Image::Pointer, mitk::DicomSeriesReader::StringContainer> fileMap;
79 
80  // TODO sort series UIDs, implementation of map iterator might differ on different platforms (or verify this is a
81  // standard topic??)
82  for (mitk::DicomSeriesReader::FileNamesGrouping::const_iterator seriesIter = seriesInFiles.begin();
83  seriesIter != seriesInFiles.end();
84  ++seriesIter)
85  {
86  mitk::DicomSeriesReader::StringContainer files = seriesIter->second.GetFilenames();
87 
89  MITK_TEST_CONDITION_REQUIRED(node.IsNotNull(), "Testing node")
90 
91  if (node.IsNotNull())
92  {
93  mitk::Image::Pointer image = dynamic_cast<mitk::Image *>(node->GetData());
94 
95  images.push_back(image);
96  fileMap.insert(std::pair<mitk::Image::Pointer, mitk::DicomSeriesReader::StringContainer>(image, files));
97  }
98  }
99 
100  // Test if DICOM tags have been added correctly to the mitk::image properties
101 
102  const gdcm::Tag tagSliceLocation(0x0020, 0x1041); // slice location
103  const gdcm::Tag tagInstanceNumber(0x0020, 0x0013); // (image) instance number
104  const gdcm::Tag tagSOPInstanceNumber(0x0008, 0x0018); // SOP instance number
105 
106  for (std::list<mitk::Image::Pointer>::const_iterator imageIter = images.begin(); imageIter != images.end();
107  ++imageIter)
108  {
109  const mitk::Image::Pointer image = *imageIter;
110 
111  // Get tag information for all dicom files of this image
112  std::map<std::string, std::map<gdcm::Tag, std::string>> tagInformations =
113  GetTagInformationFromFile((*fileMap.find(image)).second);
114 
115  mitk::StringLookupTableProperty *sliceLocation =
116  dynamic_cast<mitk::StringLookupTableProperty *>(image->GetProperty("dicom.image.0020.1041").GetPointer());
117  mitk::StringLookupTableProperty *instanceNumber =
118  dynamic_cast<mitk::StringLookupTableProperty *>(image->GetProperty("dicom.image.0020.0013").GetPointer());
119  mitk::StringLookupTableProperty *SOPInstnaceNumber =
120  dynamic_cast<mitk::StringLookupTableProperty *>(image->GetProperty("dicom.image.0008.0018").GetPointer());
121 
123  dynamic_cast<mitk::StringLookupTableProperty *>(image->GetProperty("files").GetPointer());
124 
125  MITK_TEST_CONDITION(sliceLocation != nullptr, "Test if tag for slice location has been set to mitk image");
126  if (sliceLocation != nullptr)
127  {
128  for (int i = 0; i < (int)sliceLocation->GetValue().GetLookupTable().size(); i++)
129  {
130  if (i < (int)files->GetValue().GetLookupTable().size())
131  {
132  MITK_INFO << "Table value: " << sliceLocation->GetValue().GetTableValue(i) << " and File value: "
133  << tagInformations[files->GetValue().GetTableValue(i).c_str()][tagSliceLocation] << std::endl;
134  MITK_INFO << "Filename: " << files->GetValue().GetTableValue(i).c_str() << std::endl;
135  MITK_TEST_CONDITION(sliceLocation->GetValue().GetTableValue(i) ==
136  tagInformations[files->GetValue().GetTableValue(i).c_str()][tagSliceLocation],
137  "Test if value for slice location is correct");
138  }
139  }
140  }
141 
142  MITK_TEST_CONDITION(instanceNumber != nullptr, "Test if tag for image instance number has been set to mitk image");
143  if (instanceNumber != nullptr)
144  {
145  for (int i = 0; i < (int)instanceNumber->GetValue().GetLookupTable().size(); i++)
146  {
147  if (i < (int)files->GetValue().GetLookupTable().size())
148  {
149  MITK_INFO << "Table value: " << instanceNumber->GetValue().GetTableValue(i) << " and File value: "
150  << tagInformations[files->GetValue().GetTableValue(i).c_str()][tagInstanceNumber] << std::endl;
151  MITK_INFO << "Filename: " << files->GetValue().GetTableValue(i).c_str() << std::endl;
152  MITK_TEST_CONDITION(instanceNumber->GetValue().GetTableValue(i) ==
153  tagInformations[files->GetValue().GetTableValue(i).c_str()][tagInstanceNumber],
154  "Test if value for instance number is correct");
155  }
156  }
157  }
158 
159  MITK_TEST_CONDITION(SOPInstnaceNumber != nullptr, "Test if tag for SOP instance number has been set to mitk image");
160  if (SOPInstnaceNumber != nullptr)
161  {
162  for (int i = 0; i < (int)SOPInstnaceNumber->GetValue().GetLookupTable().size(); i++)
163  {
164  if (i < (int)files->GetValue().GetLookupTable().size())
165  {
166  MITK_INFO << "Table value: " << instanceNumber->GetValue().GetTableValue(i) << " and File value: "
167  << tagInformations[files->GetValue().GetTableValue(i).c_str()][tagSOPInstanceNumber] << std::endl;
168  MITK_INFO << "Filename: " << files->GetValue().GetTableValue(i).c_str() << std::endl;
169  MITK_TEST_CONDITION(SOPInstnaceNumber->GetValue().GetTableValue(i) ==
170  tagInformations[files->GetValue().GetTableValue(i).c_str()][tagSOPInstanceNumber],
171  "Test if value for SOP instance number is correct");
172  }
173  }
174  }
175  }
176 
177  MITK_TEST_END()
178 }
#define MITK_INFO
Definition: mitkLogMacros.h:22
#define MITK_ERROR
Definition: mitkLogMacros.h:24
#define MITK_TEST_CONDITION_REQUIRED(COND, MSG)
std::vector< std::string > StringContainer
Lists of filenames.
static DataNode::Pointer LoadDicomSeries(const StringContainer &filenames, bool sort=true, bool load4D=true, bool correctGantryTilt=true, UpdateCallBackMethod callback=nullptr, itk::SmartPointer< Image > preLoadedImageBlock=nullptr)
int mitkDicomSeriesReaderTest(int argc, char *argv[])
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
ValueType GetTableValue(IdentifierType id) const
static std::map< std::string, std::map< gdcm::Tag, std::string > > GetTagInformationFromFile(mitk::DicomSeriesReader::StringContainer files)
#define MITK_TEST_CONDITION(COND, MSG)
static FileNamesGrouping GetSeries(const std::string &dir, bool groupImagesWithGantryTilt, const StringContainer &restrictions=StringContainer())
see other GetSeries().
Image class for storing images.
Definition: mitkImage.h:76
std::map< std::string, ImageBlockDescriptor > FileNamesGrouping
and MITK_TEST_END()
section MAP_FRAME_Mapper_Settings Mapper settings For the mapping of corrected images
const LookupTableType & GetLookupTable() const
virtual T GetValue() const