17 #ifndef __mitkDiffusionImageNiftiReaderService_cpp
18 #define __mitkDiffusionImageNiftiReaderService_cpp
33 #include <itkImageRegionIterator.h>
34 #include <itkMetaDataObject.h>
35 #include "itksys/SystemTools.hxx"
36 #include "itkImageFileReader.h"
37 #include "itkMetaDataObject.h"
38 #include "itkNiftiImageIO.h"
77 std::vector<itk::SmartPointer<mitk::BaseData> >
81 std::vector<itk::SmartPointer<mitk::BaseData> > result;
102 throw itk::ImageFileReaderException(__FILE__, __LINE__,
"Sorry, the filename to be read is empty!");
110 MITK_INFO <<
"DiffusionImageNiftiReaderService: reading image information";
114 ext = itksys::SystemTools::LowerCase( ext );
116 if(ext ==
".fsl" || ext ==
".fslgz")
119 std::string fname3 =
"temp_dwi";
120 fname3 += ext ==
".fsl" ?
".nii" :
".nii.gz";
121 itksys::SystemTools::CopyAFile(this->
GetInputLocation().c_str(), fname3.c_str());
124 typedef itk::Image<DiffusionPixelType,4> ImageType4D;
126 typedef itk::ImageFileReader<ImageType4D> FileReaderType;
128 reader->SetFileName(fname3);
129 reader->SetImageIO(io2);
134 itksys::SystemTools::RemoveFile(fname3.c_str());
139 VectorImageType::SpacingType spacing;
140 ImageType4D::SpacingType spacing4 = img4->GetSpacing();
141 for(
int i=0; i<3; i++)
142 spacing[i] = spacing4[i];
143 itkVectorImage->SetSpacing( spacing );
147 for(
int i=0; i<3; i++)
148 origin[i] = origin4[i];
149 itkVectorImage->SetOrigin( origin );
151 VectorImageType::DirectionType direction;
152 ImageType4D::DirectionType direction4 = img4->GetDirection();
153 for(
int i=0; i<3; i++)
154 for(
int j=0; j<3; j++)
155 direction[i][j] = direction4[i][j];
156 itkVectorImage->SetDirection( direction );
158 VectorImageType::RegionType region;
159 ImageType4D::RegionType region4 = img4->GetLargestPossibleRegion();
161 VectorImageType::RegionType::SizeType size;
162 ImageType4D::RegionType::SizeType size4 = region4.GetSize();
164 for(
int i=0; i<3; i++)
167 VectorImageType::RegionType::IndexType index;
168 ImageType4D::RegionType::IndexType index4 = region4.GetIndex();
169 for(
int i=0; i<3; i++)
170 index[i] = index4[i];
172 region.SetSize(size);
173 region.SetIndex(index);
174 itkVectorImage->SetRegions( region );
176 itkVectorImage->SetVectorLength(size4[3]);
177 itkVectorImage->Allocate();
179 itk::ImageRegionIterator<VectorImageType> it ( itkVectorImage, itkVectorImage->GetLargestPossibleRegion() );
181 for (it.GoToBegin(); !it.IsAtEnd(); ++it)
183 VecPixType vec = it.Get();
184 VectorImageType::IndexType currentIndex = it.GetIndex();
185 for(
int i=0; i<3; i++)
186 index4[i] = currentIndex[i];
187 for(
unsigned int ind=0; ind<vec.Size(); ind++)
190 vec[ind] = img4->GetPixel(index4);
195 else if(ext ==
".nii" || ext ==
".nii.gz")
198 typedef itk::Image<DiffusionPixelType,4> ImageType4D;
200 typedef itk::ImageFileReader<ImageType4D> FileReaderType;
203 reader->SetImageIO(io2);
211 VectorImageType::SpacingType spacing;
212 ImageType4D::SpacingType spacing4 = img4->GetSpacing();
213 for(
int i=0; i<3; i++)
214 spacing[i] = spacing4[i];
215 itkVectorImage->SetSpacing( spacing );
219 for(
int i=0; i<3; i++)
220 origin[i] = origin4[i];
221 itkVectorImage->SetOrigin( origin );
223 VectorImageType::DirectionType direction;
224 ImageType4D::DirectionType direction4 = img4->GetDirection();
225 for(
int i=0; i<3; i++)
226 for(
int j=0; j<3; j++)
227 direction[i][j] = direction4[i][j];
228 itkVectorImage->SetDirection( direction );
230 VectorImageType::RegionType region;
231 ImageType4D::RegionType region4 = img4->GetLargestPossibleRegion();
233 VectorImageType::RegionType::SizeType size;
234 ImageType4D::RegionType::SizeType size4 = region4.GetSize();
236 for(
int i=0; i<3; i++)
239 VectorImageType::RegionType::IndexType index;
240 ImageType4D::RegionType::IndexType index4 = region4.GetIndex();
241 for(
int i=0; i<3; i++)
242 index[i] = index4[i];
244 region.SetSize(size);
245 region.SetIndex(index);
246 itkVectorImage->SetRegions( region );
248 itkVectorImage->SetVectorLength(size4[3]);
249 itkVectorImage->Allocate();
251 itk::ImageRegionIterator<VectorImageType> it ( itkVectorImage, itkVectorImage->GetLargestPossibleRegion() );
253 for (it.GoToBegin(); !it.IsAtEnd(); ++it)
255 VecPixType vec = it.Get();
256 VectorImageType::IndexType currentIndex = it.GetIndex();
257 for(
int i=0; i<3; i++)
258 index4[i] = currentIndex[i];
259 for(
unsigned int ind=0; ind<vec.Size(); ind++)
262 vec[ind] = img4->GetPixel(index4);
275 if(ext ==
".fsl" || ext ==
".fslgz" || ext ==
".nii" || ext ==
".nii.gz")
278 bool useFSLstyle(
true );
279 std::string bvecsExtension(
"");
280 std::string bvalsExtension(
"");
282 std::string base = itksys::SystemTools::GetFilenamePath( this->
GetInputLocation() ) +
"/"
287 if( useFSLstyle && itksys::SystemTools::FileExists( std::string( base +
".bvec").c_str() )
288 && itksys::SystemTools::FileExists( std::string( base +
".bval").c_str() )
292 bvecsExtension =
".bvec";
293 bvalsExtension =
".bval";
296 if( useFSLstyle && itksys::SystemTools::FileExists( std::string( base +
".bvecs").c_str() )
297 && itksys::SystemTools::FileExists( std::string( base +
".bvals").c_str() )
301 bvecsExtension =
".bvecs";
302 bvalsExtension =
".bvals";
307 std::vector<float> bvec_entries;
315 fname = std::string( base + bvecsExtension);
317 std::ifstream myfile (fname.c_str());
318 if (myfile.is_open())
320 while ( myfile.good() )
323 getline (myfile,line2);
325 std::stringstream iss;
328 while(getline(iss,line,
' '))
331 line.erase(std::remove_if(line.begin(), line.end(),
332 [](
char c) {
return std::isspace(c) || std::iscntrl(c); } ), line.end());
334 if (line.length() > 0 )
336 bvec_entries.push_back(atof(line.c_str()));
344 MITK_INFO <<
"Unable to open bvecs file. Expected name: " << fname;
347 std::vector<float> bval_entries;
355 fname2 = std::string( base + bvalsExtension);
357 std::ifstream myfile2 (fname2.c_str());
358 if (myfile2.is_open())
360 while ( myfile2.good() )
362 getline (myfile2,line,
' ');
364 line.erase(std::remove_if(line.begin(), line.end(),
365 [](
char c) {
return std::isspace(c) || std::iscntrl(c); } ), line.end());
367 if (line.length() > 0 )
369 bval_entries.push_back(atof(line.c_str()));
378 MITK_INFO <<
"Unable to open bvals file. Expected name: " << fname2;
383 unsigned int numb = bval_entries.size();
384 for(
unsigned int i=0; i<numb; i++)
388 if(BValue == -1 && bval_entries.at(i) != 0)
390 BValue = bval_entries.at(i);
393 float b_val = bval_entries.at(i);
396 vnl_vector_fixed< double, 3 > vec;
397 vec[0] = bvec_entries.at(i);
398 vec[1] = bvec_entries.at(i+numb);
399 vec[2] = bvec_entries.at(i+2*numb);
402 float factor = b_val/BValue;
403 if(vec.magnitude() > 0)
405 vec[0] = sqrt(factor)*vec[0];
406 vec[1] = sqrt(factor)*vec[1];
407 vec[2] = sqrt(factor)*vec[2];
410 DiffusionVectors->InsertElement(i,vec);
413 for(
int i=0; i<3; i++)
414 for(
int j=0; j<3; j++)
415 MeasurementFrame[i][j] = i==j ? 1 : 0;
434 catch(std::exception& e)
436 MITK_INFO <<
"Std::Exception while reading file!!";
438 throw itk::ImageFileReaderException(__FILE__, __LINE__, e.what());
442 MITK_INFO <<
"Exception while reading file!!";
443 throw itk::ImageFileReaderException(__FILE__, __LINE__,
"Sorry, an error occurred while reading the requested vessel tree file!");
static const std::string REFERENCEBVALUEPROPERTYNAME
itk::SmartPointer< Self > Pointer
std::map< unsigned int, std::vector< unsigned int > > BValueMap
The BValueMap contains seperated IndicesVectors for each b value (index for GradientDirectionContaine...
std::string GetFilenameWithoutExtension(const std::string &path) const
Provides the filename minus the extension.
static BValueMap CreateBValueMap(const GradientDirectionsContainerType *gdc, float referenceBValue)
DataCollection - Class to facilitate loading/accessing structured data.
Image::Pointer GrabItkImageMemory(itk::SmartPointer< ItkOutputImageType > &itkimage, mitk::Image *mitkImage=nullptr, const BaseGeometry *geometry=nullptr, bool update=true)
Grabs the memory of an itk::Image (with a specific type) and puts it into an mitk::Image.The memory is managed by the mitk::Image after calling this function. The itk::Image remains valid until the mitk::Image decides to free the memory.
static const std::string MEASUREMENTFRAMEPROPERTYNAME
mitk::DiffusionPropertyHelper::MeasurementFrameType MeasurementFrameType
The CustomMimeType class represents a custom mime-type which may be registered as a service object...
virtual ~DiffusionImageNiftiReaderService()
virtual std::vector< itk::SmartPointer< BaseData > > Read() override
Reads a path or stream and creates a list of BaseData objects.
us::ServiceRegistration< IFileReader > RegisterService(us::ModuleContext *context=us::GetModuleContext())
OutputType::Pointer m_OutputCache
Convenience class to temporarily change the current locale.
const CustomMimeType * GetMimeType() const
Base class for creating mitk::BaseData objects from files or streams.
std::string GetExtension(const std::string &path) const
Provides the first matching extension.
DiffusionImageNiftiReaderService()
itk::TimeStamp m_CacheTime
virtual std::string GetInputLocation() const override
Get the current input location.
static const std::string GRADIENTCONTAINERPROPERTYNAME
static const std::string BVALUEMAPPROPERTYNAME
static const std::string ORIGINALGRADIENTCONTAINERPROPERTYNAME
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.