Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkNrrdTbssRoiImageReader.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 #ifndef __mitkNrrdTbssRoiReader_cpp
18 #define __mitkNrrdTbssRoiReader_cpp
19 
21 
22 #include "itkImageFileReader.h"
23 #include "itkMetaDataObject.h"
24 #include "itkNrrdImageIO.h"
25 #include "itkNiftiImageIO.h"
26 
27 #include <itkImageFileWriter.h>
28 
29 #include <iostream>
30 #include <fstream>
31 
32 #include "itksys/SystemTools.hxx"
33 
34 namespace mitk
35 {
36 
37 
40  {
41 
42  try
43  {
44 
45  // Change locale if needed
46  const std::string& locale = "C";
47  const std::string& currLocale = setlocale( LC_ALL, nullptr );
48 
49  if ( locale.compare(currLocale)!=0 )
50  {
51  try
52  {
53  MITK_INFO << " ** Changing locale from " << setlocale(LC_ALL, nullptr) << " to '" << locale << "'";
54  setlocale(LC_ALL, locale.c_str());
55  }
56  catch(...)
57  {
58  MITK_INFO << "Could not set locale " << locale;
59  }
60  }
61 
62 
63 
64  // READ IMAGE INFORMATION
65  const unsigned int MINDIM = 3;
66  const unsigned int MAXDIM = 4;
67 
68  MITK_INFO << "loading " << m_FileName << " via mitk::NrrdTbssImageReader... " << std::endl;
69 
70  // Check to see if we can read the file given the name or prefix
71  if ( m_FileName == "" )
72  {
73  itkWarningMacro( << "Filename is empty!" )
74  return;
75  }
76 
78  imageIO->SetFileName( m_FileName.c_str() );
79  imageIO->ReadImageInformation();
80 
81  unsigned int ndim = imageIO->GetNumberOfDimensions();
82 
83  if ( ndim < MINDIM || ndim > MAXDIM )
84  {
85  itkWarningMacro( << "Sorry, only dimensions 3 is supported. The given file has " << ndim << " dimensions!" )
86  return;
87  }
88 
89 
90  itk::ImageIORegion ioRegion( ndim );
91  itk::ImageIORegion::SizeType ioSize = ioRegion.GetSize();
92  itk::ImageIORegion::IndexType ioStart = ioRegion.GetIndex();
93 
94  unsigned int dimensions[ MAXDIM ];
95  dimensions[ 0 ] = 0;
96  dimensions[ 1 ] = 0;
97  dimensions[ 2 ] = 0;
98  dimensions[ 3 ] = 0;
99 
100  ScalarType spacing[ MAXDIM ];
101  spacing[ 0 ] = 1.0f;
102  spacing[ 1 ] = 1.0f;
103  spacing[ 2 ] = 1.0f;
104  spacing[ 3 ] = 1.0f;
105 
106 
107  Point3D origin;
108  origin.Fill(0);
109 
110  unsigned int i;
111  for ( i = 0; i < ndim ; ++i )
112  {
113  ioStart[ i ] = 0;
114  ioSize[ i ] = imageIO->GetDimensions( i );
115  if(i<MAXDIM)
116  {
117  dimensions[ i ] = imageIO->GetDimensions( i );
118  spacing[ i ] = imageIO->GetSpacing( i );
119  if(spacing[ i ] <= 0)
120  spacing[ i ] = 1.0f;
121  }
122  if(i<3)
123  {
124  origin[ i ] = imageIO->GetOrigin( i );
125  }
126  }
127 
128  ioRegion.SetSize( ioSize );
129  ioRegion.SetIndex( ioStart );
130 
131  MITK_INFO << "ioRegion: " << ioRegion << std::endl;
132  imageIO->SetIORegion( ioRegion );
133  void* buffer = new unsigned char[imageIO->GetImageSizeInBytes()];
134  imageIO->Read( buffer );
135  //mitk::Image::Pointer static_cast<OutputType*>(this->GetOutput())image = mitk::Image::New();
136  if((ndim==4) && (dimensions[3]<=1))
137  ndim = 3;
138  if((ndim==3) && (dimensions[2]<=1))
139  ndim = 2;
140 
141  static_cast<OutputType*>(this->GetPrimaryOutput())->Initialize( MakePixelType(imageIO), ndim, dimensions );
142  static_cast<OutputType*>(this->GetPrimaryOutput())->SetImportChannel( buffer, 0, Image::ManageMemory );
143 
144  // access direction of itk::Image and include spacing
145  mitk::Matrix3D matrix;
146  matrix.SetIdentity();
147  unsigned int j, itkDimMax3 = (ndim >= 3? 3 : ndim);
148  for ( i=0; i < itkDimMax3; ++i)
149  for( j=0; j < itkDimMax3; ++j )
150  matrix[i][j] = imageIO->GetDirection(j)[i];
151 
152  // re-initialize PlaneGeometry with origin and direction
153  PlaneGeometry* planeGeometry = static_cast<PlaneGeometry*>
154  (static_cast<OutputType*>
155  (this->GetPrimaryOutput())->GetSlicedGeometry(0)->GetPlaneGeometry(0));
156  planeGeometry->SetOrigin(origin);
157  planeGeometry->GetIndexToWorldTransform()->SetMatrix(matrix);
158 
159  // re-initialize SlicedGeometry3D
160  SlicedGeometry3D* slicedGeometry = static_cast<OutputType*>(this->GetPrimaryOutput())->GetSlicedGeometry(0);
161  slicedGeometry->InitializeEvenlySpaced(planeGeometry, static_cast<OutputType*>(this->GetPrimaryOutput())->GetDimension(2));
162  slicedGeometry->SetSpacing(spacing);
163 
164  // re-initialize TimeGeometry
165  dynamic_cast<ProportionalTimeGeometry *>(static_cast<OutputType*>(this->GetPrimaryOutput())->GetTimeGeometry())->Initialize(slicedGeometry, static_cast<OutputType*>(this->GetOutput(0))->GetDimension(3));
166 
167  buffer = nullptr;
168  MITK_INFO << "number of image components: "<< static_cast<OutputType*>(this->GetPrimaryOutput())->GetPixelType().GetNumberOfComponents() << std::endl;
169 
170 
171 
172  // READ TBSS HEADER INFORMATION
173  ImageType::Pointer img;
174 
175  std::string ext = itksys::SystemTools::GetFilenameLastExtension(m_FileName);
176  ext = itksys::SystemTools::LowerCase(ext);
177  if (ext == ".roi")
178  {
179  typedef itk::ImageFileReader<ImageType> FileReaderType;
181  reader->SetFileName(this->m_FileName);
182 
183  reader->SetImageIO(imageIO);
184  reader->Update();
185 
186  img = reader->GetOutput();
187 
188  static_cast<OutputType*>(this->GetPrimaryOutput())->SetImage(img);
189 
190  itk::MetaDataDictionary imgMetaDictionary = img->GetMetaDataDictionary();
191  ReadRoiInfo(imgMetaDictionary);
192 
193 
194 
195  }
196 
197 
198  // RESET LOCALE
199  try
200  {
201  MITK_INFO << " ** Changing locale back from " << setlocale(LC_ALL, nullptr) << " to '" << currLocale << "'";
202  setlocale(LC_ALL, currLocale.c_str());
203  }
204  catch(...)
205  {
206  MITK_INFO << "Could not reset locale " << currLocale;
207  }
208 
209  MITK_INFO << "...finished!" << std::endl;
210 
211  }
212  catch(std::exception& e)
213  {
214  MITK_INFO << "Std::Exception while reading file!!";
215  MITK_INFO << e.what();
216  throw itk::ImageFileReaderException(__FILE__, __LINE__, e.what());
217  }
218  catch(...)
219  {
220  MITK_INFO << "Exception while reading file!!";
221  throw itk::ImageFileReaderException(__FILE__, __LINE__, "Sorry, an error occurred while reading the requested vessel tree file!");
222  }
223 
224 
225  }
226 
227 
228 
230  ::ReadRoiInfo(itk::MetaDataDictionary dict)
231  {
232  std::vector<std::string> imgMetaKeys = dict.GetKeys();
233  std::vector<std::string>::const_iterator itKey = imgMetaKeys.begin();
234  std::string metaString;
235  std::vector< itk::Index<3> > roi;
236 
237  for (; itKey != imgMetaKeys.end(); itKey ++)
238  {
239  double x,y,z;
240  itk::Index<3> ix;
241  itk::ExposeMetaData<std::string> (dict, *itKey, metaString);
242 
243  if (itKey->find("ROI_index") != std::string::npos)
244  {
245  MITK_INFO << *itKey << " ---> " << metaString;
246  sscanf(metaString.c_str(), "%lf %lf %lf\n", &x, &y, &z);
247  ix[0] = x; ix[1] = y; ix[2] = z;
248  roi.push_back(ix);
249  }
250  else if(itKey->find("preprocessed FA") != std::string::npos)
251  {
252  MITK_INFO << *itKey << " ---> " << metaString;
253  static_cast<OutputType*>(this->GetPrimaryOutput())->SetPreprocessedFA(true);
254  static_cast<OutputType*>(this->GetPrimaryOutput())->SetPreprocessedFAFile(metaString);
255  }
256 
257  // Name of structure
258  if (itKey->find("structure") != std::string::npos)
259  {
260  MITK_INFO << *itKey << " ---> " << metaString;
261  static_cast<OutputType*>(this->GetPrimaryOutput())->SetStructure(metaString);
262  }
263  }
264  static_cast<OutputType*>(this->GetPrimaryOutput())->SetRoi(roi);
265 
266  }
267 
268 
269  const char* NrrdTbssRoiImageReader
271  {
272  return m_FileName.c_str();
273  }
274 
275 
277  ::SetFileName(const char* aFileName)
278  {
279  m_FileName = aFileName;
280  }
281 
282 
283  const char* NrrdTbssRoiImageReader
285  {
286  return m_FilePrefix.c_str();
287  }
288 
289 
291  ::SetFilePrefix(const char* aFilePrefix)
292  {
293  m_FilePrefix = aFilePrefix;
294  }
295 
296 
297  const char* NrrdTbssRoiImageReader
299  {
300  return m_FilePattern.c_str();
301  }
302 
303 
305  ::SetFilePattern(const char* aFilePattern)
306  {
307  m_FilePattern = aFilePattern;
308  }
309 
310 
312  ::CanReadFile(const std::string filename, const std::string filePrefix, const std::string filePattern)
313  {
314 
315  // First check the extension
316  if( filename == "" )
317  return false;
318 
319  // check if image is serie
320  if( filePattern != "" && filePrefix != "" )
321  return false;
322 
323 
324  std::string ext = itksys::SystemTools::GetFilenameLastExtension(filename);
325  ext = itksys::SystemTools::LowerCase(ext);
326 
327  if (ext == ".roi")
328  {
330 
331  typedef itk::ImageFileReader<ImageType> FileReaderType;
333  reader->SetImageIO(io);
334  reader->SetFileName(filename);
335 
336  try
337  {
338  reader->Update();
339  }
340  catch(itk::ExceptionObject e)
341  {
342  MITK_INFO << e.GetDescription();
343  return false;
344  }
345 
346  return true;
347 
348 
349  }
350 
351  return false;
352  }
353 
354 } //namespace MITK
355 
356 #endif
itk::SmartPointer< Self > Pointer
void SetSpacing(const mitk::Vector3D &aSpacing, bool enforceSetSpacing=false)
Set the spacing (m_Spacing).
#define MITK_INFO
Definition: mitkLogMacros.h:22
Base of all data objects.
Definition: mitkBaseData.h:39
double ScalarType
void SetFileName(const char *aFileName) override
Specify the file to load.
DataCollection - Class to facilitate loading/accessing structured data.
void ReadRoiInfo(itk::MetaDataDictionary dict)
virtual void InitializeEvenlySpaced(mitk::PlaneGeometry *geometry2D, unsigned int slices)
Completely initialize this instance as evenly-spaced with slices parallel to the provided PlaneGeomet...
static bool CanReadFile(const std::string filename, const std::string filePrefix, const std::string filePattern)
MITKCORE_EXPORT mitk::PixelType MakePixelType(vtkImageData *vtkimagedata)
deduct the PixelType for a given vtk image
const char * GetFileName() const override
static const std::string filename
void SetOrigin(const Point3D &origin)
Set the origin, i.e. the upper-left corner of the plane.
const char * GetFilePrefix() const override
Get the specified file prefix for the file(s) to load.
Describes the geometry of a data object consisting of slices.
void SetFilePrefix(const char *aFilePrefix) override
Specify file prefix for the file(s) to load.
Describes a two-dimensional, rectangular plane.
const char * GetFilePattern() const override
Get the specified file pattern for the file(s) to load. The sprintf format used to build filename fro...
void SetFilePattern(const char *aFilePattern) override
Specified file pattern for the file(s) to load. The sprintf format used to build filename from FilePr...
mitk::AffineTransform3D * GetIndexToWorldTransform()
Get the transformation used to convert from index to world coordinates.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.