Medical Imaging Interaction Toolkit  2018.4.99-a3d2e8fb
Medical Imaging Interaction Toolkit
mitkUSImageLoggingFilter.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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
14 #include <mitkIOUtil.h>
15 #include <mitkUIDGenerator.h>
16 #include <Poco/Path.h>
17 
18 #include <algorithm>
19 #include <mitkIOMimeTypes.h>
20 #include <mitkCoreServices.h>
21 #include <mitkIMimeTypeProvider.h>
22 
23 
25  m_ImageExtension(".nrrd")
26 {
27 }
28 
30 {
31 }
32 
34 {
35  mitk::Image::ConstPointer inputImage = this->GetInput();
36  mitk::Image::Pointer outputImage = this->GetOutput();
37 
38  if(inputImage.IsNull() || inputImage->IsEmpty())
39  {
40  MITK_WARN << "Input image is not valid. Cannot save image!";
41  return;
42  }
43 
44  //a clone is needed for a output and to store it.
45  mitk::Image::Pointer inputClone = inputImage->Clone();
46 
47 
48  //simply redirecy the input to the output
49  //this->SetNumberOfRequiredOutputs(1);
50  //this->SetNthOutput(0, inputClone->Clone());
51  //outputImage->Graft(inputImage);
52  //this->SetOutput(this->GetInput());
53  /*if (!this->GetOutput()->IsInitialized())
54  {
55  this->SetNumberOfRequiredOutputs(1);
56  mitk::Image::Pointer newOutput = mitk::Image::New();
57  this->SetNthOutput(0, newOutput);
58  }
59  memcpy(this->GetOutput(),this->GetInput());*/
60 
61  //this->SetNthOutput(0,inputImage.);
62  //this->AllocateOutputs();
63  //this->GraftOutput(inputClone);
64 
65  /*
66  if (!this->GetOutput()->IsInitialized())
67  {
68  mitk::Image::Pointer newOutput = mitk::Image::New();
69  this->SetNthOutput(0, newOutput);
70  }
71  this->GetOutput()Graft(this->GetInput());
72  */
73 
74 
75  m_LoggedImages.push_back(inputClone);
76  m_LoggedMITKSystemTimes.push_back(m_SystemTimeClock->GetCurrentStamp());
77 
78 }
79 
81 {
82  m_LoggedMessages.insert(std::make_pair(static_cast<int>(m_LoggedImages.size()-1),message));
83 }
84 
86 {
87 std::vector<std::string> dummy1;
88 std::string dummy2;
89 this->SaveImages(path,dummy1,dummy2);
90 }
91 
92 void mitk::USImageLoggingFilter::SaveImages(std::string path, std::vector<std::string>& filenames, std::string& csvFileName)
93 {
94  filenames = std::vector<std::string>();
95 
96  //test if path is valid
97  Poco::Path testPath(path);
98  if(!testPath.isDirectory())
99  {
100  mitkThrow() << "Attemting to write to directory " << path << " which is not valid! Aborting!";
101  }
102 
103  //generate a unique ID which is used as part of the filenames, so we avoid to overwrite old files by mistake.
105  std::string uniqueID = myGen.GetUID();
106 
107  //first: write the images
108  for(size_t i=0; i<m_LoggedImages.size(); i++)
109  {
110  std::stringstream name;
111  name << path << uniqueID << "_Image_" << i << m_ImageExtension;
112  mitk::IOUtil::Save(m_LoggedImages.at(i),name.str());
113  filenames.push_back(name.str());
114  }
115 
116  //then: write a csv file which contains comments to all the images
117 
118  //open file
119  std::stringstream csvFilenameStream;
120  csvFilenameStream << path << uniqueID << "_ImageMessages.csv";
121  csvFileName = csvFilenameStream.str();
122  std::filebuf fb;
123  fb.open (csvFileName.c_str(),std::ios::out);
124  std::ostream os(&fb);
125  os.precision(15); //set high precision to avoid loss of digits
126 
127  //write header
128  os << "image filename; MITK system timestamp; message\n";
129 
130  //write data
131  for(size_t i=0; i<m_LoggedImages.size(); i++)
132  {
133  std::map<int, std::string>::iterator it = m_LoggedMessages.find(i);
134  if (m_LoggedMessages.empty() || (it == m_LoggedMessages.end())) os << filenames.at(i) << ";" << m_LoggedMITKSystemTimes.at(i) << ";" << "" << "\n";
135  else os << filenames.at(i) << ";" << m_LoggedMITKSystemTimes.at(i) << ";" << it->second << "\n";
136  }
137 
138  //close file
139  fb.close();
140 }
141 
143  {
144  if(extension.compare(0,1,".") == 0)
145  extension = extension.substr(1,extension.size()-1);
146 
148 
149  std::vector<MimeType> mimeTypes = mimeTypeProvider->GetMimeTypesForCategory(IOMimeTypes::CATEGORY_IMAGES());
150 
151  for(std::vector<MimeType>::size_type i = 0 ; i< mimeTypes.size() ; ++i)
152  {
153  std::vector<std::string> extensions = mimeTypes[i].GetExtensions();
154  if (std::find(extensions.begin(), extensions.end(), extension) != extensions.end())
155  {
156  m_ImageExtension = "."+extension;
157  return true;
158  }
159  }
160  return false;
161  }
void SaveImages(std::string path, std::vector< std::string > &imageFilenames, std::string &csvFileName)
Generated unique IDs.
static IMimeTypeProvider * GetMimeTypeProvider(us::ModuleContext *context=us::GetModuleContext())
Get an IMimeTypeProvider instance.
std::string m_ImageExtension
stores the image extension, default is ".nrrd"
ImageCollection m_LoggedImages
An image collection for every input. The string identifies the input.
std::map< int, std::string > m_LoggedMessages
(Optional) messages for every logged image
static std::string CATEGORY_IMAGES()
RealTimeClock is a superclass to WindowsRealTimeClock, LinuxRealTimeClock, etc.
#define MITK_WARN
Definition: mitkLogMacros.h:19
bool SetImageFilesExtension(std::string extension)
#define mitkThrow()
std::vector< double > m_LoggedMITKSystemTimes
Logged system times for every logged image.
InputImageType * GetInput(void)
static void Save(const mitk::BaseData *data, const std::string &path, bool setPathProperty=false)
Save a mitk::BaseData instance.
Definition: mitkIOUtil.cpp:774
OutputType * GetOutput()
Get the output data of this image source object.
A RAII helper class for core service objects.
mitk::RealTimeClock::Pointer m_SystemTimeClock
system time clock for system time tag
void AddMessageToCurrentImage(std::string message)