File: TensorDerivedImage.cxx

package info (click to toggle)
ants 1.9.2%2Bsvn680.dfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 12,136 kB
  • sloc: cpp: 41,966; sh: 2,545; perl: 216; makefile: 43
file content (180 lines) | stat: -rw-r--r-- 6,018 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*=========================================================================

  Program:   ITK General
  Module:    $RCSfile: TensorDerivedImage.cxx,v $
  Language:  C++
  Date:      $Date: 2009/03/17 18:58:59 $
  Version:   $Revision: 1.2 $

  Author: Jeffrey T. Duda (jtduda@seas.upenn.edu)
  Institution: PICSL

  This software is distributed WITHOUT ANY WARRANTY; without even 
  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
  PURPOSE.

=========================================================================*/
#include <stdio.h>

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
//#include "itkDiffusionTensor3D.h"
#include "itkVector.h"
//#include "itkTensorFractionalAnisotropyImageFilter.h"
#include "itkImageRegionIteratorWithIndex.h"
#include <string>
#include "TensorFunctions.h"
#include "ReadWriteImage.h"
#include "itkRGBPixel.h"



 

 
int main( int argc, char *argv[] )
{
  
  // Pixel and Image typedefs
  typedef float                                 PixelType;
  typedef itk::Image<PixelType, 3>              ScalarImageType;
  typedef itk::ImageFileWriter<ScalarImageType> WriterType;

  //typedef itk::Vector<PixelType, 6>             TensorType;
  typedef itk::SymmetricSecondRankTensor< float, 3 >  TensorType; 
  typedef itk::Image<TensorType, 3>             TensorImageType;
  typedef itk::ImageFileReader<TensorImageType> ReaderType;
  typedef itk::RGBPixel< float >                ColorPixelType;
  typedef itk::Image<ColorPixelType,3>          ColorImageType;
  typedef itk::ImageFileWriter<ColorImageType>  ColorWriterType;

  // Check for valid input paramters
  if (argc < 3)
  {
    std::cout << "Usage: " << argv[0] << " tensorvolume outputvolume outputtype" << std::endl;
    return 1;
  }
    
  // Input parameters
  char * inputName = argv[1];
  char * outputName = argv[2];
  std::string outType = argv[3];  

  TensorImageType::Pointer dtimg;
  ReadTensorImage<TensorImageType>(dtimg, inputName, false);
  
  std::cout << "tensor_image: " << inputName << std::endl;
  std::cout << "output_image: " << outputName << std::endl;
  
  
  ScalarImageType::Pointer outImage;
  ColorImageType::Pointer colorImage;
  
  if (outType == "DEC") {
    colorImage = ColorImageType::New();
    colorImage->SetRegions(dtimg->GetLargestPossibleRegion() );
    colorImage->SetSpacing(dtimg->GetSpacing());
    colorImage->SetOrigin(dtimg->GetOrigin());
    colorImage->SetDirection(dtimg->GetDirection());
    colorImage->Allocate();
  }
  else {
    outImage = ScalarImageType::New();
    outImage->SetRegions(dtimg->GetLargestPossibleRegion() );
    outImage->SetSpacing(dtimg->GetSpacing());
    outImage->SetOrigin(dtimg->GetOrigin());
    outImage->SetDirection(dtimg->GetDirection());
    outImage->Allocate();
    }

  itk::ImageRegionIteratorWithIndex<TensorImageType> inputIt(dtimg,
    dtimg->GetLargestPossibleRegion());
    
  if ( (outType == "XX") || (outType == "xx") ) outType = "0";
  if ( (outType == "XY") || (outType == "YX") || (outType == "xy") || (outType == "yx") ) outType = "1";
  if ( (outType == "XZ") || (outType == "ZX") || (outType == "xz") || (outType == "zx") ) outType = "2";
  if ( (outType == "YY") || (outType == "yy") ) outType = "3";
  if ( (outType == "YZ") || (outType == "ZY") || (outType == "yz") || (outType == "zy") ) outType = "4";
  if ( (outType == "ZZ") || (outType == "zz") ) outType = "5";

  std::cout << "Calculating output..." << std::flush;

  while (!inputIt.IsAtEnd())
  {
    if ((outType == "0") || (outType == "1") || (outType == "2") || (outType == "3") ||
        (outType == "4") || (outType == "5"))
    {
      int idx = atoi(outType.c_str());
      outImage->SetPixel(inputIt.GetIndex(),inputIt.Value()[idx]);
    }
    else if ((outType == "TR") || (outType == "MD"))
    {
      ScalarImageType::PixelType tr;
      tr = inputIt.Value()[0] + inputIt.Value()[2] + inputIt.Value()[5];
      if (tr < 0)
        tr = 0;
      if (tr != tr)
        tr = 0;
      if (outType == "TR") {    
        outImage->SetPixel(inputIt.GetIndex(),tr);
      }
      else {
        outImage->SetPixel(inputIt.GetIndex(),tr/3.0);
        }
    }
    else if (outType == "V")
    {
      //unsigned int invalids = 0;
      bool success=true;
      TensorType t = TensorLogAndExp<TensorType>(inputIt.Value(), true, success);
      int current=1;
      if (!success) current=0;        
      outImage->SetPixel(inputIt.GetIndex(),current);
      //std::cout << "Found " << invalids << " invalid tensors" << std::endl;
    }
    else if (outType == "FA")
    {
      float fa = GetTensorFA<TensorType>(inputIt.Value());       
      outImage->SetPixel(inputIt.GetIndex(),fa);
    }
    else if (outType == "DEC") {
      colorImage->SetPixel(inputIt.GetIndex(), 
                         GetTensorRGB<TensorType>(inputIt.Value()));
      ++inputIt;
      }
  }
  
  std::cout << "Done. " << std::endl;
  
  if (outType == "DEC") {
    std::cout << "output origin: " << colorImage->GetOrigin() << std::endl;
    std::cout << "output size: " << colorImage->GetLargestPossibleRegion().GetSize() << std::endl;
    std::cout << "output spacing: " << colorImage->GetSpacing() << std::endl;
    std::cout << "output direction: " << colorImage->GetDirection() << std::endl;
  
  }
  else {
    std::cout << "output origin: " << outImage->GetOrigin() << std::endl;
    std::cout << "output size: " << outImage->GetLargestPossibleRegion().GetSize() << std::endl;
    std::cout << "output spacing: " << outImage->GetSpacing() << std::endl;
    std::cout << "output direction: " << outImage->GetDirection() << std::endl;
    }
  
  if (outType == "DEC") {
    ColorWriterType::Pointer writer = ColorWriterType::New();
    writer->SetInput( colorImage );
    writer->SetFileName( outputName );
    writer->Update();
  }
  else {
    WriterType::Pointer writer = WriterType::New();
    writer->SetInput( outImage );
    writer->SetFileName( outputName );
    writer->Update();
    }

  return 0;

}