2 #include "itkVectorImage.h"
3 #include "itkImageSeriesReader.h"
4 #include "itkImageFileReader.h"
5 #include "itkImageFileWriter.h"
6 #include "itkMetaDataDictionary.h"
7 #include "itkAddImageFilter.h"
8 #include "itkUnaryFunctorImageFilter.h"
11 #include "itkImageRegionConstIterator.h"
12 #include "StochasticTractographyFilterCLP.h"
13 #include "itkTensorFractionalAnisotropyImageFilter.h"
14 #include "itkPathIterator.h"
19 const int length = fileName.length();
20 for (
int i=0; i!=length; ++i){
21 if (fileName[i]==
'.'){
22 return fileName.substr(0,i);
29 template<
class TTOContainerType >
34 typename TTOContainerType::Element direction;
38 if(!m_file.is_open()){
39 std::cerr<<
"Problems opening: "<< fn << std::endl;
46 unsigned int directioncount=0;
48 while(!m_file.getline(str, 100,
'\n').eof()){
50 sscanf(str,
"%lf %lf %lf",&x,&y,&z);
55 directions->InsertElement(directioncount++,direction);
64 template<
class TractContainerType >
66 std::ofstream tractfile( fn, std::ios::out | std::ios::binary );
68 if(tractfile.is_open()){
69 for(
int i=0; i<tractcontainer->Size(); i++ ){
70 typename TractContainerType::Element tract =
71 tractcontainer->GetElement(i);
74 tract->GetVertexList();
76 for(
int j=0; j<vertexlist->Size(); j++){
77 typename TractContainerType::Element::ObjectType::VertexListType::Element vertex =
78 vertexlist->GetElement(j);
79 tractfile.write((
char*)&vertex[0],
sizeof(vertex[0]));
80 tractfile.write((
char*)&vertex[1],
sizeof(vertex[1]));
81 tractfile.write((
char*)&vertex[2],
sizeof(vertex[2]));
83 double endoftractsymbol = 0.0;
84 tractfile.write((
char*)&endoftractsymbol,
sizeof(endoftractsymbol));
85 tractfile.write((
char*)&endoftractsymbol,
sizeof(endoftractsymbol));
86 tractfile.write((
char*)&endoftractsymbol,
sizeof(endoftractsymbol));
93 std::cerr<<
"Problems opening file to write tracts\n";
97 template<
class FAContainerType >
99 std::ofstream fafile( fn, std::ios::out );
103 if(fafile.is_open()){
104 for(
int i=0; i<facontainer->Size(); i++){
105 fafile<<facontainer->GetElement(i)<<std::endl;
111 std::cerr<<
"Problems opening file to write FA value\n";
118 template<
class TInput,
class TOutput>
128 bool operator==(
const ZeroDWITest & other )
const
130 return !(*
this != other);
132 inline TOutput operator()(
const TInput & A )
145 template<
class TInput,
class TOutput>
150 ~ScalarMultiply() {};
151 bool operator!=(
const ScalarMultiply & )
const
155 bool operator==(
const ScalarMultiply & other )
const
157 return !(*
this != other);
159 inline TOutput operator()(
const TInput & A )
174 int main(
int argc,
char* argv[]){
177 typedef itk::VectorImage< unsigned short int, 3 > DWIVectorImageType;
178 typedef itk::Image< float, 3 > WMPImageType;
179 typedef itk::Image< short int, 3 > ROIImageType;
180 typedef itk::Image< unsigned int, 3 > CImageType;
183 typedef itk::ImageRegionConstIterator< ROIImageType > ROIImageIteratorType;
186 typedef itk::ImageFileReader< DWIVectorImageType > DWIVectorImageReaderType;
187 typedef itk::ImageFileReader< WMPImageType > WMPImageReaderType;
188 typedef itk::ImageFileReader< ROIImageType > ROIImageReaderType;
189 typedef itk::ImageFileWriter< CImageType > CImageWriterType;
190 typedef itk::ImageFileWriter< WMPImageType > WMPImageWriterType;
193 typedef itk::MetaDataDictionary DictionaryType;
194 typedef DictionaryType::ConstIterator DictionaryIteratorType;
201 typedef PTFilterType::bValueContainerType bValueContainerType;
202 typedef PTFilterType::GradientDirectionContainerType GDContainerType;
203 typedef PTFilterType::MeasurementFrameType MeasurementFrameType;
206 typedef Functor::ZeroDWITest< DWIVectorImageType::PixelType, WMPImageType::PixelType >
208 typedef itk::UnaryFunctorImageFilter< DWIVectorImageType, WMPImageType,
209 ZeroDWITestType > ExcludeZeroDWIFilterType;
212 typedef itk::Image< double, 3 > FAImageType;
213 typedef itk::TensorFractionalAnisotropyImageFilter< PTFilterType::OutputTensorImageType,
214 FAImageType > FAFilterType;
215 typedef itk::ImageFileWriter< FAImageType > FAImageWriterType;
218 typedef Functor::ScalarMultiply< FAImageType::PixelType, FAImageType::PixelType >
220 typedef itk::UnaryFunctorImageFilter< FAImageType, FAImageType,
221 ScalarMultiplyType > MultiplyFAFilterType;
224 typedef itk::AddImageFilter< CImageType, CImageType, CImageType> AddImageFilterType;
243 std::string fafilename = outputprefix +
"_CONDFAValues.txt";
245 std::string lengthfilename = outputprefix +
"_CONDLENGTHValues.txt";
249 std::ofstream fafile( fafilename.c_str() );
253 if(!fafile.is_open()){
254 std::cerr<<
"Could not open FA file!\n";
258 std::ofstream lengthfile( lengthfilename.c_str() );
267 dwireaderPtr->SetFileName(dwifilename);
268 dwireaderPtr->Update();
271 DictionaryType& dictionary = dwireaderPtr->GetMetaDataDictionary();
273 MeasurementFrameType measurement_frame;
275 bValueContainerType::Element scaling_bValue = 0;
276 GDContainerType::Element g_i;
280 for(DictionaryIteratorType dictit = dictionary.Begin();
281 dictit!=dictionary.End();
283 if(dictit->first.find(
"DWMRI_gradient") != std::string::npos){
284 std::string metaDataValue;
285 itk::ExposeMetaData< std::string > (dictionary, dictit->first, metaDataValue);
286 sscanf(metaDataValue.c_str(),
"%lf %lf %lf\n", &g_i[0], &g_i[1], &g_i[2]);
288 gradientsPtr->InsertElement( gradientsPtr->Size(), g_i.normalize() );
290 else if(dictit->first.find(
"DWMRI_b-value") != std::string::npos){
291 std::string metaDataValue;
292 itk::ExposeMetaData< std::string >(dictionary, dictit->first, metaDataValue);
293 scaling_bValue = atof(metaDataValue.c_str());
295 else if(dictit->first.find(
"NRRD_measurement frame") != std::string::npos){
296 std::vector< std::vector < double > > metaDataValue;
297 itk::ExposeMetaData< std::vector< std::vector<double> > >
298 (dictionary, dictit->first, metaDataValue);
299 for(
int i=0;i<3;i++){
301 measurement_frame(i,j) = metaDataValue[j][i];
305 std::cout << dictit->first << std::endl;
310 if(scaling_bValue == 0){
311 std::cerr <<
"scaling_bValue should never be 0, possibly not found in Nrrd header\n";
314 else if(gradientsPtr->Size() == 0){
315 std::cerr <<
"no gradients were found!";
318 else if(measurement_frame.size() == 0){
319 std::cerr <<
"no measurement frame was found!";
324 std::cout << scaling_bValue << std::endl;
325 for(
unsigned int i=0; i<gradientsPtr->Size(); i++)
326 std::cout << gradientsPtr->GetElement(i) << std::endl;
330 for(
unsigned int i=0; i<measurement_frame.rows(); i++){
331 for(
unsigned int j=0; j<measurement_frame.columns(); j++){
332 std::cout<<measurement_frame(i,j) <<
" ";
334 std::cout << std::endl;
341 for(
unsigned int i=0; i<gradientsPtr->Size() ;i++){
342 if(gradientsPtr->GetElement(i).squared_magnitude() == 0){
343 bValuesPtr->InsertElement(i, 0);
348 bValuesPtr->InsertElement(i, scaling_bValue);
354 roireaderPtr->SetFileName(roifilename);
355 roireaderPtr->Update();
359 wmpreader->SetFileName(wmpfilename);
363 if(recenteroriginswitch==
true){
364 roireaderPtr->GetOutput()->SetOrigin( dwireaderPtr->GetOutput()->GetOrigin() );
365 wmpreader->GetOutput()->SetOrigin( dwireaderPtr->GetOutput()->GetOrigin() );
399 std::cout<<
"Create PTFilter\n";
402 ptfilterPtr->SetInput( dwireaderPtr->GetOutput() );
404 ptfilterPtr->SetWhiteMatterProbabilityImageInput( wmpreader->GetOutput() );
405 ptfilterPtr->SetbValues(bValuesPtr);
406 ptfilterPtr->SetGradients( gradientsPtr );
407 ptfilterPtr->SetMeasurementFrame( measurement_frame );
409 ptfilterPtr->SetMaxTractLength( maxtractlength );
410 ptfilterPtr->SetTotalTracts( totaltracts );
411 ptfilterPtr->SetMaxLikelihoodCacheSize( maxlikelihoodcachesize );
412 if(totalthreads!=0) ptfilterPtr->SetNumberOfThreads( totalthreads );
415 ptfilterPtr->GenerateTensorImageOutput();
420 fafilter->SetInput( ptfilterPtr->GetOutputTensorImage() );
424 typedef itk::VectorContainer< unsigned int, double > FAContainerType;
428 typedef itk::VectorContainer< unsigned int, unsigned int > LengthContainerType;
436 accumulatedcimagePtr->CopyInformation( dwireaderPtr->GetOutput() );
437 accumulatedcimagePtr->SetBufferedRegion( dwireaderPtr->GetOutput()->GetBufferedRegion() );
438 accumulatedcimagePtr->SetRequestedRegion( dwireaderPtr->GetOutput()->GetRequestedRegion() );
439 accumulatedcimagePtr->Allocate();
440 accumulatedcimagePtr->FillBuffer(0);
445 conditionedcimagePtr->CopyInformation( dwireaderPtr->GetOutput() );
446 conditionedcimagePtr->SetBufferedRegion( dwireaderPtr->GetOutput()->GetBufferedRegion() );
447 conditionedcimagePtr->SetRequestedRegion( dwireaderPtr->GetOutput()->GetRequestedRegion() );
448 conditionedcimagePtr->Allocate();
449 conditionedcimagePtr->FillBuffer(0);
452 addimagefilterPtr->GraftOutput(accumulatedcimagePtr);
453 addimagefilterPtr->SetInput1(ptfilterPtr->GetOutput());
454 addimagefilterPtr->SetInput2(addimagefilterPtr->GetOutput());
456 ROIImageIteratorType ROIImageIt( roireaderPtr->GetOutput(),
457 roireaderPtr->GetOutput()->GetRequestedRegion() );
458 for(ROIImageIt.GoToBegin(); !ROIImageIt.IsAtEnd(); ++ROIImageIt){
459 if(ROIImageIt.Get() == labelnumber){
460 std::cout <<
"PixelIndex: "<< ROIImageIt.GetIndex() << std::endl;
461 ptfilterPtr->SetSeedIndex( ROIImageIt.GetIndex() );
466 addimagefilterPtr->Update();
468 std::cout<<
"Calculate Statistics\n";
471 ptfilterPtr->GetOutputTractContainer();
473 typedef itk::PathIterator< ROIImageType, PTFilterType::TractType >
474 ROITractIteratorType;
476 for(
int i=0; i<tractcontainer->Size(); i++ ){
478 ROITractIteratorType roitractIt( roireaderPtr->GetOutput(),
481 for(roitractIt.GoToBegin(); !roitractIt.IsAtEnd(); ++roitractIt){
482 if(roitractIt.Get() == endlabelnumber){
484 unsigned int stepcount=0;
486 for(
double t=0; t<currtract->EndOfInput(); t+=0.2){
489 accumFA+=fafilter->GetOutput()->GetPixel(currtract->EvaluateToIndex(t));
491 fafile<<accumFA/((double)stepcount)<<std::endl;
492 lengthfile<<tractcontainer->GetElement(i)->EndOfInput()<<std::endl;
493 if(fafile.fail() || lengthfile.fail() ){
494 std::cerr<<
"Error writing to text files\n";
502 for(roitractIt.GoToBegin(); !roitractIt.IsAtEnd(); ++roitractIt){
503 conditionedcimagePtr->GetPixel(roitractIt.GetIndex())++;
512 std::cout<<
"DWI image origin:"<< dwireaderPtr->GetOutput()->GetOrigin() <<std::endl;
513 std::cout<<
"ROI image origin:"<< roireaderPtr->GetOutput()->GetOrigin() <<std::endl;
514 std::cout<<
"wmp image origin:"<< wmpreader->GetOutput()->GetOrigin() <<std::endl;
515 if(outputimageswitch){
517 std::string cfilename = outputprefix +
"_CMAP.nhdr";
520 writerPtr->SetInput( accumulatedcimagePtr );
521 writerPtr->SetFileName( cfilename.c_str() );
525 std::string tensorimagefilename = outputprefix +
"_TENSOR.nhdr";
527 typedef itk::ImageFileWriter< PTFilterType::OutputTensorImageType > TensorImageWriterType;
529 tensorwriter->SetInput( ptfilterPtr->GetOutputTensorImage() );
530 tensorwriter->SetFileName( tensorimagefilename.c_str() );
531 tensorwriter->Update();
536 multFAfilter->SetInput( fafilter->GetOutput() );
538 std::string faimagefilename = outputprefix +
"_FA.nhdr";
541 fawriter->SetInput( multFAfilter->GetOutput() );
542 fawriter->SetFileName( faimagefilename.c_str() );
547 condcmapwriterPtr->SetInput( conditionedcimagePtr );
548 std::string condcmapfilename = outputprefix +
"_COND.nhdr";
550 condcmapwriterPtr->SetFileName( condcmapfilename );
551 condcmapwriterPtr->Update();
556 if(fafile.fail() || lengthfile.fail() ){
557 std::cerr<<
"Error closing text files\n";
int main(int argc, char *argv[])
itk::SmartPointer< Self > Pointer
MITKCORE_EXPORT bool operator!=(const InteractionEvent &a, const InteractionEvent &b)
bool WriteTractContainerToFile(const char *fn, TractContainerType *tractcontainer)
std::string stripExtension(const std::string &fileName)
MITKCORE_EXPORT bool operator==(const InteractionEvent &a, const InteractionEvent &b)
itk::SmartPointer< const Self > ConstPointer
bool WriteScalarContainerToFile(const char *fn, FAContainerType *facontainer)
bool SamplingDirections(const char *fn, typename TTOContainerType::Pointer directions)
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.