예제 #1
0
static mitk::Image::Pointer GenerateMaskImage(unsigned int dimX,
                                              unsigned int dimY,
                                              unsigned int dimZ,
                                              float spacingX = 1,
                                              float spacingY = 1,
                                              float spacingZ = 1)
{
  typedef itk::Image< TPixelType, 3 > ImageType;
  typename ImageType::RegionType imageRegion;
  imageRegion.SetSize(0, dimX);
  imageRegion.SetSize(1, dimY);
  imageRegion.SetSize(2, dimZ);
  typename ImageType::SpacingType spacing;
  spacing[0] = spacingX;
  spacing[1] = spacingY;
  spacing[2] = spacingZ;

  mitk::Point3D                       origin; origin.Fill(0.0);
  itk::Matrix<double, 3, 3>           directionMatrix; directionMatrix.SetIdentity();

  typename ImageType::Pointer image = ImageType::New();
  image->SetSpacing( spacing );
  image->SetOrigin( origin );
  image->SetDirection( directionMatrix );
  image->SetLargestPossibleRegion( imageRegion );
  image->SetBufferedRegion( imageRegion );
  image->SetRequestedRegion( imageRegion );
  image->Allocate();
  image->FillBuffer(1);

  mitk::Image::Pointer mitkImage = mitk::Image::New();
  mitkImage->InitializeByItk( image.GetPointer() );
  mitkImage->SetVolume( image->GetBufferPointer() );
  return mitkImage;
}
예제 #2
0
typename PatchExtractor<PValue>::ImageType::Pointer 
PatchExtractor<PValue>::ExtractPatch()
{
	ContIndexType startIndex;
	for(unsigned int i = 0; i < 2; i++)
	{
		startIndex[i] = m_Center[i] - (m_Size[i] / 2.0);
	}
	startIndex[2] = 0;

	PointType newOrigin;
	m_Image->TransformContinuousIndexToPhysicalPoint(startIndex, newOrigin);


	typename ImageType::Pointer patch = ImageType::New();
	patch->SetDirection(m_Image->GetDirection());
	patch->SetOrigin(newOrigin);
	patch->SetSpacing(m_Image->GetSpacing());
	
	typename ImageType::RegionType region;
	m_Size[2] = 1;
	region.SetSize(m_Size);
	
	patch->SetRegions(region);
	patch->Allocate();

	
	typedef itk::NearestNeighborInterpolateImageFunction<ImageType, double> InterpolatorType;
	typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
	interpolator->SetInputImage(m_Image);


	itk::ImageRegionIterator<ImageType> imageIt(patch, region);
	while(!imageIt.IsAtEnd())
	{
		typename ImageType::IndexType index = imageIt.GetIndex();
		PointType point;
		patch->TransformIndexToPhysicalPoint(index, point);

		if(interpolator->IsInsideBuffer(point))
			imageIt.Set(interpolator->Evaluate(point));
		else
			imageIt.Set(0);
		++imageIt;
	}

	return patch;
	
}
예제 #3
0
static mitk::Image::Pointer GenerateGradientWithDimXImage(unsigned int dimX,
                                                          unsigned int dimY,
                                                          unsigned int dimZ,
                                                          float spacingX = 1,
                                                          float spacingY = 1,
                                                          float spacingZ = 1)
{
  typedef itk::Image< TPixelType, 3 > ImageType;
  typename ImageType::RegionType imageRegion;
  imageRegion.SetSize(0, dimX);
  imageRegion.SetSize(1, dimY);
  imageRegion.SetSize(2, dimZ);
  typename ImageType::SpacingType spacing;
  spacing[0] = spacingX;
  spacing[1] = spacingY;
  spacing[2] = spacingZ;

  mitk::Point3D                       origin; origin.Fill(0.0);
  itk::Matrix<double, 3, 3>           directionMatrix; directionMatrix.SetIdentity();

  typename ImageType::Pointer image = ImageType::New();
  image->SetSpacing( spacing );
  image->SetOrigin( origin );
  image->SetDirection( directionMatrix );
  image->SetLargestPossibleRegion( imageRegion );
  image->SetBufferedRegion( imageRegion );
  image->SetRequestedRegion( imageRegion );
  image->Allocate();
  image->FillBuffer(0.0);

  typedef itk::ImageRegionIterator<ImageType>      IteratorOutputType;
  IteratorOutputType it(image, imageRegion);
  it.GoToBegin();

  TPixelType val = 0;
  while(!it.IsAtEnd())
  {
    it.Set(val % dimX);
    val++;
    ++it;
  }

  mitk::Image::Pointer mitkImage = mitk::Image::New();
  mitkImage->InitializeByItk( image.GetPointer() );
  mitkImage->SetVolume( image->GetBufferPointer() );
  return mitkImage;
}
예제 #4
0
void WorkbenchUtils::addPaddingItk(itk::Image <PixelType, ImageDimension> *itkImage, Axis axis, bool append,
                                   int numberOfSlices, float pixelValue, Image::Pointer outImage) {
    // pixel type is templated. The input field for the value is set to float, so the user might enter some invalid values for the image type at hand.
    // since all primitive built-in types have well defined casting behaviour between each other, we'll just do a typecast. we will clip the entered
    // value at PixelTypes min/max to prevent an overflow. The possible loss of precision is ignored.
    float lower = itk::NumericTraits<PixelType>::min();
    float upper = itk::NumericTraits<PixelType>::max();
    float clippedPixelValue = std::max(lower, std::min(pixelValue, upper));

    PixelType paddingPixelValue = (PixelType) clippedPixelValue;

    typedef itk::Image <PixelType, ImageDimension> ImageType;

    // gather all data
    typename ImageType::SizeType lowerBound;
    typename ImageType::SizeType upperBound;
    lowerBound.Fill(0);
    upperBound.Fill(0);

    unsigned int itkAxis = convertToItkAxis(axis);
    if (append) {
        upperBound[itkAxis] = numberOfSlices;
    } else {
        lowerBound[itkAxis] = numberOfSlices;
    }

    // setup the filter
    typedef itk::ConstantPadImageFilter <ImageType, ImageType> PadFilterType;
    typename PadFilterType::Pointer padFilter = PadFilterType::New();
    padFilter->SetInput(itkImage);
    padFilter->SetConstant(paddingPixelValue);
    padFilter->SetPadLowerBound(lowerBound);
    padFilter->SetPadUpperBound(upperBound);
    padFilter->UpdateLargestPossibleRegion();

    // Update the origin, since padding creates negative index that is lost when returned to MITK
    typename ImageType::Pointer paddedImage = padFilter->GetOutput();
    typename ImageType::RegionType paddedImageRegion = paddedImage->GetLargestPossibleRegion();
    typename ImageType::PointType origin;
    paddedImage->TransformIndexToPhysicalPoint(paddedImageRegion.GetIndex(), origin);
    paddedImage->SetOrigin(origin);

    // get the results and cast them back to mitk. return via out parameter.
    outImage->InitializeByItk(paddedImage.GetPointer());
    CastToMitkImage(paddedImage, outImage);
}
void invcondemonsforces(int nlhs,
                 mxArray *plhs[],
                 int nrhs,
                 const mxArray *prhs[])
{
   typedef float PixelType;
   typedef itk::Image< PixelType, Dimension >           ImageType;
   
   typedef float                                        VectorComponentType;
   typedef itk::Vector<VectorComponentType, Dimension>  VectorPixelType;
   typedef itk::Image<VectorPixelType, Dimension>       DeformationFieldType;

   typedef itk::ESMInvConDemonsRegistrationFunction
      <ImageType,ImageType,DeformationFieldType>        DemonsRegistrationFunctionType;

   
   //boost::timer timer;


   // Allocate images and deformation field
   typename ImageType::Pointer fixedimage
      = ImageType::New();
   typename ImageType::Pointer movingimage
      = ImageType::New();
   typename ImageType::Pointer fw_weightimage
      = ImageType::New();  
   typename DeformationFieldType::Pointer field
      = DeformationFieldType::New();
   
   typename ImageType::Pointer jacobianimage
      = ImageType::New();  
      
      
   typename DeformationFieldType::Pointer inv_field
      = DeformationFieldType::New();
      
   typename DeformationFieldType::Pointer update
      = DeformationFieldType::New();
   
   typename DeformationFieldType::SpacingType spacing;
   spacing.Fill( 1.0 );
   
   typename DeformationFieldType::PointType origin;
   origin.Fill( 0.0 );
   
   typename DeformationFieldType::RegionType     region;
   typename DeformationFieldType::SizeType       size;
   typename DeformationFieldType::IndexType      start;

   unsigned int numPix(1u);
   const MatlabPixelType * fixinptr =  static_cast<const MatlabPixelType *>(mxGetData(prhs[0]));
   const MatlabPixelType * movinptr =  static_cast<const MatlabPixelType *>(mxGetData(prhs[1]));
   const MatlabPixelType * fieldinptrs[Dimension];
   const MatlabPixelType * inv_fieldinptrs[Dimension];
   
   
   mwSize matlabdims[Dimension];
   for (unsigned int d=0; d<Dimension; d++)
   {
      matlabdims[d]= mxGetDimensions(prhs[0])[d];
      size[d] = matlabdims[d];
      start[d] = 0;
      numPix *= size[d];

      fieldinptrs[d] = static_cast<const MatlabPixelType *>(mxGetData(prhs[2+d]));
      inv_fieldinptrs[d] = static_cast<const MatlabPixelType *>(mxGetData(prhs[2+Dimension+d]));
   }
   
   const MatlabPixelType * jacobianptr = static_cast<const MatlabPixelType *> (mxGetData(prhs[2*Dimension + 2]));
   const MatlabPixelType * fw_weightptr = static_cast<const MatlabPixelType *> (mxGetData(prhs[2*Dimension + 3]));
   
   const double RegWeight = static_cast<double>( mxGetPr(prhs[2*Dimension+4])[0] );
   
   const unsigned int UseJacFlag = 1;
   
   //std::cout << "RegWeight : " << RegWeight << std::endl;
   
   region.SetSize( size );
   region.SetIndex( start );

   fixedimage->SetOrigin( origin );
   fixedimage->SetSpacing( spacing );
   fixedimage->SetRegions( region );
   fixedimage->Allocate();

   movingimage->SetOrigin( origin );
   movingimage->SetSpacing( spacing );
   movingimage->SetRegions( region );
   movingimage->Allocate();
   
   fw_weightimage->SetOrigin( origin );
   fw_weightimage->SetSpacing( spacing );
   fw_weightimage->SetRegions( region );
   fw_weightimage->Allocate();
   
   
   field->SetOrigin( origin );
   field->SetSpacing( spacing );
   field->SetRegions( region );
   field->Allocate();
   
   inv_field->SetOrigin( origin );
   inv_field->SetSpacing( spacing );
   inv_field->SetRegions( region );
   inv_field->Allocate();

   update->SetOrigin( origin );
   update->SetSpacing( spacing );
   update->SetRegions( region );
   update->Allocate();
   if (UseJacFlag > 0)
   {
        jacobianimage->SetOrigin( origin );
        jacobianimage->SetSpacing( spacing );
        jacobianimage->SetRegions( region );
        jacobianimage->Allocate();
   }
   

   //mexPrintf("done Allocate(); %f sec\n", timer.elapsed());
   //timer.restart();

   
   PixelType * fixptr = fixedimage->GetBufferPointer();
   const PixelType * const fixbuff_end = fixptr + numPix;
   PixelType * movptr = movingimage->GetBufferPointer();
   PixelType * fwweightptr = NULL;
   PixelType * jacptr = NULL;
   if (UseJacFlag > 0)
   {
       jacptr = jacobianimage->GetBufferPointer();
   }
   
   
   
   fwweightptr = fw_weightimage->GetBufferPointer();
   
   VectorPixelType * fieldptr = field->GetBufferPointer();
   VectorPixelType * inv_fieldptr = inv_field->GetBufferPointer();
   
   while ( fixptr != fixbuff_end )
   {
      *fixptr++ = *fixinptr++;
      *movptr++ = *movinptr++;
      *fwweightptr++ = *fw_weightptr++; 
      
      for (unsigned int d=0; d<Dimension; d++)
      {
         (*fieldptr)[d] = *(fieldinptrs[d])++;
      }
      if (UseJacFlag > 0)
      {
        *jacptr++ = *jacobianptr++; 
      }
      
      ++fieldptr;
      
      for (unsigned int d=0; d<Dimension; d++)
      {
         (*inv_fieldptr)[d] = *(inv_fieldinptrs[d])++;
      }
      ++inv_fieldptr;
   }

   // Create demons function
   typename DemonsRegistrationFunctionType::Pointer drfp
      = DemonsRegistrationFunctionType::New();

   //mexPrintf("step size: %f\n",mxGetPr(prhs[2*Dimension+2])[0]);
   //drfp->SetMaximumUpdateStepLength( mxGetPr(prhs[2*Dimension+2])[0] );

   typename DemonsRegistrationFunctionType::GradientType gtype = DemonsRegistrationFunctionType::Symmetric;
   drfp->SetUseGradientType( gtype );

   drfp->SetDeformationField( field );
   drfp->SetInvDeformationField( inv_field);
   drfp->SetFixedImage( fixedimage );
   drfp->SetMovingImage( movingimage );
   
   drfp->SetRegWeight(RegWeight);
   
   drfp->SetUseFwWeight(true);
   drfp->SetFwWeightImage(fw_weightimage);
   
   if (UseJacFlag > 0)
   {
       drfp->SetUseJacobian(true);
       drfp->SetJacobianDetImage(jacobianimage);
   }
   else
   {
       drfp->SetUseJacobian(false);
   }
   
   drfp->InitializeIteration();

   //mexPrintf("done demons function init %f sec\n", timer.elapsed());
   //timer.restart();

   const itk::Size<Dimension> radius = drfp->GetRadius();

   // Break the input into a series of regions.  The first region is free
   // of boundary conditions, the rest with boundary conditions.  We operate
   // on the output region because input has been copied to output.
   typedef itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator
      <DeformationFieldType> FaceCalculatorType;
   typedef typename FaceCalculatorType::FaceListType FaceListType;
   typedef typename DemonsRegistrationFunctionType::NeighborhoodType
      NeighborhoodIteratorType;
   typedef itk::ImageRegionIterator<DeformationFieldType> UpdateIteratorType;

   FaceCalculatorType faceCalculator;
   
   FaceListType faceList = faceCalculator(field, region, radius);
   typename FaceListType::iterator fIt = faceList.begin();
   
   // Ask the function object for a pointer to a data structure it
   // will use to manage any global values it needs.  We'll pass this
   // back to the function object at each calculation and then
   // again so that the function object can use it to determine a
   // time step for this iteration.
   void * globalData = drfp->GetGlobalDataPointer();
   
   // Process the non-boundary region.
   NeighborhoodIteratorType nD(radius, field, *fIt);
   UpdateIteratorType       nU(update,  *fIt);
   nD.GoToBegin();
   while( !nD.IsAtEnd() )
   {
      nU.Value() = drfp->ComputeUpdate(nD, globalData);
      ++nD;
      ++nU;
   }

   // Process each of the boundary faces.
   
   NeighborhoodIteratorType bD;
   UpdateIteratorType   bU;
   for (++fIt; fIt != faceList.end(); ++fIt)
   {
      bD = NeighborhoodIteratorType(radius, field, *fIt);
      bU = UpdateIteratorType(update, *fIt);
      
      bD.GoToBegin();
      bU.GoToBegin();
      while ( !bD.IsAtEnd() )
      {
         bU.Value() = drfp->ComputeUpdate(bD, globalData);
         ++bD;
         ++bU;
      }
   }

   // Ask the finite difference function to compute the time step for
   // this iteration.  We give it the global data pointer to use, then
   // ask it to free the global data memory.
   //timeStep = df->ComputeGlobalTimeStep(globalData);
   drfp->ReleaseGlobalDataPointer(globalData);


   //mexPrintf("done actual computations %f sec\n", timer.elapsed());
   //timer.restart();


   // Allocate outputs
   const mxClassID classID = mxGetClassID(prhs[0]);
   MatlabPixelType * outptrs[Dimension];
   for (unsigned int d=0; d<Dimension; d++)
   {
      plhs[d] = mxCreateNumericArray(
         Dimension, matlabdims, classID, mxREAL);

      outptrs[d] = static_cast<MatlabPixelType *>(mxGetData(plhs[d]));
   }

   
   //mexPrintf("done allocate outputs %f sec\n", timer.elapsed());
   //timer.restart();
   

   // put result into outputs
   const VectorPixelType * upptr = update->GetBufferPointer();
   const VectorPixelType * const upbuff_end = upptr + numPix;
   
   while ( upptr != upbuff_end )
   {
      for (unsigned int d=0; d<Dimension; d++)
      {
         *(outptrs[d])++ = (*upptr)[d];
      }
      ++upptr;
   }

   //mexPrintf("done outputs copy %f sec\n", timer.elapsed());
}