void mitk::ToFImageDownsamplingFilter::ItkImageResampling( const itk::Image<TPixel,VImageDimension>* itkImage ) { // declare typdef for itk image from input mitk image typedef itk::Image< TPixel, VImageDimension > ItkImageType; //declare itk filter related typedefs (transform type, interpolater, and size type) typedef itk::ResampleImageFilter<ItkImageType,ItkImageType> ResamplerFilterType; typedef itk::IdentityTransform<double, VImageDimension> TransformType; typedef itk::NearestNeighborInterpolateImageFunction<ItkImageType, double > InterpolatorType; typedef typename ItkImageType::SizeType::SizeValueType SizeValueType; //instantiate filter related parameters typename ResamplerFilterType::Pointer resampler = ResamplerFilterType::New(); typename TransformType::Pointer transform = TransformType::New(); typename InterpolatorType::Pointer interpolator = InterpolatorType::New(); // establish size for downsampled image ( the result of this filter) typename ItkImageType::SizeType inputSize = itkImage->GetLargestPossibleRegion().GetSize(); typename ItkImageType::SizeType size; size[0] = static_cast< SizeValueType >( m_ResampledX ); size[1] = static_cast< SizeValueType >( m_ResampledY ); size[2] = static_cast< SizeValueType >( m_ResampledZ ); //establish spacing for new downsampled image ( resulting image) const typename ItkImageType::SpacingType& inputSpacing = itkImage->GetSpacing(); typename ItkImageType::SpacingType outputSpacing; outputSpacing[0] = inputSpacing[0] * ( inputSize[0]/ m_ResampledX ); outputSpacing[1] = inputSpacing[1] * ( inputSize[1]/ m_ResampledY ); outputSpacing[2] = inputSpacing[2] * ( inputSize[2]/ m_ResampledZ ); mitk::Vector3D mitkspacing; mitkspacing[0] = outputSpacing[0]; mitkspacing[1] = outputSpacing[1]; mitkspacing[2] = outputSpacing[2]; mitk::Point3D mitkorig; mitkorig[0] = itkImage->GetOrigin()[0]; mitkorig[1] = itkImage->GetOrigin()[1]; mitkorig[2] = 0.0; // set filter parameters and update transform->SetIdentity(); resampler->SetTransform(transform); resampler->SetInterpolator(interpolator); resampler->SetOutputSpacing(outputSpacing); resampler->SetOutputOrigin(itkImage->GetOrigin()); resampler->SetSize(size); resampler->SetInput(itkImage); resampler->UpdateLargestPossibleRegion(); // Create mitk container for resulting image mitk::Image::Pointer resultImage = ImageToImageFilter::GetOutput(); resultImage->SetSpacing(mitkspacing); resultImage->SetOrigin(mitkorig); // Cast itk image to mitk image mitk::CastToMitkImage(resampler->GetOutput(), resultImage); }
void WorkbenchUtils::resampleImageItk(itk::Image <PixelType, ImageDimension> *itkImage, Interpolator interpolType, unsigned int *newDimensions, Image::Pointer outImage) { typedef itk::Image <PixelType, ImageDimension> ImageType; // get original image informations const typename ImageType::RegionType &inputRegion = itkImage->GetLargestPossibleRegion(); const typename ImageType::SizeType &inputDimensions = inputRegion.GetSize(); const typename ImageType::SpacingType &inputSpacing = itkImage->GetSpacing(); // calculate spacing double outputSpacing[ImageDimension]; itk::Size <ImageDimension> outputSize; for (unsigned int i = 0; i < ImageDimension; ++i) { outputSpacing[i] = inputSpacing[i] * (double) inputDimensions[i] / newDimensions[i]; outputSize[i] = newDimensions[i]; } // transform typedef itk::IdentityTransform<double, ImageDimension> TransformType; typename TransformType::Pointer transform = TransformType::New(); transform->SetIdentity(); // interpolator typedefs typedef double CoordinateType; typedef itk::LinearInterpolateImageFunction <ImageType, CoordinateType> LinearInterpolatorType; typedef itk::NearestNeighborInterpolateImageFunction <ImageType, CoordinateType> NearestNeighborInterpolatorType; typedef itk::GaussianInterpolateImageFunction <ImageType, CoordinateType> GaussianInterpolatorType; typedef itk::BSplineInterpolateImageFunction <ImageType, CoordinateType> BSplineInterpolatorType; // set up the filter typedef itk::ResampleImageFilter <ImageType, ImageType> ResampleFilterType; typename ResampleFilterType::Pointer resampleFilter = ResampleFilterType::New(); resampleFilter->SetTransform(transform); resampleFilter->SetOutputOrigin(itkImage->GetOrigin()); resampleFilter->SetOutputSpacing(outputSpacing); resampleFilter->SetSize(outputSize); switch (interpolType) { case Interpolator::LINEAR: // the default; resampleFilter->SetInterpolator(LinearInterpolatorType::New()); break; case Interpolator::NEAREST_NEIGHBOR: resampleFilter->SetInterpolator(NearestNeighborInterpolatorType::New()); break; case Interpolator::GAUSSIAN: resampleFilter->SetInterpolator(GaussianInterpolatorType::New()); break; case Interpolator::BSPLINE: resampleFilter->SetInterpolator(BSplineInterpolatorType::New()); break; } resampleFilter->SetInput(itkImage); resampleFilter->UpdateLargestPossibleRegion(); // get the results and cast them back to mitk. return via out parameter. outImage->InitializeByItk(resampleFilter->GetOutput()); CastToMitkImage(resampleFilter->GetOutput(), outImage); }
itk::Object::Pointer QmitkTranslationTransformView::GetTransform2(itk::Image<TPixelType, VImageDimension>* /*itkImage1*/) { typedef typename itk::Image< TPixelType, VImageDimension > FixedImageType; typedef typename itk::Image< TPixelType, VImageDimension > MovingImageType; typedef itk::AffineTransform<double, VImageDimension> TransformType; typename TransformType::Pointer transform = TransformType::New(); transform->SetIdentity(); m_TransformObject = transform; return transform.GetPointer(); }
typename TImage::Pointer modelBasedImageToImageRegistration(std::string referenceFilename, std::string targetFilename, typename TStatisticalModelType::Pointer model, std::string outputDfFilename, unsigned numberOfIterations){ typedef itk::ImageFileReader<TImage> ImageReaderType; typedef itk::InterpolatingStatisticalDeformationModelTransform<TRepresenter, double, VImageDimension> TransformType; typedef itk::LBFGSOptimizer OptimizerType; typedef itk::ImageRegistrationMethod<TImage, TImage> RegistrationFilterType; typedef itk::WarpImageFilter< TImage, TImage, TVectorImage > WarperType; typedef itk::LinearInterpolateImageFunction< TImage, double > InterpolatorType; typename ImageReaderType::Pointer referenceReader = ImageReaderType::New(); referenceReader->SetFileName(referenceFilename.c_str()); referenceReader->Update(); typename TImage::Pointer referenceImage = referenceReader->GetOutput(); referenceImage->Update(); typename ImageReaderType::Pointer targetReader = ImageReaderType::New(); targetReader->SetFileName(targetFilename.c_str()); targetReader->Update(); typename TImage::Pointer targetImage = targetReader->GetOutput(); targetImage->Update(); // do the fitting typename TransformType::Pointer transform = TransformType::New(); transform->SetStatisticalModel(model); transform->SetIdentity(); // Setting up the fitting OptimizerType::Pointer optimizer = OptimizerType::New(); optimizer->MinimizeOn(); optimizer->SetMaximumNumberOfFunctionEvaluations(numberOfIterations); typedef IterationStatusObserver ObserverType; ObserverType::Pointer observer = ObserverType::New(); optimizer->AddObserver( itk::IterationEvent(), observer ); typename TMetricType::Pointer metric = TMetricType::New(); typename InterpolatorType::Pointer interpolator = InterpolatorType::New(); typename RegistrationFilterType::Pointer registration = RegistrationFilterType::New(); registration->SetInitialTransformParameters(transform->GetParameters()); registration->SetMetric(metric); registration->SetOptimizer( optimizer ); registration->SetTransform( transform ); registration->SetInterpolator( interpolator ); registration->SetFixedImage( targetImage ); registration->SetFixedImageRegion(targetImage->GetBufferedRegion() ); registration->SetMovingImage( referenceImage ); try { std::cout << "Performing registration... " << std::flush; registration->Update(); std::cout << "[done]" << std::endl; } catch ( itk::ExceptionObject& o ) { std::cout << "caught exception " << o << std::endl; } typename TVectorImage::Pointer df = model->DrawSample(transform->GetCoefficients()); // write deformation field if(outputDfFilename.size()>0){ typename itk::ImageFileWriter<TVectorImage>::Pointer df_writer = itk::ImageFileWriter<TVectorImage>::New(); df_writer->SetFileName(outputDfFilename); df_writer->SetInput(df); df_writer->Update(); } // warp reference std::cout << "Warping reference... " << std::flush; typename WarperType::Pointer warper = WarperType::New(); warper->SetInput(referenceImage ); warper->SetInterpolator( interpolator ); warper->SetOutputSpacing( targetImage->GetSpacing() ); warper->SetOutputOrigin( targetImage->GetOrigin() ); warper->SetOutputDirection( targetImage->GetDirection() ); warper->SetDisplacementField( df ); warper->Update(); std::cout << "[done]" << std::endl; return warper->GetOutput(); }