21 #include "itkImageFileReader.h"
22 #include "itkImageRegionIterator.h"
23 #include "itkMetaDataObject.h"
24 #include "itkNrrdImageIO.h"
25 #include <itkNiftiImageIO.h>
51 std::vector<itk::SmartPointer<mitk::BaseData> > result;
56 throw itk::ImageFileReaderException(__FILE__, __LINE__,
"Sorry, the filename is empty!");
66 std::string fname3 =
"temp_dti.nii";
67 itksys::SystemTools::CopyAFile(location.c_str(), fname3.c_str());
69 typedef itk::VectorImage<float,3>
ImageType;
71 typedef itk::ImageFileReader<ImageType> FileReaderType;
73 reader->SetImageIO(io);
74 reader->SetFileName(fname3);
78 typedef itk::Image<itk::DiffusionTensor3D<float>,3> VecImgType;
80 vecImg->SetSpacing( img->GetSpacing() );
81 vecImg->SetOrigin( img->GetOrigin() );
82 vecImg->SetDirection( img->GetDirection() );
83 vecImg->SetRegions( img->GetLargestPossibleRegion());
86 itk::ImageRegionIterator<VecImgType> ot (vecImg, vecImg->GetLargestPossibleRegion() );
89 itk::ImageRegionIterator<ImageType> it (img, img->GetLargestPossibleRegion() );
94 int numComponents = img->GetNumberOfComponentsPerPixel();
98 MITK_INFO <<
"Trying to load dti as 6-comp nifti ...";
101 VarPixType vec = it.Get();
102 FixPixType fixVec(vec.GetDataPointer());
104 itk::DiffusionTensor3D<float> tensor;
105 tensor.SetElement(0, vec.GetElement(0));
106 tensor.SetElement(1, vec.GetElement(1));
107 tensor.SetElement(2, vec.GetElement(2));
108 tensor.SetElement(3, vec.GetElement(3));
109 tensor.SetElement(4, vec.GetElement(4));
110 tensor.SetElement(5, vec.GetElement(5));
119 else if(numComponents==9)
121 MITK_INFO <<
"Trying to load dti as 9-comp nifti ...";
122 while (!it.IsAtEnd())
124 VarPixType vec = it.Get();
125 itk::DiffusionTensor3D<float> tensor;
126 tensor.SetElement(0, vec.GetElement(0));
127 tensor.SetElement(1, vec.GetElement(1));
128 tensor.SetElement(2, vec.GetElement(2));
129 tensor.SetElement(3, vec.GetElement(4));
130 tensor.SetElement(4, vec.GetElement(5));
131 tensor.SetElement(5, vec.GetElement(8));
133 FixPixType fixVec(tensor);
139 else if (numComponents==1)
141 MITK_INFO <<
"Trying to load dti as 4D nifti ...";
143 typedef itk::ImageFileReader<ImageType> FileReaderType;
145 reader->SetImageIO(io);
146 reader->SetFileName(fname3);
150 itk::Size<4> size = img->GetLargestPossibleRegion().GetSize();
152 while (!ot.IsAtEnd())
154 itk::DiffusionTensor3D<float> tensor;
155 ImageType::IndexType idx;
156 idx[0] = ot.GetIndex()[0]; idx[1] = ot.GetIndex()[1]; idx[2] = ot.GetIndex()[2];
160 for (
unsigned int te=0; te<size[3]; te++)
163 tensor.SetElement(te, img->GetPixel(idx));
169 tensor.SetElement(0, img->GetPixel(idx));
171 tensor.SetElement(1, img->GetPixel(idx));
173 tensor.SetElement(2, img->GetPixel(idx));
175 tensor.SetElement(3, img->GetPixel(idx));
177 tensor.SetElement(4, img->GetPixel(idx));
179 tensor.SetElement(5, img->GetPixel(idx));
182 throw itk::ImageFileReaderException(__FILE__, __LINE__,
"Unknown number of components for DTI file. Should be 6 or 9!");
184 FixPixType fixVec(tensor);
190 resultImage->InitializeByItk( vecImg.GetPointer() );
191 resultImage->SetVolume( vecImg->GetBufferPointer() );
192 result.push_back( resultImage.GetPointer() );
196 MITK_INFO <<
"Trying to load dti as nrrd ...";
198 typedef itk::VectorImage<float,3>
ImageType;
200 typedef itk::ImageFileReader<ImageType> FileReaderType;
202 reader->SetImageIO(io);
203 reader->SetFileName(location);
207 typedef itk::Image<itk::DiffusionTensor3D<float>,3> VecImgType;
209 vecImg->SetSpacing( img->GetSpacing() );
210 vecImg->SetOrigin( img->GetOrigin() );
211 vecImg->SetDirection( img->GetDirection() );
212 vecImg->SetRegions( img->GetLargestPossibleRegion());
215 itk::ImageRegionIterator<VecImgType> ot (vecImg, vecImg->GetLargestPossibleRegion() );
218 itk::ImageRegionIterator<ImageType> it (img, img->GetLargestPossibleRegion() );
223 int numComponents = img->GetNumberOfComponentsPerPixel();
225 itk::MetaDataDictionary imgMetaDictionary = img->GetMetaDataDictionary();
226 std::vector<std::string> imgMetaKeys = imgMetaDictionary.GetKeys();
227 std::vector<std::string>::const_iterator itKey = imgMetaKeys.begin();
228 std::string metaString;
230 bool readFrame =
false;
231 double xx, xy, xz, yx, yy, yz, zx, zy, zz;
233 measFrame.SetIdentity();
235 measFrameTransp.SetIdentity();
237 for (; itKey != imgMetaKeys.end(); itKey ++)
239 itk::ExposeMetaData<std::string> (imgMetaDictionary, *itKey, metaString);
240 if (itKey->find(
"measurement frame") != std::string::npos)
242 sscanf(metaString.c_str(),
" ( %lf , %lf , %lf ) ( %lf , %lf , %lf ) ( %lf , %lf , %lf ) \n", &xx, &xy, &xz, &yx, &yy, &yz, &zx, &zy, &zz);
244 if (xx>10e-10 || xy>10e-10 || xz>10e-10 ||
245 yx>10e-10 || yy>10e-10 || yz>10e-10 ||
246 zx>10e-10 || zy>10e-10 || zz>10e-10 )
260 measFrameTransp = measFrame.GetTranspose();
265 if (numComponents==6)
267 while (!it.IsAtEnd())
270 VarPixType vec = it.Get();
271 FixPixType fixVec(vec.GetDataPointer());
275 itk::DiffusionTensor3D<float> tensor;
276 tensor.SetElement(0, vec.GetElement(0));
277 tensor.SetElement(1, vec.GetElement(1));
278 tensor.SetElement(2, vec.GetElement(2));
279 tensor.SetElement(3, vec.GetElement(3));
280 tensor.SetElement(4, vec.GetElement(4));
281 tensor.SetElement(5, vec.GetElement(5));
283 tensor = ConvertMatrixTypeToFixedArrayType(tensor.PreMultiply(measFrame));
284 tensor = ConvertMatrixTypeToFixedArrayType(tensor.PostMultiply(measFrameTransp));
293 else if(numComponents==9)
295 while (!it.IsAtEnd())
297 VarPixType vec = it.Get();
298 itk::DiffusionTensor3D<float> tensor;
299 tensor.SetElement(0, vec.GetElement(0));
300 tensor.SetElement(1, vec.GetElement(1));
301 tensor.SetElement(2, vec.GetElement(2));
302 tensor.SetElement(3, vec.GetElement(4));
303 tensor.SetElement(4, vec.GetElement(5));
304 tensor.SetElement(5, vec.GetElement(8));
308 tensor = ConvertMatrixTypeToFixedArrayType(tensor.PreMultiply(measFrame));
309 tensor = ConvertMatrixTypeToFixedArrayType(tensor.PostMultiply(measFrameTransp));
312 FixPixType fixVec(tensor);
318 else if (numComponents==1)
322 typedef itk::ImageFileReader<ImageType> FileReaderType;
324 reader->SetImageIO(io);
325 reader->SetFileName(location);
329 itk::Size<4> size = img->GetLargestPossibleRegion().GetSize();
331 while (!ot.IsAtEnd())
333 itk::DiffusionTensor3D<float> tensor;
334 ImageType::IndexType idx;
335 idx[0] = ot.GetIndex()[0]; idx[1] = ot.GetIndex()[1]; idx[2] = ot.GetIndex()[2];
339 for (
unsigned int te=0; te<size[3]; te++)
342 tensor.SetElement(te, img->GetPixel(idx));
348 tensor.SetElement(0, img->GetPixel(idx));
350 tensor.SetElement(1, img->GetPixel(idx));
352 tensor.SetElement(2, img->GetPixel(idx));
354 tensor.SetElement(3, img->GetPixel(idx));
356 tensor.SetElement(4, img->GetPixel(idx));
358 tensor.SetElement(5, img->GetPixel(idx));
361 throw itk::ImageFileReaderException(__FILE__, __LINE__,
"Unknown number of komponents for DTI file. Should be 6 or 9!");
365 tensor = ConvertMatrixTypeToFixedArrayType(tensor.PreMultiply(measFrame));
366 tensor = ConvertMatrixTypeToFixedArrayType(tensor.PostMultiply(measFrameTransp));
368 FixPixType fixVec(tensor);
375 throw itk::ImageFileReaderException(__FILE__, __LINE__,
"Image has wrong number of pixel components!");
379 resultImage->InitializeByItk( vecImg.GetPointer() );
380 resultImage->SetVolume( vecImg->GetBufferPointer() );
381 result.push_back( resultImage.GetPointer() );
385 catch(std::exception& e)
387 throw itk::ImageFileReaderException(__FILE__, __LINE__, e.what());
391 throw itk::ImageFileReaderException(__FILE__, __LINE__,
"Sorry, an error occurred while reading the requested DTI file!");
398 itk::DiffusionTensor3D<float> NrrdTensorImageReader
399 ::ConvertMatrixTypeToFixedArrayType(
const itk::DiffusionTensor3D<float>::Superclass::MatrixType & matrix)
405 itk::DiffusionTensor3D<float> arr;
406 arr.SetElement(0,matrix(0,0));
407 arr.SetElement(1,matrix(0,1));
408 arr.SetElement(2,matrix(0,2));
409 arr.SetElement(3,matrix(1,3));
410 arr.SetElement(4,matrix(1,4));
411 arr.SetElement(5,matrix(2,5));
virtual std::vector< itk::SmartPointer< BaseData > > Read() override
Reads a path or stream and creates a list of BaseData objects.
itk::SmartPointer< Self > Pointer
DataCollection - Class to facilitate loading/accessing structured data.
The CustomMimeType class represents a custom mime-type which may be registered as a service object...
map::core::discrete::Elements< 3 >::InternalImageType ImageType
us::ServiceRegistration< IFileReader > RegisterService(us::ModuleContext *context=us::GetModuleContext())
Convenience class to temporarily change the current locale.
itk::Matrix< float, 3, 3 > MeasurementFrameType
Base class for creating mitk::BaseData objects from files or streams.
virtual ~NrrdTensorImageReader()
virtual std::string GetInputLocation() const override
Get the current input location.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.