//! a helper function for image saving
template<class T> void save_image(typename T::Pointer image,const char *fname,const char* minc_datatype,itk::Object* metadata,const std::string& history)
{
  if(metadata) copy_metadata(image,metadata);
  if(!history.empty()) itk::append_minc_history(image,history);
  itk::set_minc_storage_type(image,minc_datatype);
  typename itk::ImageFileWriter< T >::Pointer writer = itk::ImageFileWriter<T>::New();
  writer->SetFileName(fname);
  writer->SetInput( image );
  writer->Update();
}
Beispiel #2
0
// a helper function for minc writing
template <class T> void save_minc(const char *file,typename T::ConstPointer img)
{
  typedef itk::MincImageIO ImageIOType;
  ImageIOType::Pointer minc2ImageIO = ImageIOType::New();
     
  typename itk::ImageFileWriter< T >::Pointer writer = itk::ImageFileWriter<T>::New();
  writer->SetFileName(file);
  writer->SetImageIO( minc2ImageIO );
  writer->SetInput( img );
  writer->Update();
}
void runImageToImageRegistration(std::string referenceFilename, 
									std::string referenceLandmarkFilename, 
									std::string targetFilename, 
									std::string targetLandmarkFilename, 
									double gaussianKernelSigma, 
									double gaussianKernelScale,
									double landmarkUncertainty,
									std::string similarityMetric, 
									std::string outputFilename, 
									std::string outputDfFilename,
									unsigned numberOfBasisFunctions,
									unsigned numberOfIterations){
	
	
	typedef itk::Image<TPixelType, VImageDimension> ImageType;
	typedef itk::Image< itk::Vector<float, VImageDimension> ,VImageDimension > VectorImageType;
	typedef itk::VectorImageRepresenter<float, VImageDimension, VImageDimension> TRepresenter;

	typedef itk::StatisticalModel<TRepresenter> StatisticalModelType;
	typedef itk::MeanSquaresImageToImageMetric<ImageType, ImageType> MeanSquaresMetricType;
	typedef itk::NormalizedCorrelationImageToImageMetric<ImageType, ImageType> NormalizedCorrelationMetricType;
	
	// build deformation model
	typename StatisticalModelType::Pointer model = buildLowRankGPModel<TRepresenter, VectorImageType, StatisticalModelType>(referenceFilename.c_str(), gaussianKernelSigma, gaussianKernelScale, numberOfBasisFunctions);
	if(referenceLandmarkFilename.size()>0 && targetLandmarkFilename.size()>0){
		model = constrainModel< TRepresenter, VectorImageType, StatisticalModelType, VImageDimension>(model, referenceLandmarkFilename, targetLandmarkFilename, landmarkUncertainty);
	}

	// image to image registration with this model
	typename ImageType::Pointer registeredImage;
	if(similarityMetric=="MeanSquares"){
		registeredImage =  modelBasedImageToImageRegistration<TRepresenter, ImageType, VectorImageType, StatisticalModelType, MeanSquaresMetricType, VImageDimension>(referenceFilename, targetFilename, model, outputDfFilename, numberOfIterations);
	}
	if(similarityMetric=="NormalizedCorrelation"){
		registeredImage =  modelBasedImageToImageRegistration<TRepresenter, ImageType, VectorImageType, StatisticalModelType, NormalizedCorrelationMetricType, VImageDimension>(referenceFilename, targetFilename, model, outputDfFilename, numberOfIterations);
	}

	// write registered image
	if(outputFilename.size()>0){
		typename itk::ImageFileWriter<ImageType>::Pointer writer = itk::ImageFileWriter<ImageType>::New();
		writer->SetFileName(outputFilename);
		writer->SetInput(registeredImage);
		writer->Update();
	}
}
Beispiel #4
0
bool itkDataImageWriterBase::write_image(const QString& path,const char* type) {
    medAbstractData* medData = dynamic_cast<medAbstractData*>(this->data());
    if (medData && medData->identifier()!=type)
        return false;

    typedef itk::Image<T,DIM> Image;
    typename Image::Pointer image = dynamic_cast<Image*>((itk::Object*)(this->data()->output()));
    if (image.IsNull())
        return false;
    if (medData->hasMetaData(medAbstractImageData::PixelMeaningMetaData)) {
        itk::MetaDataDictionary& dict = image->GetMetaDataDictionary();
        itk::EncapsulateMetaData(dict,"intent_name",medData->metadata(medAbstractImageData::PixelMeaningMetaData));
    }
    typename itk::ImageFileWriter<Image>::Pointer writer = itk::ImageFileWriter <Image>::New();
    writer->SetImageIO (this->io);
    writer->UseCompressionOn();
    writer->SetFileName(path.toLatin1().constData());
    writer->SetInput(image);
    writer->Update();

    return true;
}
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();
}