00001 /*========================================================================= 00002 00003 Program: Medical Imaging & Interaction Toolkit 00004 Language: C++ 00005 Date: $Date: 2008-02-08 11:19:03 +0100 (Fr, 08 Feb 2008) $ 00006 Version: $Revision: 11989 $ 00007 00008 Copyright (c) German Cancer Research Center, Division of Medical and 00009 Biological Informatics. All rights reserved. 00010 See MITKCopyright.txt or https://www.mitk.org/copyright.html for details. 00011 00012 This software is distributed WITHOUT ANY WARRANTY; without even 00013 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00014 PURPOSE. See the above copyright notices for more information. 00015 00016 =========================================================================*/ 00017 00018 #include "mitkTensorImage.h" 00019 #include "mitkImageDataItem.h" 00020 #include "mitkImageCast.h" 00021 00022 #include "itkDiffusionTensor3D.h" 00023 #include "itkTensorToRgbImageFilter.h" 00024 00025 #include "vtkImageData.h" 00026 00027 // #ifdef _OPENMP 00028 // #include "omp.h" 00029 // #endif 00030 00031 mitk::TensorImage::TensorImage() : Image() 00032 { 00033 m_RgbImage = 0; 00034 } 00035 00036 mitk::TensorImage::~TensorImage() 00037 { 00038 00039 } 00040 00041 vtkImageData* mitk::TensorImage::GetVtkImageData(int t, int n) 00042 { 00043 if(m_RgbImage.IsNull()) 00044 { 00045 ConstructRgbImage(); 00046 } 00047 return m_RgbImage->GetVtkImageData(t,n); 00048 } 00049 00050 void mitk::TensorImage::ConstructRgbImage() 00051 { 00052 typedef itk::Image<itk::DiffusionTensor3D<float>,3> ImageType; 00053 typedef itk::TensorToRgbImageFilter<ImageType> FilterType; 00054 FilterType::Pointer filter = FilterType::New(); 00055 00056 ImageType::Pointer itkvol = ImageType::New(); 00057 mitk::CastToItkImage<ImageType>(this, itkvol); 00058 filter->SetInput(itkvol); 00059 filter->Update(); 00060 00061 m_RgbImage = mitk::Image::New(); 00062 m_RgbImage->InitializeByItk( filter->GetOutput() ); 00063 m_RgbImage->SetVolume( filter->GetOutput()->GetBufferPointer() ); 00064 } 00065 00066 vtkImageData* mitk::TensorImage::GetNonRgbVtkImageData(int t, int n) 00067 { 00068 return Superclass::GetVtkImageData(t,n); 00069 } 00070 00071 // 00072 //vtkImageData* mitk::TensorImage::GetRgbVtkImageData(int t, int n) 00073 //{ 00074 // if(m_Initialized==false) 00075 // { 00076 // if(GetSource()==NULL) 00077 // return NULL; 00078 // if(GetSource()->Updating()==false) 00079 // GetSource()->UpdateOutputInformation(); 00080 // } 00081 // 00082 // if(m_VtkImageData==NULL) 00083 // ConstructVtkImageData(); 00084 // return m_VtkImageData; 00085 // 00086 // ImageDataItemPointer volume=GetVolumeData(t, n); 00087 // if(volume.GetPointer()==NULL || volume->GetVtkImageData() == NULL) 00088 // return NULL; 00089 // 00090 //#if ((VTK_MAJOR_VERSION > 4) || ((VTK_MAJOR_VERSION==4) && (VTK_MINOR_VERSION>=4) )) 00091 // float *fspacing = const_cast<float *>(GetSlicedGeometry(t)->GetFloatSpacing()); 00092 // double dspacing[3] = {fspacing[0],fspacing[1],fspacing[2]}; 00093 // volume->GetVtkImageData()->SetSpacing( dspacing ); 00094 //#else 00095 // volume->GetVtkImageData()->SetSpacing(const_cast<float*>(GetSlicedGeometry(t)->GetFloatSpacing())); 00096 //#endif 00097 // return volume->GetVtkImageData(); 00098 //} 00099 // 00100 //void mitk::TensorImage::ConstructRgbVtkImageData(int t, int n) const 00101 //{ 00102 // vtkImageData *inData = vtkImageData::New(); 00103 // vtkDataArray *scalars = NULL; 00104 // 00105 // mitkIpPicDescriptor* picDescriptor = GetVolumeData(t,n)->GetPicDescriptor(); 00106 // 00107 // unsigned long size = 0; 00108 // if ( picDescriptor->dim == 1 ) 00109 // { 00110 // inData->SetDimensions( picDescriptor->n[0] -1, 1, 1); 00111 // size = picDescriptor->n[0]; 00112 // inData->SetOrigin( ((float) picDescriptor->n[0]) / 2.0f, 0, 0 ); 00113 // } 00114 // else if ( picDescriptor->dim == 2 ) 00115 // { 00116 // inData->SetDimensions( picDescriptor->n[0] , picDescriptor->n[1] , 1 ); 00117 // size = picDescriptor->n[0] * picDescriptor->n[1]; 00118 // inData->SetOrigin( ((float) picDescriptor->n[0]) / 2.0f, ((float) picDescriptor->n[1]) / 2.0f, 0 ); 00119 // } 00120 // else if ( picDescriptor->dim >= 3 ) 00121 // { 00122 // inData->SetDimensions( picDescriptor->n[0], picDescriptor->n[1], picDescriptor->n[2] ); 00123 // size = picDescriptor->n[0] * picDescriptor->n[1] * picDescriptor->n[2]; 00124 // // Test 00125 // //inData->SetOrigin( (float) picDescriptor->n[0] / 2.0f, (float) picDescriptor->n[1] / 2.0f, (float) picDescriptor->n[2] / 2.0f ); 00126 // inData->SetOrigin( 0, 0, 0 ); 00127 // } 00128 // else 00129 // { 00130 // inData->Delete () ; 00131 // return; 00132 // } 00133 // 00134 // // allocate new scalars with three components for RGB 00135 // inData->SetNumberOfScalarComponents(3); 00136 // inData->SetScalarType( VTK_UNSIGNED_CHAR ); 00137 // scalars = vtkUnsignedCharArray::New(); 00138 // m_VtkImageDataTensor = inData; 00139 // scalars->SetNumberOfComponents(m_VtkImageDataTensor->GetNumberOfScalarComponents()); 00140 // 00141 // // calculate RGB information from tensors 00142 // if(m_PixelType == typeid(itk::DiffusionTensor3D<float>)) 00143 // scalars->SetVoidArray(ConvertTensorsToRGB<itk::DiffusionTensor3D<float> >(), m_Size/2, 1); 00144 // if(m_PixelType == typeid(itk::DiffusionTensor3D<double>)) 00145 // scalars->SetVoidArray(ConvertTensorsToRGB<itk::DiffusionTensor3D<double> >(), m_Size/2, 1); 00146 // 00147 // m_VtkImageDataTensor->GetPointData()->SetScalars(scalars); 00148 // scalars->Delete(); 00149 // 00150 //} 00151 // 00153 //* This method calculates RGB image from tensor information. 00154 //* Templated over pixeltype, output always three uchar components. 00155 //*/ 00156 //template <class TPixeltype> 00157 //unsigned char *mitk::ImageDataItem::ConvertTensorsToRGB() const 00158 //{ 00159 // const unsigned char *p = m_Data; 00160 // unsigned char *out = (unsigned char *) malloc(m_Size/2); 00161 // const int pixelsize = sizeof(TPixeltype); 00162 // const int numIts = m_Size/pixelsize; 00163 // 00164 //#ifdef _OPENMP 00165 //#pragma omp parallel for 00166 //#endif 00167 // for(int i=0; i<numIts; i++) 00168 // { 00169 // const TPixeltype* tensor = static_cast<TPixeltype* >((void*)(p+i*pixelsize)); 00170 // typename TPixeltype::EigenValuesArrayType eigenvalues; 00171 // typename TPixeltype::EigenVectorsMatrixType eigenvectors; 00172 // tensor->ComputeEigenAnalysis(eigenvalues, eigenvectors); 00173 // 00174 // int index = 2; 00175 // if( (eigenvalues[0] >= eigenvalues[1]) 00176 // && (eigenvalues[0] >= eigenvalues[2]) ) 00177 // index = 0; 00178 // else if(eigenvalues[1] >= eigenvalues[2]) 00179 // index = 1; 00180 // 00181 // const float fa = tensor->GetFractionalAnisotropy(); 00182 // float r = abs(eigenvectors(index,0)) * fa; 00183 // float g = abs(eigenvectors(index,1)) * fa; 00184 // float b = abs(eigenvectors(index,2)) * fa; 00185 // 00186 // __IMG_DAT_ITEM__CEIL_ZERO_ONE__(r); 00187 // __IMG_DAT_ITEM__CEIL_ZERO_ONE__(g); 00188 // __IMG_DAT_ITEM__CEIL_ZERO_ONE__(b); 00189 // 00190 // *(out+i*3+0) = (unsigned char)( 255.0f * r ); 00191 // *(out+i*3+1) = (unsigned char)( 255.0f * g ); 00192 // *(out+i*3+2) = (unsigned char)( 255.0f * b ); 00193 // } 00194 // return out; 00195 //}