Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkdump.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 
42 
43 using mitk::DICOMTag;
44 
45 std::string buildDateString()
46 {
47  std::time_t rawtime;
48  std::tm *timeinfo;
49  char buffer[80];
50 
51  std::time(&rawtime);
52  timeinfo = std::localtime(&rawtime);
53 
54  std::strftime(buffer, 80, "%Y%m%d-%H%M%S", timeinfo);
55 
56  return std::string(buffer);
57 }
58 
59 void gen_random(char *s, const int len)
60 {
61  static const char alphanum[] = "0123456789"
62  "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
63 
64  for (int i = 0; i < len; ++i)
65  {
66  s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
67  }
68 
69  s[len] = 0;
70 }
71 
72 std::string gen_random(const int len)
73 {
74  if (len > 0 && len < 100)
75  {
76  char retval[100];
77  gen_random(retval, len);
78  return std::string(retval);
79  }
80  else
81  {
82  return std::string("");
83  }
84 }
85 
86 std::string removeUnsafeChars(const std::string &str)
87 {
88  std::string retval;
89  for (std::string::const_iterator it = str.begin(); it != str.end(); ++it)
90  {
91  const char &c = *it;
92  if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c == '.') || (c == '-') ||
93  (c == '_'))
94  {
95  retval += c;
96  }
97  }
98  return retval;
99 }
100 
101 std::string extractDirString(const std::string &dirString)
102 {
103  std::string wholeprefix = dirString.substr(0, dirString.find_last_of("/\\"));
104  std::string lastDirectoryPart = wholeprefix.substr(wholeprefix.find_last_of("/\\") + 1);
105  std::string cleanLastDirectoryPart = removeUnsafeChars(lastDirectoryPart);
106  if (!cleanLastDirectoryPart.empty())
107  {
108  return cleanLastDirectoryPart;
109  }
110  else
111  {
112  std::stringstream emptydirname;
113  emptydirname << "noname_" << gen_random(6);
114 
115  return emptydirname.str();
116  }
117 }
118 
119 int main(int argc, char *argv[])
120 {
121  bool fileDetails(false);
122  bool loadimage(false);
123  int firstFileIndex = 1;
124 
125  // see if we got the '-v' flag to output file details
126  if (argc > 1 && std::string(argv[firstFileIndex]) == "-v")
127  {
128  fileDetails = true;
129  ++firstFileIndex;
130  }
131 
132  // see if we got the '-l' flag
133  if (argc > 1 && std::string(argv[firstFileIndex]) == "-l")
134  {
135  loadimage = true;
136  ++firstFileIndex;
137  }
138 
139  // analyze files from argv
140  mitk::StringList inputFiles;
141  for (int a = firstFileIndex; a < argc; ++a)
142  {
143  inputFiles.push_back(std::string(argv[a]));
144  }
145 
146  if (inputFiles.empty())
147  {
148  MITK_INFO << "0 input files given, exiting...";
149  return EXIT_SUCCESS;
150  }
151 
153  configSelector->LoadBuiltIn3DConfigs(); // a set of compiled in ressources with standard configurations that work well
154  configSelector->SetInputFiles(inputFiles);
155  mitk::DICOMFileReader::Pointer reader = configSelector->GetFirstReaderWithMinimumNumberOfOutputImages();
156  if (reader.IsNull())
157  {
158  MITK_ERROR << "Could not configure any DICOM reader.. Exiting...";
159  return EXIT_FAILURE;
160  }
161 
162  // output best reader result
163  MITK_INFO << "---- Best reader configuration '" << reader->GetConfigurationLabel() << "' with "
164  << reader->GetNumberOfOutputs() << " outputs";
165  if (fileDetails)
166  {
167  reader->PrintOutputs(std::cout, fileDetails);
168  }
169 
170  // construct the name of a log file
171  std::string datestring = buildDateString();
172  ;
173  std::string dirString = extractDirString(argv[firstFileIndex]);
174  std::string logfilename = datestring + "_dir_" + dirString + ".mitkdump";
175  MITK_INFO << "Logfile " << logfilename;
176 
177  // write output to file for later analysis
178  std::ofstream fs;
179  fs.open(logfilename.c_str());
180  fs << "---- " << dirString << ": Best reader configuration '" << reader->GetConfigurationLabel() << "' with "
181  << reader->GetNumberOfOutputs() << " outputs" << std::endl;
182  reader->PrintOutputs(fs, true); // always verbose in log file
183  fs.close();
184 
185  // serialize, deserialize, analyze again, verify result!
187  std::string readerSerialization = serializer->CreateConfigStringFromReader(reader.GetPointer());
188 
189  bool outputError(false);
190  for (unsigned int outputIndex = 0; outputIndex < reader->GetNumberOfOutputs(); ++outputIndex)
191  {
192  const mitk::DICOMImageBlockDescriptor &outputDescriptor = reader->GetOutput(outputIndex);
193 
194  mitk::StringList filenamesOfThisGroup;
195  const mitk::DICOMImageFrameList &frames = outputDescriptor.GetImageFrameList();
196  for (auto fIter = frames.begin(); fIter != frames.end(); ++fIter)
197  {
198  filenamesOfThisGroup.push_back((*fIter)->Filename);
199  }
200 
202  mitk::DICOMFileReader::Pointer dicomReader = readerConfigurator->CreateFromUTF8ConfigString(readerSerialization);
203  dicomReader->SetInputFiles(filenamesOfThisGroup);
204  dicomReader->AnalyzeInputFiles();
205  if (dicomReader->GetNumberOfOutputs() != 1)
206  {
207  MITK_ERROR << "****** Re-analyzing files of output group " << outputIndex << " yields "
208  << dicomReader->GetNumberOfOutputs() << " groups";
209  outputError = true;
210 
211  for (auto fIter = frames.begin(); fIter != frames.end(); ++fIter)
212  {
213  MITK_INFO << "filename group " << outputIndex << ": " << (*fIter)->Filename;
214  }
215  }
216  else
217  {
218  MITK_INFO << "Re-analyzing files of output group " << outputIndex << " yields "
219  << dicomReader->GetNumberOfOutputs() << " groups";
220  }
221  }
222 
223  if (outputError)
224  {
225  std::stringstream es;
226  es << "Original reader configuration: " << std::endl;
227  reader->PrintConfiguration(es);
228  es << std::endl;
230  mitk::DICOMFileReader::Pointer dicomReader = readerConfigurator->CreateFromUTF8ConfigString(readerSerialization);
231  es << "New reader configuration: " << std::endl;
232  dicomReader->PrintConfiguration(es);
233  es << std::endl;
234 
235  es << "Original XML: \n" << readerSerialization << std::endl;
236  std::string newSerialization = serializer->CreateConfigStringFromReader(dicomReader.GetPointer());
237  es << "New XML: \n" << newSerialization << std::endl;
238  MITK_ERROR << es.str();
239  }
240 
241  if (loadimage)
242  {
243  MITK_INFO << "Loading...";
244  reader->LoadImages();
245  mitk::Image::Pointer image = reader->GetOutput(0).GetMitkImage();
246  MITK_INFO << "---- Output image:";
247  mitk::BaseGeometry::Pointer geo3D = image->GetGeometry();
248  if (geo3D.IsNotNull())
249  {
250  mitk::SlicedGeometry3D::Pointer sg = dynamic_cast<mitk::SlicedGeometry3D *>(geo3D.GetPointer());
251  if (sg.IsNotNull())
252  {
253  unsigned int nos = sg->GetSlices();
254  mitk::PlaneGeometry::ConstPointer first = sg->GetPlaneGeometry(0);
255  mitk::PlaneGeometry::ConstPointer last = sg->GetPlaneGeometry(nos - 1);
256 
257  mitk::Point3D firstOrigin = first->GetOrigin();
258  mitk::Point3D lastOrigin = last->GetOrigin();
259  MITK_INFO << "Geometry says: First slice at " << firstOrigin << ", last slice at " << lastOrigin;
260 
262  dynamic_cast<mitk::StringLookupTableProperty *>(image->GetProperty("dicom.image.0020.1041").GetPointer());
263  if (sliceLocations.IsNotNull())
264  {
265  std::string firstSliceLocation = sliceLocations->GetValue().GetTableValue(0);
266  std::string lastSliceLocation = sliceLocations->GetValue().GetTableValue(nos - 1);
267  MITK_INFO << "Image properties says: first slice location at " << firstSliceLocation
268  << ", last slice location at " << lastSliceLocation;
269  }
270 
272  dynamic_cast<mitk::StringLookupTableProperty *>(image->GetProperty("dicom.image.0020.0013").GetPointer());
273  if (instanceNumbers.IsNotNull())
274  {
275  std::string firstInstanceNumber = instanceNumbers->GetValue().GetTableValue(0);
276  std::string lastInstanceNumber = instanceNumbers->GetValue().GetTableValue(nos - 1);
277  MITK_INFO << "Image properties says: first instance number at " << firstInstanceNumber
278  << ", last instance number at " << lastInstanceNumber;
279  }
280  }
281  }
282  MITK_INFO << "---- End of output";
283  }
284 
285  // if we got so far, everything is fine
286  return EXIT_SUCCESS;
287 }
section MAP_FRAME_Mapper_Settings Mapper settings For the mapping of corrected you have several settings but high interpolation errors for gray value images Right choice for label images or masks li Details of the frame selection In this tab you can specify the frames of the currently selected image that should be corrected As default all frames of an image will be corrected If you only select specific frames
const DICOMImageFrameList & GetImageFrameList() const
List of frames that constitute the mitk::Image (DICOMImageFrames)
itk::SmartPointer< Self > Pointer
std::string removeUnsafeChars(const std::string &str)
Definition: mitkdump.cpp:86
#define MITK_INFO
Definition: mitkLogMacros.h:22
#define MITK_ERROR
Definition: mitkLogMacros.h:24
Representation of a DICOM tag.
Definition: mitkDICOMTag.h:37
int main(int argc, char *argv[])
Definition: mitkdump.cpp:119
std::string extractDirString(const std::string &dirString)
Definition: mitkdump.cpp:101
void gen_random(char *s, const int len)
Definition: mitkdump.cpp:59
std::vector< DICOMImageFrameInfo::Pointer > DICOMImageFrameList
Output descriptor for DICOMFileReader.
std::vector< std::string > StringList
Describes the geometry of a data object consisting of slices.
std::string buildDateString()
Definition: mitkdump.cpp:45
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.