From: Romulo Pinho Date: Tue, 10 Jan 2012 14:18:12 +0000 (+0100) Subject: invert VF on given image grid X-Git-Tag: v1.3.0~122 X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=2252420857d1554424d1f2c69ea68547bc759678;p=clitk.git invert VF on given image grid - with --like option --- diff --git a/itk/clitkInvertVFFilter.h b/itk/clitkInvertVFFilter.h index 2afceb6..586d872 100644 --- a/itk/clitkInvertVFFilter.h +++ b/itk/clitkInvertVFFilter.h @@ -71,15 +71,21 @@ namespace clitk m_NumberOfThreads=r; } itkSetMacro(ThreadSafe, bool); + itkSetMacro(OutputSpacing, SpacingType); + itkSetMacro(OutputSize, SizeType); protected: InvertVFFilter(); ~InvertVFFilter() {}; void GenerateData( ); - + void GenerateOutputInformation(); + void GenerateInputRequestedRegion(); + bool m_Verbose; bool m_NumberOfThreadsIsGiven; + SpacingType m_OutputSpacing; + SizeType m_OutputSize; unsigned int m_NumberOfThreads; PixelType m_EdgePaddingValue; bool m_ThreadSafe; diff --git a/itk/clitkInvertVFFilter.txx b/itk/clitkInvertVFFilter.txx index 0c443ca..ae6be89 100644 --- a/itk/clitkInvertVFFilter.txx +++ b/itk/clitkInvertVFFilter.txx @@ -17,6 +17,7 @@ ===========================================================================**/ #ifndef __clitkInvertVFFilter_txx #define __clitkInvertVFFilter_txx + namespace { @@ -73,6 +74,8 @@ protected: ~HelperClass1() {}; //the actual processing + void GenerateInputRequestedRegion(); + void GenerateOutputInformation(); void BeforeThreadedGenerateData(); void ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread, int threadId ); @@ -98,11 +101,39 @@ HelperClass1::HelperClass1() m_ThreadSafe=false; } +//========================================================================================================================= +//Set input image params +template +void HelperClass1::GenerateInputRequestedRegion() +{ + //std::cout << "HelperClass1::GenerateInputRequestedRegion - IN" << std::endl; + + typename InputImageType::Pointer inputPtr = const_cast< InputImageType * >(this->GetInput()); + inputPtr->SetRequestedRegion(inputPtr->GetLargestPossibleRegion()); + + //std::cout << "HelperClass1::GenerateInputRequestedRegion - OUT" << std::endl; +} + +//========================================================================================================================= +//Set output image params +template +void HelperClass1::GenerateOutputInformation() +{ + //std::cout << "HelperClass1::GenerateOutputInformation - IN" << std::endl; + Superclass::GenerateOutputInformation(); + + // it's the weight image that determines the size of the output + typename OutputImageType::Pointer outputPtr = this->GetOutput(); + outputPtr->SetLargestPossibleRegion( m_Weights->GetLargestPossibleRegion() ); + outputPtr->SetSpacing(m_Weights->GetSpacing()); +} + //========================================================================================================================= //Before threaded data template void HelperClass1::BeforeThreadedGenerateData() { + //std::cout << "HelperClass1::BeforeThreadedGenerateData - IN" << std::endl; //Since we will add, put to zero! this->GetOutput()->FillBuffer(itk::NumericTraits::Zero); this->GetWeights()->FillBuffer(itk::NumericTraits::Zero); @@ -113,49 +144,58 @@ void HelperClass1::BeforeThreadedGenerateData() template void HelperClass1::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread, int threadId ) { - + //std::cout << "HelperClass1::ThreadedGenerateData - IN" << std::endl; //Get pointer to the input typename InputImageType::ConstPointer inputPtr = this->GetInput(); //Get pointer to the output typename OutputImageType::Pointer outputPtr = this->GetOutput(); - //typename OutputImageType::SizeType size=outputPtr->GetLargestPossibleRegion().GetSize(); +/* outputPtr->SetLargestPossibleRegion( m_Weights->GetLargestPossibleRegion() ); + outputPtr->SetSpacing(m_Weights->GetSpacing());*/ + typename OutputImageType::SizeType size=outputPtr->GetLargestPossibleRegion().GetSize(); + //std::cout << outputPtr->GetSpacing() << size << std::endl; //Iterator over input - typedef itk::ImageRegionConstIteratorWithIndex InputImageIteratorType; + //typedef itk::ImageRegionConstIteratorWithIndex InputImageIteratorType; + typedef itk::ImageRegionConstIteratorWithIndex InputImageIteratorType; //define them over the outputRegionForThread - InputImageIteratorType inputIt(inputPtr, outputRegionForThread); + //InputImageIteratorType outputIt(inputPtr, outputRegionForThread); + InputImageIteratorType outputIt(outputPtr, outputPtr->GetLargestPossibleRegion()); //Initialize typename InputImageType::IndexType index; - itk::ContinuousIndex contIndex; + itk::ContinuousIndex contIndex, inContIndex; typename InputImageType::PointType ipoint; typename OutputImageType::PointType opoint; typedef typename OutputImageType::PixelType DisplacementType; DisplacementType displacement; - inputIt.GoToBegin(); + outputIt.GoToBegin(); //define some temp variables signed long baseIndex[ImageDimension]; double distance[ImageDimension]; unsigned int dim, counter, upper; double totalOverlap,overlap; - typename OutputImageType::IndexType neighIndex; + typename OutputImageType::IndexType neighIndex, outIndex; //Find the number of neighbors unsigned int neighbors = 1 << ImageDimension; //================================================================================================== - //Loop over the region and add the intensities to the output and the weight to the weights + //Loop over the output region and add the intensities from the input to the output and the weight to the weights //================================================================================================== - while( !inputIt.IsAtEnd() ) { + while( !outputIt.IsAtEnd() ) { // get the input image index - index = inputIt.GetIndex(); + outIndex = outputIt.GetIndex(); + outputPtr->TransformIndexToPhysicalPoint( outIndex,opoint ); + for(unsigned int j = 0; j < ImageDimension; j++ ) ipoint[j] = opoint[j]; + inputPtr->TransformPhysicalPointToIndex(ipoint, index); inputPtr->TransformIndexToPhysicalPoint( index,ipoint ); // get the required displacement - displacement = inputIt.Get(); + //displacement = outputIt.Get(); + displacement = inputPtr->GetPixel(index); // compute the required output image point for(unsigned int j = 0; j < ImageDimension; j++ ) opoint[j] = ipoint[j] + (double)displacement[j]; @@ -230,9 +270,10 @@ void HelperClass1::ThreadedGenerateData(const O } } - ++inputIt; + ++outputIt; } + //std::cout << "HelperClass1::ThreadedGenerateData - OUT" << std::endl; } @@ -310,7 +351,8 @@ template HelperClass2 void HelperClass2::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread, int threadId ) { - + //std::cout << "HelperClass2::ThreadedGenerateData - IN" << std::endl; + //Get pointer to the input typename InputImageType::ConstPointer inputPtr = this->GetInput(); @@ -360,6 +402,9 @@ template void HelperClass2::InvertVFFilter() m_Verbose=false; } +//========================================================================================================================= +//Set input image params +template +void InvertVFFilter::GenerateInputRequestedRegion() +{ + //std::cout << "InvertVFFilter::GenerateInputRequestedRegion - IN" << std::endl; + // call the superclass' implementation of this method + // Superclass::GenerateInputRequestedRegion(); + + typename InputImageType::Pointer inputPtr = const_cast< InputImageType * >(this->GetInput()); + inputPtr->SetRequestedRegion(inputPtr->GetLargestPossibleRegion()); + + //std::cout << "InvertVFFilter::GenerateInputRequestedRegion - OUT" << std::endl; +} + +//========================================================================================================================= +//Set output image params +template +void InvertVFFilter::GenerateOutputInformation() +{ + //std::cout << "InvertVFFilter::GenerateOutputInformation - IN" << std::endl; + Superclass::GenerateOutputInformation(); + + typename InputImageType::ConstPointer inputPtr=this->GetInput(); + typename WeightsImageType::RegionType region = inputPtr->GetLargestPossibleRegion(); + region.SetSize(m_OutputSize); + + typename OutputImageType::Pointer outputPtr = this->GetOutput(); + outputPtr->SetRegions( region ); + outputPtr->SetSpacing(m_OutputSpacing); + outputPtr->SetOrigin(inputPtr->GetOrigin()); + + //std::cout << "InvertVFFilter::GenerateOutputInformation - OUT" << std::endl; +} //========================================================================================================================= //Update template void InvertVFFilter::GenerateData() { + //std::cout << "InvertVFFilter::GenerateData - IN" << std::endl; //Get the properties of the input typename InputImageType::ConstPointer inputPtr=this->GetInput(); - typename WeightsImageType::RegionType region; - typename WeightsImageType::RegionType::SizeType size=inputPtr->GetLargestPossibleRegion().GetSize(); - region.SetSize(size); - typename OutputImageType::IndexType start; - for (unsigned int i=0; i< ImageDimension; i++) start[i]=0; - region.SetIndex(start); - + typename WeightsImageType::RegionType region = inputPtr->GetLargestPossibleRegion(); + //region.SetSize(size); + region.SetSize(m_OutputSize); //Allocate the weights typename WeightsImageType::Pointer weights=WeightsImageType::New(); + weights->SetOrigin(inputPtr->GetOrigin()); weights->SetRegions(region); + weights->SetSpacing(m_OutputSpacing); weights->Allocate(); - weights->SetSpacing(inputPtr->GetSpacing()); + //weights->SetSpacing(inputPtr->GetSpacing()); //=========================================================================== //Inversion is divided in in two loops, for each we will call a threaded helper class @@ -435,7 +513,8 @@ template void InvertVFFilterSetRegions(region); mutex->Allocate(); - mutex->SetSpacing(inputPtr->GetSpacing()); + //mutex->SetSpacing(inputPtr->GetSpacing()); + mutex->SetSpacing(m_OutputSpacing); helper1->SetMutexImage(mutex); if (m_Verbose) std::cout <<"Inverting using a thread-safe algorithm" < void InvertVFFilterGetSpacing() << temp->GetLargestPossibleRegion().GetSize() << std::endl; + //std::cout << weights->GetSpacing() << weights->GetLargestPossibleRegion().GetSize() << std::endl; helper2->SetInput(temp); helper2->SetWeights(weights); helper2->SetEdgePaddingValue(m_EdgePaddingValue); diff --git a/tools/clitkInvertVF.ggo b/tools/clitkInvertVF.ggo index 54c1e21..19c451a 100644 --- a/tools/clitkInvertVF.ggo +++ b/tools/clitkInvertVF.ggo @@ -15,3 +15,4 @@ option "type" t "Type of filter: 0=clitk (fast forward splat using line option "threadSafe" - "Clitk: use thread safe algorithm" flag off option "pad" p "Clitk: edge padding value (1 or N number of values!, defautls to zeros)" double multiple no option "sampling" s "Itk: subsampling factor" int no default="20" +option "like" l "Image whose grid (spacing and size) will be used for output" string no diff --git a/tools/clitkInvertVFGenericFilter.txx b/tools/clitkInvertVFGenericFilter.txx index 9835c2e..2ddd46c 100644 --- a/tools/clitkInvertVFGenericFilter.txx +++ b/tools/clitkInvertVFGenericFilter.txx @@ -129,6 +129,19 @@ InvertVFGenericFilter::UpdateWithDimAndPixelType() typedef clitk::InvertVFFilter FilterType; typename FilterType::Pointer filter =FilterType::New(); filter->SetInput(input); + typename FilterType::SpacingType spacing = input->GetSpacing(); + typename FilterType::SizeType size = input->GetLargestPossibleRegion().GetSize(); + if (m_ArgsInfo.like_given) { + itk::ImageIOBase::Pointer header = readImageHeader(m_ArgsInfo.like_arg); + for(unsigned int i=0; iGetDimensions(i); + spacing[i] = header->GetSpacing(i); + } + } + std::cout << spacing << size << std::endl; + filter->SetOutputSpacing(spacing); + filter->SetOutputSize(size); + filter->SetVerbose(m_Verbose); if (m_ArgsInfo.threads_given) filter->SetNumberOfThreads(m_ArgsInfo.threads_arg); if (m_ArgsInfo.pad_given) {