void DicomDiffusionImageReader<TPixelType, TDimension> ::EnlargeOutputRequestedRegion(itk::DataObject *output) { typename OutputImageType::Pointer out = dynamic_cast<OutputImageType*>(output); out->SetRequestedRegion( out->GetLargestPossibleRegion() ); }
void ExtractChannelFromRgbaImageFilter< ReferenceImageType, OutputImageType >::GenerateData() { typename InputImageType::Pointer rgbaImage = static_cast< InputImageType * >( this->ProcessObject::GetInput(0) ); typename OutputImageType::Pointer outputImage = static_cast< OutputImageType * >(this->ProcessObject::GetOutput(0)); typename InputImageType::RegionType region = rgbaImage->GetLargestPossibleRegion(); outputImage->SetSpacing( m_ReferenceImage->GetSpacing() ); // Set the image spacing outputImage->SetOrigin( m_ReferenceImage->GetOrigin() ); // Set the image origin outputImage->SetDirection( m_ReferenceImage->GetDirection() ); // Set the image direction outputImage->SetRegions( m_ReferenceImage->GetLargestPossibleRegion()); outputImage->Allocate(); outputImage->FillBuffer(0); float* outImageBufferPointer = outputImage->GetBufferPointer(); itk::Image< short, 3 >::Pointer counterImage = itk::Image< short, 3 >::New(); counterImage->SetSpacing( m_ReferenceImage->GetSpacing() ); // Set the image spacing counterImage->SetOrigin( m_ReferenceImage->GetOrigin() ); // Set the image origin counterImage->SetDirection( m_ReferenceImage->GetDirection() ); // Set the image direction counterImage->SetRegions( m_ReferenceImage->GetLargestPossibleRegion()); counterImage->Allocate(); counterImage->FillBuffer(0); short* counterImageBufferPointer = counterImage->GetBufferPointer(); int w = m_ReferenceImage->GetLargestPossibleRegion().GetSize().GetElement(0); int h = m_ReferenceImage->GetLargestPossibleRegion().GetSize().GetElement(1); int d = m_ReferenceImage->GetLargestPossibleRegion().GetSize().GetElement(2); typedef ImageRegionConstIterator< InputImageType > InImageIteratorType; InImageIteratorType rgbaIt(rgbaImage, region); rgbaIt.GoToBegin(); while(!rgbaIt.IsAtEnd()){ InPixelType x = rgbaIt.Get(); ++rgbaIt; itk::Point<float, 3> vertex; itk::Index<3> index = rgbaIt.GetIndex(); rgbaImage->TransformIndexToPhysicalPoint(index, vertex); outputImage->TransformPhysicalPointToIndex(vertex, index); itk::ContinuousIndex<float, 3> contIndex; outputImage->TransformPhysicalPointToContinuousIndex(vertex, contIndex); float frac_x = contIndex[0] - index[0]; float frac_y = contIndex[1] - index[1]; float frac_z = contIndex[2] - index[2]; int px = index[0]; if (frac_x<0) { px -= 1; frac_x += 1; } int py = index[1]; if (frac_y<0) { py -= 1; frac_y += 1; } int pz = index[2]; if (frac_z<0) { pz -= 1; frac_z += 1; } frac_x = 1-frac_x; frac_y = 1-frac_y; frac_z = 1-frac_z; // int coordinates inside image? if (px < 0 || px >= w-1) continue; if (py < 0 || py >= h-1) continue; if (pz < 0 || pz >= d-1) continue; OutPixelType out; switch (m_Channel) { case RED: out = (float)x.GetRed()/255; break; case GREEN: out = (float)x.GetGreen()/255; break; case BLUE: out = (float)x.GetBlue()/255; break; case ALPHA: out = (float)x.GetAlpha()/255; } outImageBufferPointer[( px + w*(py + h*pz ))] += out*( frac_x)*( frac_y)*( frac_z); outImageBufferPointer[( px + w*(py+1+ h*pz ))] += out*( frac_x)*(1-frac_y)*( frac_z); outImageBufferPointer[( px + w*(py + h*pz+h))] += out*( frac_x)*( frac_y)*(1-frac_z); outImageBufferPointer[( px + w*(py+1+ h*pz+h))] += out*( frac_x)*(1-frac_y)*(1-frac_z); outImageBufferPointer[( px+1 + w*(py + h*pz ))] += out*(1-frac_x)*( frac_y)*( frac_z); outImageBufferPointer[( px+1 + w*(py + h*pz+h))] += out*(1-frac_x)*( frac_y)*(1-frac_z); outImageBufferPointer[( px+1 + w*(py+1+ h*pz ))] += out*(1-frac_x)*(1-frac_y)*( frac_z); outImageBufferPointer[( px+1 + w*(py+1+ h*pz+h))] += out*(1-frac_x)*(1-frac_y)*(1-frac_z); counterImageBufferPointer[( px + w*(py + h*pz ))] += 1; counterImageBufferPointer[( px + w*(py+1+ h*pz ))] += 1; counterImageBufferPointer[( px + w*(py + h*pz+h))] += 1; counterImageBufferPointer[( px + w*(py+1+ h*pz+h))] += 1; counterImageBufferPointer[( px+1 + w*(py + h*pz ))] += 1; counterImageBufferPointer[( px+1 + w*(py + h*pz+h))] += 1; counterImageBufferPointer[( px+1 + w*(py+1+ h*pz ))] += 1; counterImageBufferPointer[( px+1 + w*(py+1+ h*pz+h))] += 1; } typedef ImageRegionIterator< OutputImageType > OutImageIteratorType; OutImageIteratorType outIt(outputImage, outputImage->GetLargestPossibleRegion()); outIt.GoToBegin(); typedef ImageRegionConstIterator< itk::Image< short, 3 > > CountImageIteratorType; CountImageIteratorType counterIt(counterImage, counterImage->GetLargestPossibleRegion()); counterIt.GoToBegin(); while(!outIt.IsAtEnd() && !counterIt.IsAtEnd()){ if (counterIt.Value()>0) outIt.Set(outIt.Value()/counterIt.Value()); ++outIt; ++counterIt; } }
template <class inputType, unsigned int Dimension> medAbstractJob::medJobExitStatus medItkBiasCorrectionProcess::N4BiasCorrectionCore() { medJobExitStatus eRes = medAbstractJob::MED_JOB_EXIT_SUCCESS; typedef itk::Image<inputType, Dimension > ImageType; typedef itk::Image <float, Dimension> OutputImageType; typedef itk::Image<unsigned char, Dimension> MaskImageType; typedef itk::N4BiasFieldCorrectionImageFilter<OutputImageType, MaskImageType, OutputImageType> BiasFilter; typedef itk::ConstantPadImageFilter<OutputImageType, OutputImageType> PadderType; typedef itk::ConstantPadImageFilter<MaskImageType, MaskImageType> MaskPadderType; typedef itk::ShrinkImageFilter<OutputImageType, OutputImageType> ShrinkerType; typedef itk::ShrinkImageFilter<MaskImageType, MaskImageType> MaskShrinkerType; typedef itk::BSplineControlPointImageFilter<typename BiasFilter::BiasFieldControlPointLatticeType, typename BiasFilter::ScalarImageType> BSplinerType; typedef itk::ExpImageFilter<OutputImageType, OutputImageType> ExpFilterType; typedef itk::DivideImageFilter<OutputImageType, OutputImageType, OutputImageType> DividerType; typedef itk::ExtractImageFilter<OutputImageType, OutputImageType> CropperType; unsigned int uiThreadNb = static_cast<unsigned int>(m_poUIThreadNb->value()); unsigned int uiShrinkFactors = static_cast<unsigned int>(m_poUIShrinkFactors->value()); unsigned int uiSplineOrder = static_cast<unsigned int>(m_poUISplineOrder->value()); float fWienerFilterNoise = static_cast<float>(m_poFWienerFilterNoise->value()); float fbfFWHM = static_cast<float>(m_poFbfFWHM->value()); float fConvergenceThreshold = static_cast<float>(m_poFConvergenceThreshold->value()); float fSplineDistance = static_cast<float>(m_poFSplineDistance->value()); float fProgression = 0; QStringList oListValue = m_poSMaxIterations->value().split("x"); std::vector<unsigned int> oMaxNumbersIterationsVector(oListValue.size()); std::vector<float> oInitialMeshResolutionVect(Dimension); for (int i=0; i<oMaxNumbersIterationsVector.size(); ++i) { oMaxNumbersIterationsVector[i] = (unsigned int)oListValue[i].toInt(); } oInitialMeshResolutionVect[0] = static_cast<float>(m_poFInitialMeshResolutionVect1->value()); oInitialMeshResolutionVect[1] = static_cast<float>(m_poFInitialMeshResolutionVect2->value()); oInitialMeshResolutionVect[2] = static_cast<float>(m_poFInitialMeshResolutionVect3->value()); typename ImageType::Pointer image = dynamic_cast<ImageType *>((itk::Object*)(this->input()->data())); typedef itk::CastImageFilter <ImageType, OutputImageType> CastFilterType; typename CastFilterType::Pointer castFilter = CastFilterType::New(); castFilter->SetInput(image); /********************************************************************************/ /***************************** PREPARING STARTING *******************************/ /********************************************************************************/ /*** 0 ******************* Create filter and accessories ******************/ ABORT_CHECKING(m_bAborting); typename BiasFilter::Pointer filter = BiasFilter::New(); typename BiasFilter::ArrayType oNumberOfControlPointsArray; m_filter = filter; /*** 1 ******************* Read input image *******************************/ ABORT_CHECKING(m_bAborting); fProgression = 1; updateProgression(fProgression); /*** 2 ******************* Creating Otsu mask *****************************/ ABORT_CHECKING(m_bAborting); itk::TimeProbe timer; timer.Start(); typename MaskImageType::Pointer maskImage = ITK_NULLPTR; typedef itk::OtsuThresholdImageFilter<OutputImageType, MaskImageType> ThresholderType; typename ThresholderType::Pointer otsu = ThresholderType::New(); m_filter = otsu; otsu->SetInput(castFilter->GetOutput()); otsu->SetNumberOfHistogramBins(200); otsu->SetInsideValue(0); otsu->SetOutsideValue(1); otsu->SetNumberOfThreads(uiThreadNb); otsu->Update(); updateProgression(fProgression); maskImage = otsu->GetOutput(); /*** 3A *************** Set Maximum number of Iterations for the filter ***/ ABORT_CHECKING(m_bAborting); typename BiasFilter::VariableSizeArrayType itkTabMaximumIterations; itkTabMaximumIterations.SetSize(oMaxNumbersIterationsVector.size()); for (int i = 0; i < oMaxNumbersIterationsVector.size(); ++i) { itkTabMaximumIterations[i] = oMaxNumbersIterationsVector[i]; } filter->SetMaximumNumberOfIterations(itkTabMaximumIterations); /*** 3B *************** Set Fitting Levels for the filter *****************/ typename BiasFilter::ArrayType oFittingLevelsTab; oFittingLevelsTab.Fill(oMaxNumbersIterationsVector.size()); filter->SetNumberOfFittingLevels(oFittingLevelsTab); updateProgression(fProgression); /*** 4 ******************* Save image's index, size, origine **************/ ABORT_CHECKING(m_bAborting); typename ImageType::IndexType oImageIndex = image->GetLargestPossibleRegion().GetIndex(); typename ImageType::SizeType oImageSize = image->GetLargestPossibleRegion().GetSize(); typename ImageType::PointType newOrigin = image->GetOrigin(); typename OutputImageType::Pointer outImage = castFilter->GetOutput(); if (fSplineDistance > 0) { /*** 5 ******************* Compute number of control points **************/ ABORT_CHECKING(m_bAborting); itk::SizeValueType lowerBound[3]; itk::SizeValueType upperBound[3]; for (unsigned int i = 0; i < 3; i++) { float domain = static_cast<float>(image->GetLargestPossibleRegion().GetSize()[i] - 1) * image->GetSpacing()[i]; unsigned int numberOfSpans = static_cast<unsigned int>(std::ceil(domain / fSplineDistance)); unsigned long extraPadding = static_cast<unsigned long>((numberOfSpans * fSplineDistance - domain) / image->GetSpacing()[i] + 0.5); lowerBound[i] = static_cast<unsigned long>(0.5 * extraPadding); upperBound[i] = extraPadding - lowerBound[i]; newOrigin[i] -= (static_cast<float>(lowerBound[i]) * image->GetSpacing()[i]); oNumberOfControlPointsArray[i] = numberOfSpans + filter->GetSplineOrder(); } updateProgression(fProgression); /*** 6 ******************* Padder ****************************************/ ABORT_CHECKING(m_bAborting); typename PadderType::Pointer imagePadder = PadderType::New(); m_filter = imagePadder; imagePadder->SetInput(castFilter->GetOutput()); imagePadder->SetPadLowerBound(lowerBound); imagePadder->SetPadUpperBound(upperBound); imagePadder->SetConstant(0); imagePadder->SetNumberOfThreads(uiThreadNb); imagePadder->Update(); updateProgression(fProgression); outImage = imagePadder->GetOutput(); /*** 7 ******************** Handle the mask image *************************/ ABORT_CHECKING(m_bAborting); typename MaskPadderType::Pointer maskPadder = MaskPadderType::New(); m_filter = maskPadder; maskPadder->SetInput(maskImage); maskPadder->SetPadLowerBound(lowerBound); maskPadder->SetPadUpperBound(upperBound); maskPadder->SetConstant(0); maskPadder->SetNumberOfThreads(uiThreadNb); maskPadder->Update(); updateProgression(fProgression); maskImage = maskPadder->GetOutput(); /*** 8 ******************** SetNumber Of Control Points *******************/ ABORT_CHECKING(m_bAborting); filter->SetNumberOfControlPoints(oNumberOfControlPointsArray); } else if (oInitialMeshResolutionVect.size() == 3) { /*** 9 ******************** SetNumber Of Control Points alternative *******/ ABORT_CHECKING(m_bAborting); for (unsigned i = 0; i < 3; i++) { oNumberOfControlPointsArray[i] = static_cast<unsigned int>(oInitialMeshResolutionVect[i]) + filter->GetSplineOrder(); } filter->SetNumberOfControlPoints(oNumberOfControlPointsArray); updateProgression(fProgression, 3); } else { fProgression = 0; updateProgression(fProgression); std::cout << "No BSpline distance and Mesh Resolution is ignored because not 3 dimensions" << std::endl; } /*** 10 ******************* Shrinker image ********************************/ ABORT_CHECKING(m_bAborting); typename ShrinkerType::Pointer imageShrinker = ShrinkerType::New(); m_filter = imageShrinker; imageShrinker->SetInput(outImage); /*** 11 ******************* Shrinker mask *********************************/ ABORT_CHECKING(m_bAborting); typename MaskShrinkerType::Pointer maskShrinker = MaskShrinkerType::New(); m_filter = maskShrinker; maskShrinker->SetInput(maskImage); /*** 12 ******************* Shrink mask and image *************************/ ABORT_CHECKING(m_bAborting); imageShrinker->SetShrinkFactors(uiShrinkFactors); maskShrinker->SetShrinkFactors(uiShrinkFactors); imageShrinker->SetNumberOfThreads(uiThreadNb); maskShrinker->SetNumberOfThreads(uiThreadNb); imageShrinker->Update(); updateProgression(fProgression); maskShrinker->Update(); updateProgression(fProgression); /*** 13 ******************* Filter setings ********************************/ ABORT_CHECKING(m_bAborting); filter->SetSplineOrder(uiSplineOrder); filter->SetWienerFilterNoise(fWienerFilterNoise); filter->SetBiasFieldFullWidthAtHalfMaximum(fbfFWHM); filter->SetConvergenceThreshold(fConvergenceThreshold); filter->SetInput(imageShrinker->GetOutput()); filter->SetMaskImage(maskShrinker->GetOutput()); /*** 14 ******************* Apply filter **********************************/ ABORT_CHECKING(m_bAborting); try { filter->SetNumberOfThreads(uiThreadNb); filter->Update(); updateProgression(fProgression, 5); } catch (itk::ExceptionObject & err) { std::cerr << "ExceptionObject caught !" << std::endl; std::cerr << err << std::endl; eRes = medAbstractJob::MED_JOB_EXIT_FAILURE; return eRes; } /** * Reconstruct the bias field at full image resolution. Divide * the original input image by the bias field to get the final * corrected image. */ ABORT_CHECKING(m_bAborting); typename BSplinerType::Pointer bspliner = BSplinerType::New(); m_filter = bspliner; bspliner->SetInput(filter->GetLogBiasFieldControlPointLattice()); bspliner->SetSplineOrder(filter->GetSplineOrder()); bspliner->SetSize(image->GetLargestPossibleRegion().GetSize()); bspliner->SetOrigin(newOrigin); bspliner->SetDirection(image->GetDirection()); bspliner->SetSpacing(image->GetSpacing()); bspliner->SetNumberOfThreads(uiThreadNb); bspliner->Update(); updateProgression(fProgression); /*********************** Logarithm phase ***************************/ ABORT_CHECKING(m_bAborting); typename OutputImageType::Pointer logField = OutputImageType::New(); logField->SetOrigin(image->GetOrigin()); logField->SetSpacing(image->GetSpacing()); logField->SetRegions(image->GetLargestPossibleRegion()); logField->SetDirection(image->GetDirection()); logField->Allocate(); itk::ImageRegionIterator<typename BiasFilter::ScalarImageType> IB(bspliner->GetOutput(), bspliner->GetOutput()->GetLargestPossibleRegion()); itk::ImageRegionIterator<OutputImageType> IF(logField, logField->GetLargestPossibleRegion()); for (IB.GoToBegin(), IF.GoToBegin(); !IB.IsAtEnd(); ++IB, ++IF) { IF.Set(IB.Get()[0]); } /*********************** Exponential phase *************************/ ABORT_CHECKING(m_bAborting); typename ExpFilterType::Pointer expFilter = ExpFilterType::New(); m_filter = expFilter; expFilter->SetInput(logField); expFilter->SetNumberOfThreads(uiThreadNb); expFilter->Update(); updateProgression(fProgression); /************************ Dividing phase ***************************/ ABORT_CHECKING(m_bAborting); typename DividerType::Pointer divider = DividerType::New(); m_filter = divider; divider->SetInput1(castFilter->GetOutput()); divider->SetInput2(expFilter->GetOutput()); divider->SetNumberOfThreads(uiThreadNb); divider->Update(); updateProgression(fProgression); /******************** Prepare cropping phase ***********************/ ABORT_CHECKING(m_bAborting); typename ImageType::RegionType inputRegion; inputRegion.SetIndex(oImageIndex); inputRegion.SetSize(oImageSize); /************************ Cropping phase ***************************/ ABORT_CHECKING(m_bAborting); typename CropperType::Pointer cropper = CropperType::New(); m_filter = cropper; cropper->SetInput(divider->GetOutput()); cropper->SetExtractionRegion(inputRegion); cropper->SetDirectionCollapseToSubmatrix(); cropper->SetNumberOfThreads(uiThreadNb); cropper->Update(); updateProgression(fProgression); /********************** Write output image *************************/ ABORT_CHECKING(m_bAborting); medAbstractImageData *out = qobject_cast<medAbstractImageData *>(medAbstractDataFactory::instance()->create("itkDataImageFloat3")); out->setData(cropper->GetOutput()); this->setOutput(out); m_filter = 0; return eRes; }