19 #ifndef __itkProfileFromSPFImageFilter_hxx 20 #define __itkProfileFromSPFImageFilter_hxx 22 #include <itkProgressReporter.h> 31 template<
class TInputImage,
class TOutputImage >
37 Superclass::GenerateOutputInformation();
38 typename TOutputImage::Pointer outputPtr = this->GetOutput();
39 unsigned int numberOfComponentsPerPixel = 0;
40 if (this->m_Orientations->Rows()==0)
48 numberOfComponentsPerPixel = this->m_Orientations->Rows();
49 utlGlobalException(m_RadiusVector->size()>0 && m_RadiusVector->size()!=this->m_Orientations->Rows(),
"wrong size of m_RadiusVector or m_Orientations");
51 outputPtr->SetNumberOfComponentsPerPixel(numberOfComponentsPerPixel);
54 template<
class TInputImage,
class TOutputImage >
60 Superclass::VerifyInputParameters();
61 utlGlobalException(this->m_Radius<0 && this->m_RadiusVector->size()==0,
"need to set radius or radiusVector");
64 template<
class TInputImage,
class TOutputImage >
65 typename LightObject::Pointer
69 typename LightObject::Pointer loPtr = Superclass::InternalClone();
74 itkExceptionMacro(<<
"downcast to type " << this->GetNameOfClass()<<
" failed.");
76 rval->m_Radius = m_Radius;
77 rval->m_RadiusVector = m_RadiusVector;
81 template<
class TInputImage,
class TOutputImage >
87 if (this->m_Orientations->Rows()==0)
91 utlException(this->m_SHRank<0 || this->m_RadialRank<0, "need to set this->m_SHRank and this->m_RadialRank
"); 93 // if it is in q-space, then convert b value to q value 94 double radius = this->m_Radius; 95 if ((this->m_IsInQSpace && !this->m_IsFourier) || (!this->m_IsInQSpace && this->m_IsFourier)) 96 radius = std::sqrt(this->m_Radius/(4*M_PI*M_PI*this->m_Tau)); 98 // std::cout << "radius =
" << radius << std::endl << std::flush; 99 if (this->m_BasisType==Superclass::SPF || this->m_BasisType==Superclass::DSPF) 101 typedef SphericalPolarFourierRadialGenerator<double> SPFGenerator; 102 int n_b_sh = (this->m_SHRank+1)*(this->m_SHRank+2)/2; 103 int n_b_ra = this->m_RadialRank+1; 104 int n_b = n_b_sh * n_b_ra; 105 typename SPFGenerator::Pointer spf = SPFGenerator::New(); 107 // NOTE: if use ReSize, different threads modify the same data block, which is not thread safe. 108 // this->m_SPFToFeatureTransform->ReSize(n_b_sh,n_b); 109 this->m_SPFToFeatureTransform = MatrixPointer (new MatrixType(n_b_sh,n_b) ); 110 this->m_SPFToFeatureTransform->Fill(0.0); 112 if ( (this->m_BasisType==Superclass::SPF && !this->m_IsFourier) || (this->m_BasisType==Superclass::DSPF && this->m_IsFourier) ) 114 spf->SetSPFType(SPFGenerator::SPF); 115 spf->SetScale(this->m_BasisScale); 116 for ( int n = 0; n <= this->m_RadialRank; n += 1 ) 119 double spfValue = spf->Evaluate(radius); 121 for ( int l = 0; l <= this->m_SHRank; l += 2 ) 123 for ( int m = -l; m <= l; m += 1 ) 125 (*this->m_SPFToFeatureTransform)(jj,n*n_b_sh+jj) = spfValue; 126 // utlPrintVar4(true, n,l,m, spf->Evaluate(radius)); 134 spf->SetSPFType(SPFGenerator::DSPF); 135 spf->SetScale(this->m_BasisScale); 136 for ( int n = 0; n <= this->m_RadialRank; n += 1 ) 140 for ( int l = 0; l <= this->m_SHRank; l += 2 ) 143 double spfValue = spf->Evaluate(radius); 144 for ( int m = -l; m <= l; m += 1 ) 146 (*this->m_SPFToFeatureTransform)(jj,n*n_b_sh+jj) = spfValue; 157 if (m_RadiusVector->size()==0) 159 utlException(this->m_Radius<0, "need to
set m_Radius or m_RadiusVector
"); 160 utl::MatchBVectorAndGradientMatrix(this->m_Radius, *m_RadiusVector, *this->m_Orientations); 162 utlException(m_RadiusVector->size()>0 && m_RadiusVector->size()!=this->m_Orientations->Rows(), "wrong size of m_RadiusVector or m_Orientations
"); 163 this->m_SPFIEstimator->SetBasisScale(this->m_BasisScale); 164 // std::cout << "this->m_BasisScale =
" << this->m_BasisScale << std::endl << std::flush; 165 this->m_SPFIEstimator->ComputeBasisMatrix(); 166 this->m_SPFToFeatureTransform = this->m_SPFIEstimator->GetBasisMatrix(); 167 // this->m_SPFIEstimator->Print(std::cout<<"m_SPFIEstimator :
"); 170 if (this->GetDebug()) 171 utl::PrintUtlMatrix(*this->m_SPFToFeatureTransform, "this->m_SPFToFeatureTransform
"); 174 template <class TInputImage, class TOutputImage> 176 ProfileFromSPFImageFilter<TInputImage,TOutputImage> 177 ::BeforeThreadedGenerateData() 179 utlShowPosition(this->GetDebug()); 180 // this->Print(std::cout<<"this=
"); 181 this->SetSPFIEstimator(); 183 if (this->m_Orientations->Rows()>0) 185 this->m_SPFIEstimator->GetSamplingSchemeQSpace()->SetOrientationsSpherical(this->m_Orientations); 186 if (m_RadiusVector->size()==0) 188 utlException(this->m_Radius<0, "need to
set m_Radius or m_RadiusVector
"); 189 utl::MatchBVectorAndGradientMatrix(this->m_Radius, *m_RadiusVector, *this->m_Orientations); 191 this->m_SPFIEstimator->GetSamplingSchemeQSpace()->SetTau(this->m_Tau); 192 this->m_SPFIEstimator->SetSHRank(this->m_SHRank); 193 this->m_SPFIEstimator->SetRadialRank(this->m_RadialRank); 194 this->m_SPFIEstimator->SetBasisScale(this->m_BasisScale); 195 this->m_SPFIEstimator->SetScaleImage(this->m_ScaleImage); 196 this->m_SPFIEstimator->SetDebug(this->GetDebug()); 198 bool needToConvertToQValue = (this->m_IsInQSpace && !this->m_IsFourier) || (!this->m_IsInQSpace && this->m_IsFourier); 199 if (needToConvertToQValue) 200 this->m_SPFIEstimator->GetSamplingSchemeQSpace()->SetBVector(this->m_RadiusVector); 202 this->m_SPFIEstimator->GetSamplingSchemeQSpace()->SetRadiusVector(this->m_RadiusVector); 206 // this->m_SPFIEstimator->Print(std::cout<<"m_SPFIEstimator:
"); 208 Superclass::BeforeThreadedGenerateData(); 211 template <class TInputImage, class TOutputImage> 213 ProfileFromSPFImageFilter<TInputImage,TOutputImage> 214 ::ThreadedGenerateData( const typename TOutputImage::RegionType &outputRegionForThread, ThreadIdType threadId) 216 utlShowPosition(this->GetDebug()); 217 typename TInputImage::ConstPointer inputPtr = this->GetInput(); 218 typename TOutputImage::Pointer outputPtr = this->GetOutput(); 220 // Define the iterators 221 ImageRegionConstIteratorWithIndex<TInputImage> inputIt(inputPtr, outputRegionForThread); 222 ImageRegionIteratorWithIndex<TOutputImage> outputIt(outputPtr, outputRegionForThread); 223 ImageRegionIteratorWithIndex<MaskImageType> maskIt; 224 if (this->IsMaskUsed()) 225 maskIt = ImageRegionIteratorWithIndex<MaskImageType>(this->m_MaskImage, outputRegionForThread); 227 ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels()); 228 typename TInputImage::PixelType inputPixel; 229 typename TInputImage::IndexType inputIndex; 230 typename TOutputImage::PixelType outputPixel; 232 unsigned int outputDim = outputPtr->GetNumberOfComponentsPerPixel(); 233 outputPixel.SetSize(outputDim); 234 unsigned int inputDim = inputPtr->GetNumberOfComponentsPerPixel(); 235 inputPixel.SetSize(inputDim); 238 outputIt.GoToBegin(); 239 VectorType spfVec, temp; 241 Pointer selfClone = this->Clone(); 243 while( !inputIt.IsAtEnd() ) 245 if (!this->IsMaskUsed() || (this->IsMaskUsed() && maskIt.Get()>0)) 247 inputPixel = inputIt.Get(); 248 if (inputPixel.GetSquaredNorm()>1e-8) 250 inputIndex = inputIt.GetIndex(); 251 if (this->GetDebug()) 252 std::cout << "inputIndex =
" << inputIndex << std::endl << std::flush; 253 if (!IsImageEmpty(this->m_ScaleImage)) 254 selfClone->SetBasisScale(this->m_ScaleImage->GetPixel(inputIndex)); 255 if (selfClone->m_SPFToFeatureTransform->Size()==0) 256 selfClone->ComputeSPFToFeatureTransform(); 257 spfVec = utl::VariableLengthVectorToUtlVector(inputPixel); 258 // utl::PrintUtlVector(spfVec, "spfVec
"); 259 // utl::PrintUtlMatrix(*selfClone->m_SPFToFeatureTransform, "*selfClone->m_SPFToFeatureTransform
"); 261 utl::ProductUtlMv(*selfClone->m_SPFToFeatureTransform, spfVec, temp); 262 outputPixel = utl::UtlVectorToVariableLengthVector(temp); 263 if (this->GetDebug()) 265 if (!IsImageEmpty(this->m_ScaleImage)) 266 std::cout << "scale =
" << this->m_ScaleImage->GetPixel(inputIndex) << std::endl << std::flush; 267 itk::PrintVariableLengthVector(inputPixel, "inputPixel
"); 268 itk::PrintVariableLengthVector(outputPixel, "outputPixel
"); 272 outputPixel.Fill(0.0); 275 outputPixel.Fill(0.0); 277 outputIt.Set(outputPixel); 278 progress.CompletedPixel(); 279 if (this->IsMaskUsed()) 283 progress.CompletedPixel(); // potential exception thrown here 287 template< class TInputImage, class TOutputImage > 289 ProfileFromSPFImageFilter< TInputImage, TOutputImage > 290 ::PrintSelf(std::ostream &os, Indent indent) const 292 Superclass::PrintSelf(os, indent); 294 << ", m_Radius =
" << this->m_Radius 296 utl::PrintVector(*m_RadiusVector, "m_RadiusVector
", " ", os<<indent); helper functions specifically used in dmritool
LightObject::Pointer InternalClone() const ITK_OVERRIDE
#define utlException(cond, expout)
#define utlGlobalException(cond, expout)
int RankToDimSH(const int shRank)
void VerifyInputParameters() const ITK_OVERRIDE
#define utlShowPosition(cond)
void GenerateOutputInformation() ITK_OVERRIDE
Compute some features (DWI/EAP profile, ODFs, scalar indices) from SPF coefficients.
void ComputeSPFToFeatureTransform() ITK_OVERRIDE
SmartPointer< Self > Pointer