int testRepresenterForVectorImage(const std::string& datadir) {

    typedef itk::StandardImageRepresenter<itk::Vector<float, 2>, 2> RepresenterType;
    typedef GenericRepresenterTest<RepresenterType> RepresenterTestType;

    const std::string referenceFilename = datadir + "/hand_dfs/df-hand-1.vtk";
    const std::string testDatasetFilename = datadir + "/hand_dfs/df-hand-2.vtk";

    RepresenterType::Pointer representer = RepresenterType::New();
    VectorImageType::Pointer reference = loadVectorImage(referenceFilename);
    representer->SetReference(reference);

    // choose a test dataset, a point and its associate pixel value

    VectorImageType::Pointer testDataset = loadVectorImage(testDatasetFilename);
    VectorImageType::IndexType idx;
    idx.Fill(0);
    VectorImageType::PointType testPt;
    reference->TransformIndexToPhysicalPoint(idx, testPt);
    VectorImageType::PixelType testValue = testDataset->GetPixel(idx);

    RepresenterTestType representerTest(representer, testDataset, std::make_pair(testPt, testValue));

    return (representerTest.runAllTests() == true);
}
VectorImageType::Pointer loadVectorImage(const std::string& filename) {
    itk::ImageFileReader<VectorImageType>::Pointer reader = itk::ImageFileReader<VectorImageType>::New();
    reader->SetFileName(filename);
    reader->Update();
    VectorImageType::Pointer img = reader->GetOutput();
    img->DisconnectPipeline();
    return img;
}
void TestExtractChannels()
{
  typedef itk::VectorImage<float, 2> VectorImageType;
  VectorImageType::Pointer image = VectorImageType::New();

  itk::Index<2> corner = {{0,0}};
  itk::Size<2> size = {{100,100}};
  itk::ImageRegion<2> region(corner, size);

  image->SetRegions(region);
  image->SetNumberOfComponentsPerPixel(3);
  image->Allocate();

  // Extract the first two channels
  std::vector<unsigned int> channels;
  channels.push_back(0);
  channels.push_back(1);

  typedef itk::VectorImage<float, 2> FloatScalarImageType;
  FloatScalarImageType::Pointer floatScalarImage = FloatScalarImageType::New();
  ITKHelpers::ExtractChannels(image.GetPointer(), channels, floatScalarImage.GetPointer());

  typedef itk::VectorImage<unsigned char, 2> UnsignedCharScalarImageType;
  UnsignedCharScalarImageType::Pointer unsignedCharScalarImage = UnsignedCharScalarImageType::New();
  ITKHelpers::ExtractChannels(image.GetPointer(), channels, unsignedCharScalarImage.GetPointer());
}
bool TestCreateLuminanceImage()
{
  // From RGB image
  {
  itk::Index<2> imageCorner = {{0,0}};
  itk::Size<2> imageSize = {{100,100}};
  itk::ImageRegion<2> imageRegion(imageCorner, imageSize);

  typedef itk::Image<itk::RGBPixel<unsigned char>, 2> RGBImageType;
  RGBImageType::Pointer rgbImage = RGBImageType::New();
  rgbImage->SetRegions(imageRegion);
  rgbImage->Allocate();

  typedef itk::Image<float, 2> LuminanceImageType;
  LuminanceImageType::Pointer luminanceImage = LuminanceImageType::New();

  ITKHelpers::CreateLuminanceImage(rgbImage.GetPointer(), luminanceImage.GetPointer());
  }

  // From Vector image
  {
  itk::Index<2> imageCorner = {{0,0}};
  itk::Size<2> imageSize = {{100,100}};
  itk::ImageRegion<2> imageRegion(imageCorner, imageSize);

  typedef itk::Image<itk::CovariantVector<unsigned char, 3>, 2> VectorImageType;
  VectorImageType::Pointer vectorImage = VectorImageType::New();
  vectorImage->SetRegions(imageRegion);
  vectorImage->Allocate();

  typedef itk::Image<float, 2> LuminanceImageType;
  LuminanceImageType::Pointer luminanceImage = LuminanceImageType::New();

  ITKHelpers::CreateLuminanceImage(vectorImage.GetPointer(), luminanceImage.GetPointer());
  }

  return true;
}
void TestExtractChannel()
{
  typedef itk::VectorImage<float, 2> VectorImageType;
  VectorImageType::Pointer image = VectorImageType::New();

  itk::Index<2> corner = {{0,0}};
  itk::Size<2> size = {{100,100}};
  itk::ImageRegion<2> region(corner, size);

  image->SetRegions(region);
  image->SetNumberOfComponentsPerPixel(2);
  image->Allocate();

  typedef itk::Image<float, 2> FloatScalarImageType;
  FloatScalarImageType::Pointer floatScalarImage = FloatScalarImageType::New();
  ITKHelpers::ExtractChannel(image.GetPointer(), 0, floatScalarImage.GetPointer());

  typedef itk::Image<unsigned char, 2> UnsignedCharScalarImageType;
  UnsignedCharScalarImageType::Pointer unsignedCharScalarImage = UnsignedCharScalarImageType::New();
  ITKHelpers::ExtractChannel(image.GetPointer(), 0, unsignedCharScalarImage.GetPointer());
}
void otb::Wrapper::ObjectsRadiometricStatistics::DoExecute()
{
	VectorImageType::Pointer referenceImage = GetParameterImage("im");

	otb::ogr::DataSource::Pointer ogrDS;
	ogrDS = otb::ogr::DataSource::New(GetParameterString("in"), otb::ogr::DataSource::Modes::Update_LayerUpdate);

	m_OGRDataSourceRendering = OGRDataSourceToMapFilterType::New();
	m_OGRDataSourceRendering->AddOGRDataSource(ogrDS);

	m_OGRDataSourceRendering->SetOutputSize(referenceImage->GetLargestPossibleRegion().GetSize());
	m_OGRDataSourceRendering->SetOutputOrigin(referenceImage->GetOrigin());

	m_OGRDataSourceRendering->SetOutputSpacing(referenceImage->GetSpacing());

	m_OGRDataSourceRendering->SetOutputProjectionRef(referenceImage->GetProjectionRef());

	//	m_OGRDataSourceRendering->SetOutputParametersFromImage(referenceImage); // A tester

	m_OGRDataSourceRendering->SetBackgroundValue(GetParameterInt("background"));
	m_OGRDataSourceRendering->SetBurnAttributeMode(true);
	m_OGRDataSourceRendering->SetBurnAttribute(GetParameterString("field"));

	// Write label image from OGR
	/*
	   WriterType::Pointer writer = WriterType::New(); 
	   writer->SetInput(m_OGRDataSourceRendering->GetOutput());	
	   writer->SetFileName("label_image.tif");
	   writer->Update();
	 */

	// Label image from OGR to statistics label map
	ConverterStatisticsType::Pointer converterStats = ConverterStatisticsType::New(); 
	converterStats->SetInput(m_OGRDataSourceRendering->GetOutput()); 
	converterStats->SetBackgroundValue(GetParameterInt("background")); 
	converterStats->Update();

	// Prepare channel extraction

	ExtractROIFilterType::Pointer m_ExtractROIFilter = ExtractROIFilterType::New();
	m_ExtractROIFilter->SetInput(referenceImage);

	// Update dataset with new fields

	otb::ogr::Layer layer = ogrDS->GetLayerChecked(0);

	OGRFieldDefn fieldNbPixels("NbPixels", OFTInteger);
	layer.CreateField(fieldNbPixels, true);

	OGRFieldDefn fieldFlatness("Flat", OFTReal);
	layer.CreateField(fieldFlatness, true);

	OGRFieldDefn fieldRoundness("Round", OFTReal);
	layer.CreateField(fieldRoundness, true);

	OGRFieldDefn fieldElongation("Elong", OFTReal);
	layer.CreateField(fieldElongation, true);

	OGRFieldDefn fieldPerimeter("Perim", OFTReal);
	layer.CreateField(fieldPerimeter, true);

	for(unsigned int i = 0; i < referenceImage->GetNumberOfComponentsPerPixel(); i++)
	{
		std::ostringstream fieldoss;
		fieldoss<<"meanB"<<i+1;
		OGRFieldDefn field(fieldoss.str().c_str(), OFTReal);
		layer.CreateField(field, true);
	}

	for(unsigned int i = 0; i < referenceImage->GetNumberOfComponentsPerPixel(); i++)
	{
		std::ostringstream fieldoss;
		fieldoss<<"stdB"<<i+1;
		OGRFieldDefn field(fieldoss.str().c_str(), OFTReal);
		layer.CreateField(field, true);
	}

	for(unsigned int i = 0; i < referenceImage->GetNumberOfComponentsPerPixel(); i++)
	{
		std::ostringstream fieldoss;
		fieldoss<<"MedB"<<i+1;
		OGRFieldDefn field(fieldoss.str().c_str(), OFTReal);
		layer.CreateField(field, true);
	}

	for(unsigned int i = 0; i < referenceImage->GetNumberOfComponentsPerPixel(); i++)
	{
		std::ostringstream fieldoss;
		fieldoss<<"VarB"<<i+1;
		OGRFieldDefn field(fieldoss.str().c_str(), OFTReal);
		layer.CreateField(field, true);
	}

	for(unsigned int i = 0; i < referenceImage->GetNumberOfComponentsPerPixel(); i++)
	{
		std::ostringstream fieldoss;
		fieldoss<<"KurtB"<<i+1;
		OGRFieldDefn field(fieldoss.str().c_str(), OFTReal);
		layer.CreateField(field, true);
	}

	for(unsigned int i = 0; i < referenceImage->GetNumberOfComponentsPerPixel(); i++)
	{
		std::ostringstream fieldoss;
		fieldoss<<"SkewB"<<i+1;
		OGRFieldDefn field(fieldoss.str().c_str(), OFTReal);
		layer.CreateField(field, true);
	}

	for(unsigned int i = 0; i < referenceImage->GetNumberOfComponentsPerPixel(); i++)
	{
		// Channel selection

		m_ExtractROIFilter->SetChannel(i + 1);

		// Statistics computation
		StatisticsFilterType::Pointer statistics = StatisticsFilterType::New(); 
		statistics->SetInput(converterStats->GetOutput());
		statistics->SetFeatureImage(m_ExtractROIFilter->GetOutput());
		statistics->SetComputePerimeter(true);
		statistics->Update();

		// Write shape attributes only one time
		if(i==0)
		{
			for(otb::ogr::Layer::iterator featIt = layer.begin(); featIt!=layer.end(); ++featIt)
			{
				otb::ogr::Feature m_Feature = *featIt;

				unsigned int label = m_Feature.ogr().GetFieldAsInteger(layer.GetLayerDefn().GetFieldIndex(GetParameterString("field").c_str()));

				if(statistics->GetOutput()->HasLabel(label))
				{
					const StatisticsLabelObjectType *labelObjectStats = statistics->GetOutput()->GetLabelObject(label);

					m_Feature.ogr().SetField("NbPixels", (int)labelObjectStats->GetNumberOfPixels());
					m_Feature.ogr().SetField("Flat", labelObjectStats->GetFlatness());
					m_Feature.ogr().SetField("Round", labelObjectStats->GetRoundness());
					m_Feature.ogr().SetField("Elong", labelObjectStats->GetElongation());
					m_Feature.ogr().SetField("Perim", labelObjectStats->GetPerimeter());

					layer.SetFeature(m_Feature);
				}
			}	
		}

		// Features iteration
		for(otb::ogr::Layer::iterator featIt = layer.begin(); featIt!=layer.end(); ++featIt)
		{
			otb::ogr::Feature m_Feature = *featIt;

			unsigned int label = m_Feature.ogr().GetFieldAsInteger(layer.GetLayerDefn().GetFieldIndex(GetParameterString("field").c_str()));

			if(statistics->GetOutput()->HasLabel(label))
			{
				const StatisticsLabelObjectType *labelObjectStats = statistics->GetOutput()->GetLabelObject(label);
				std::ostringstream fieldoss;

				fieldoss<<"meanB"<<i+1;
				m_Feature.ogr().SetField(fieldoss.str().c_str(),labelObjectStats->GetMean());
				fieldoss.str("");	

				fieldoss<<"stdB"<<i+1;
				m_Feature.ogr().SetField(fieldoss.str().c_str(),labelObjectStats->GetStandardDeviation());
				fieldoss.str("");

				fieldoss<<"medB"<<i+1;
				m_Feature.ogr().SetField(fieldoss.str().c_str(),labelObjectStats->GetMedian());
				fieldoss.str("");	

				fieldoss<<"varB"<<i+1;
				m_Feature.ogr().SetField(fieldoss.str().c_str(),labelObjectStats->GetVariance());
				fieldoss.str("");	

				fieldoss<<"kurtB"<<i+1;
				m_Feature.ogr().SetField(fieldoss.str().c_str(),labelObjectStats->GetKurtosis());
				fieldoss.str("");	

				fieldoss<<"skewB"<<i+1;
				m_Feature.ogr().SetField(fieldoss.str().c_str(),labelObjectStats->GetSkewness());
				fieldoss.str("");	
			}
			else
			{
				otbAppLogINFO( << "Object number " << label << " is skipped. This could happen during the rasterisation process when an object is smaller than 1 pixel.");
			}
		}
	}
}
void QmitkBasicImageProcessing::StartButtonClicked()
{
  if(!m_SelectedImageNode->GetNode()) return;

  this->BusyCursorOn();

  mitk::Image::Pointer newImage;

  try
  {
    newImage = dynamic_cast<mitk::Image*>(m_SelectedImageNode->GetNode()->GetData());
  }
  catch ( std::exception &e )
  {
  QString exceptionString = "An error occured during image loading:\n";
  exceptionString.append( e.what() );
    QMessageBox::warning( NULL, "Basic Image Processing", exceptionString , QMessageBox::Ok, QMessageBox::NoButton );
    this->BusyCursorOff();
    return;
  }

  // check if input image is valid, casting does not throw exception when casting from 'NULL-Object'
  if ( (! newImage) || (newImage->IsInitialized() == false) )
  {
    this->BusyCursorOff();

    QMessageBox::warning( NULL, "Basic Image Processing", "Input image is broken or not initialized. Returning.", QMessageBox::Ok, QMessageBox::NoButton );
    return;
  }

  // check if operation is done on 4D a image time step
  if(newImage->GetDimension() > 3)
  {
    mitk::ImageTimeSelector::Pointer timeSelector = mitk::ImageTimeSelector::New();
    timeSelector->SetInput(newImage);
    timeSelector->SetTimeNr( ((QmitkSliderNavigatorWidget*)m_Controls->sliceNavigatorTime)->GetPos() );
    timeSelector->Update();
    newImage = timeSelector->GetOutput();
  }



  // check if image or vector image
  ImageType::Pointer itkImage = ImageType::New();
  VectorImageType::Pointer itkVecImage = VectorImageType::New();

  int isVectorImage = newImage->GetPixelType().GetNumberOfComponents();

  if(isVectorImage > 1)
  {
    CastToItkImage( newImage, itkVecImage );
  }
  else
  {
    CastToItkImage( newImage, itkImage );
  }

  std::stringstream nameAddition("");

  int param1 = m_Controls->sbParam1->value();
  int param2 = m_Controls->sbParam2->value();
  double dparam1 = m_Controls->dsbParam1->value();
  double dparam2 = m_Controls->dsbParam2->value();
  double dparam3 = m_Controls->dsbParam3->value();

  try{

  switch (m_SelectedAction)
  {

  case GAUSSIAN:
    {
      GaussianFilterType::Pointer gaussianFilter = GaussianFilterType::New();
      gaussianFilter->SetInput( itkImage );
      gaussianFilter->SetVariance( param1 );
      gaussianFilter->UpdateLargestPossibleRegion();
      newImage = mitk::ImportItkImage(gaussianFilter->GetOutput())->Clone();
      nameAddition << "_Gaussian_var_" << param1;
      std::cout << "Gaussian filtering successful." << std::endl;
      break;
    }

  case MEDIAN:
    {
      MedianFilterType::Pointer medianFilter = MedianFilterType::New();
      MedianFilterType::InputSizeType size;
      size.Fill(param1);
      medianFilter->SetRadius( size );
      medianFilter->SetInput(itkImage);
      medianFilter->UpdateLargestPossibleRegion();
      newImage = mitk::ImportItkImage(medianFilter->GetOutput())->Clone();
      nameAddition << "_Median_radius_" << param1;
      std::cout << "Median Filtering successful." << std::endl;
      break;
    }

  case TOTALVARIATION:
    {
      if(isVectorImage > 1)
      {
        VectorTotalVariationFilterType::Pointer TVFilter
          = VectorTotalVariationFilterType::New();
        TVFilter->SetInput( itkVecImage.GetPointer() );
        TVFilter->SetNumberIterations(param1);
        TVFilter->SetLambda(double(param2)/1000.);
        TVFilter->UpdateLargestPossibleRegion();

        newImage = mitk::ImportItkImage(TVFilter->GetOutput())->Clone();
      }
      else
      {
        ImagePTypeToFloatPTypeCasterType::Pointer floatCaster = ImagePTypeToFloatPTypeCasterType::New();
        floatCaster->SetInput( itkImage );
        floatCaster->Update();
        FloatImageType::Pointer fImage = floatCaster->GetOutput();

        TotalVariationFilterType::Pointer TVFilter
          = TotalVariationFilterType::New();
        TVFilter->SetInput( fImage.GetPointer() );
        TVFilter->SetNumberIterations(param1);
        TVFilter->SetLambda(double(param2)/1000.);
        TVFilter->UpdateLargestPossibleRegion();

        newImage = mitk::ImportItkImage(TVFilter->GetOutput())->Clone();
      }

      nameAddition << "_TV_Iter_" << param1 << "_L_" << param2;
      std::cout << "Total Variation Filtering successful." << std::endl;
      break;
    }

  case DILATION:
    {
      BallType binaryBall;
      binaryBall.SetRadius( param1 );
      binaryBall.CreateStructuringElement();

      DilationFilterType::Pointer dilationFilter = DilationFilterType::New();
      dilationFilter->SetInput( itkImage );
      dilationFilter->SetKernel( binaryBall );
      dilationFilter->UpdateLargestPossibleRegion();
      newImage = mitk::ImportItkImage(dilationFilter->GetOutput())->Clone();
      nameAddition << "_Dilated_by_" << param1;
      std::cout << "Dilation successful." << std::endl;
      break;
    }

  case EROSION:
    {
      BallType binaryBall;
      binaryBall.SetRadius( param1 );
      binaryBall.CreateStructuringElement();

      ErosionFilterType::Pointer erosionFilter = ErosionFilterType::New();
      erosionFilter->SetInput( itkImage );
      erosionFilter->SetKernel( binaryBall );
      erosionFilter->UpdateLargestPossibleRegion();
      newImage = mitk::ImportItkImage(erosionFilter->GetOutput())->Clone();
      nameAddition << "_Eroded_by_" << param1;
      std::cout << "Erosion successful." << std::endl;
      break;
    }

  case OPENING:
    {
      BallType binaryBall;
      binaryBall.SetRadius( param1 );
      binaryBall.CreateStructuringElement();

      OpeningFilterType::Pointer openFilter = OpeningFilterType::New();
      openFilter->SetInput( itkImage );
      openFilter->SetKernel( binaryBall );
      openFilter->UpdateLargestPossibleRegion();
      newImage = mitk::ImportItkImage(openFilter->GetOutput())->Clone();
      nameAddition << "_Opened_by_" << param1;
      std::cout << "Opening successful." << std::endl;
      break;
    }

  case CLOSING:
    {
      BallType binaryBall;
      binaryBall.SetRadius( param1 );
      binaryBall.CreateStructuringElement();

      ClosingFilterType::Pointer closeFilter = ClosingFilterType::New();
      closeFilter->SetInput( itkImage );
      closeFilter->SetKernel( binaryBall );
      closeFilter->UpdateLargestPossibleRegion();
      newImage = mitk::ImportItkImage(closeFilter->GetOutput())->Clone();
      nameAddition << "_Closed_by_" << param1;
      std::cout << "Closing successful." << std::endl;
      break;
    }

  case GRADIENT:
    {
      GradientFilterType::Pointer gradientFilter = GradientFilterType::New();
      gradientFilter->SetInput( itkImage );
      gradientFilter->SetSigma( param1 );
      gradientFilter->UpdateLargestPossibleRegion();
      newImage = mitk::ImportItkImage(gradientFilter->GetOutput())->Clone();
      nameAddition << "_Gradient_sigma_" << param1;
      std::cout << "Gradient calculation successful." << std::endl;
      break;
    }

  case LAPLACIAN:
    {
      // the laplace filter requires a float type image as input, we need to cast the itkImage
      // to correct type
      ImagePTypeToFloatPTypeCasterType::Pointer caster = ImagePTypeToFloatPTypeCasterType::New();
      caster->SetInput( itkImage );
      caster->Update();
      FloatImageType::Pointer fImage = caster->GetOutput();

      LaplacianFilterType::Pointer laplacianFilter = LaplacianFilterType::New();
      laplacianFilter->SetInput( fImage );
      laplacianFilter->UpdateLargestPossibleRegion();
      newImage = mitk::ImportItkImage(laplacianFilter->GetOutput())->Clone();
      nameAddition << "_Second_Derivative";
      std::cout << "Laplacian filtering successful." << std::endl;
      break;
    }

  case SOBEL:
    {
      // the sobel filter requires a float type image as input, we need to cast the itkImage
      // to correct type
      ImagePTypeToFloatPTypeCasterType::Pointer caster = ImagePTypeToFloatPTypeCasterType::New();
      caster->SetInput( itkImage );
      caster->Update();
      FloatImageType::Pointer fImage = caster->GetOutput();

      SobelFilterType::Pointer sobelFilter = SobelFilterType::New();
      sobelFilter->SetInput( fImage );
      sobelFilter->UpdateLargestPossibleRegion();
      newImage = mitk::ImportItkImage(sobelFilter->GetOutput())->Clone();
      nameAddition << "_Sobel";
      std::cout << "Edge Detection successful." << std::endl;
      break;
    }

  case THRESHOLD:
    {
      ThresholdFilterType::Pointer thFilter = ThresholdFilterType::New();
      thFilter->SetLowerThreshold(param1 < param2 ? param1 : param2);
      thFilter->SetUpperThreshold(param2 > param1 ? param2 : param1);
      thFilter->SetInsideValue(1);
      thFilter->SetOutsideValue(0);
      thFilter->SetInput(itkImage);
      thFilter->UpdateLargestPossibleRegion();
      newImage = mitk::ImportItkImage(thFilter->GetOutput())->Clone();
      nameAddition << "_Threshold";
      std::cout << "Thresholding successful." << std::endl;
      break;
    }

  case INVERSION:
    {
      InversionFilterType::Pointer invFilter = InversionFilterType::New();
      mitk::ScalarType min = newImage->GetScalarValueMin();
      mitk::ScalarType max = newImage->GetScalarValueMax();
      invFilter->SetMaximum( max + min );
      invFilter->SetInput(itkImage);
      invFilter->UpdateLargestPossibleRegion();
      newImage = mitk::ImportItkImage(invFilter->GetOutput())->Clone();
      nameAddition << "_Inverted";
      std::cout << "Image inversion successful." << std::endl;
      break;
    }

  case DOWNSAMPLING:
    {
      ResampleImageFilterType::Pointer downsampler = ResampleImageFilterType::New();
      downsampler->SetInput( itkImage );

      NearestInterpolatorType::Pointer interpolator = NearestInterpolatorType::New();
      downsampler->SetInterpolator( interpolator );

      downsampler->SetDefaultPixelValue( 0 );

      ResampleImageFilterType::SpacingType spacing = itkImage->GetSpacing();
      spacing *= (double) param1;
      downsampler->SetOutputSpacing( spacing );

      downsampler->SetOutputOrigin( itkImage->GetOrigin() );
      downsampler->SetOutputDirection( itkImage->GetDirection() );

      ResampleImageFilterType::SizeType size = itkImage->GetLargestPossibleRegion().GetSize();
      for ( int i = 0; i < 3; ++i )
      {
        size[i] /= param1;
      }
      downsampler->SetSize( size );
      downsampler->UpdateLargestPossibleRegion();

      newImage = mitk::ImportItkImage(downsampler->GetOutput())->Clone();
      nameAddition << "_Downsampled_by_" << param1;
      std::cout << "Downsampling successful." << std::endl;
      break;
    }

  case FLIPPING:
    {
      FlipImageFilterType::Pointer flipper = FlipImageFilterType::New();
      flipper->SetInput( itkImage );
      itk::FixedArray<bool, 3> flipAxes;
      for(int i=0; i<3; ++i)
      {
        if(i == param1)
        {
          flipAxes[i] = true;
        }
        else
        {
          flipAxes[i] = false;
        }
      }
      flipper->SetFlipAxes(flipAxes);
      flipper->UpdateLargestPossibleRegion();
      newImage = mitk::ImportItkImage(flipper->GetOutput())->Clone();
      std::cout << "Image flipping successful." << std::endl;
      break;
    }

  case RESAMPLING:
    {
      std::string selectedInterpolator;
      ResampleImageFilterType::Pointer resampler = ResampleImageFilterType::New();
      switch (m_SelectedInterpolation)
      {
      case LINEAR:
        {
          LinearInterpolatorType::Pointer interpolator = LinearInterpolatorType::New();
          resampler->SetInterpolator(interpolator);
          selectedInterpolator = "Linear";
          break;
        }
      case NEAREST:
        {
          NearestInterpolatorType::Pointer interpolator = NearestInterpolatorType::New();
          resampler->SetInterpolator(interpolator);
          selectedInterpolator = "Nearest";
          break;
        }
      default:
        {
          LinearInterpolatorType::Pointer interpolator = LinearInterpolatorType::New();
          resampler->SetInterpolator(interpolator);
          selectedInterpolator = "Linear";
          break;
        }
      }
      resampler->SetInput( itkImage );
      resampler->SetOutputOrigin( itkImage->GetOrigin() );

      ImageType::SizeType input_size = itkImage->GetLargestPossibleRegion().GetSize();
      ImageType::SpacingType input_spacing = itkImage->GetSpacing();

      ImageType::SizeType output_size;
      ImageType::SpacingType output_spacing;

      output_size[0] = input_size[0] * (input_spacing[0] / dparam1);
      output_size[1] = input_size[1] * (input_spacing[1] / dparam2);
      output_size[2] = input_size[2] * (input_spacing[2] / dparam3);
      output_spacing [0] = dparam1;
      output_spacing [1] = dparam2;
      output_spacing [2] = dparam3;

      resampler->SetSize( output_size );
      resampler->SetOutputSpacing( output_spacing );
      resampler->SetOutputDirection( itkImage->GetDirection() );

      resampler->UpdateLargestPossibleRegion();

      ImageType::Pointer resampledImage = resampler->GetOutput();

      newImage = mitk::ImportItkImage( resampledImage );
      nameAddition << "_Resampled_" << selectedInterpolator;
      std::cout << "Resampling successful." << std::endl;
      break;
    }


  case RESCALE:
    {
      FloatImageType::Pointer floatImage = FloatImageType::New();
      CastToItkImage( newImage, floatImage );
      itk::RescaleIntensityImageFilter<FloatImageType,FloatImageType>::Pointer filter = itk::RescaleIntensityImageFilter<FloatImageType,FloatImageType>::New();
      filter->SetInput(0, floatImage);
      filter->SetOutputMinimum(dparam1);
      filter->SetOutputMaximum(dparam2);
      filter->Update();
      floatImage = filter->GetOutput();

      newImage = mitk::Image::New();
      newImage->InitializeByItk(floatImage.GetPointer());
      newImage->SetVolume(floatImage->GetBufferPointer());
      nameAddition << "_Rescaled";
      std::cout << "Rescaling successful." << std::endl;

      break;
    }

  default:
    this->BusyCursorOff();
    return;
  }
  }
  catch (...)
  {
    this->BusyCursorOff();
    QMessageBox::warning(NULL, "Warning", "Problem when applying filter operation. Check your input...");
    return;
  }

  newImage->DisconnectPipeline();

  // adjust level/window to new image
  mitk::LevelWindow levelwindow;
  levelwindow.SetAuto( newImage );
  mitk::LevelWindowProperty::Pointer levWinProp = mitk::LevelWindowProperty::New();
  levWinProp->SetLevelWindow( levelwindow );

  // compose new image name
  std::string name = m_SelectedImageNode->GetNode()->GetName();
  if (name.find(".pic.gz") == name.size() -7 )
  {
    name = name.substr(0,name.size() -7);
  }
  name.append( nameAddition.str() );

  // create final result MITK data storage node
  mitk::DataNode::Pointer result = mitk::DataNode::New();
  result->SetProperty( "levelwindow", levWinProp );
  result->SetProperty( "name", mitk::StringProperty::New( name.c_str() ) );
  result->SetData( newImage );

  // for vector images, a different mapper is needed
  if(isVectorImage > 1)
  {
    mitk::VectorImageMapper2D::Pointer mapper =
      mitk::VectorImageMapper2D::New();
    result->SetMapper(1,mapper);
  }

  // reset GUI to ease further processing
//  this->ResetOneImageOpPanel();

  // add new image to data storage and set as active to ease further processing
  GetDefaultDataStorage()->Add( result, m_SelectedImageNode->GetNode() );
  if ( m_Controls->cbHideOrig->isChecked() == true )
    m_SelectedImageNode->GetNode()->SetProperty( "visible", mitk::BoolProperty::New(false) );
  // TODO!! m_Controls->m_ImageSelector1->SetSelectedNode(result);

  // show the results
  mitk::RenderingManager::GetInstance()->RequestUpdateAll();
  this->BusyCursorOff();
}
void mitk::DiffusionImageNrrdWriterService::Write()
{
  mitk::Image::ConstPointer input = dynamic_cast<const mitk::Image *>(this->GetInput());

  VectorImageType::Pointer itkImg;
  mitk::CastToItkImage(input,itkImg);

  if (input.IsNull())
  {
    MITK_ERROR <<"Sorry, input to DiffusionImageNrrdWriterService is NULL!";
    return;
  }
  if ( this->GetOutputLocation().empty() )
  {
    MITK_ERROR << "Sorry, filename has not been set!";
    return ;
  }
  mitk::LocaleSwitch localeSwitch("C");

  char keybuffer[512];
  char valbuffer[512];

  //itk::MetaDataDictionary dic = input->GetImage()->GetMetaDataDictionary();

  vnl_matrix_fixed<double,3,3> measurementFrame = mitk::DiffusionPropertyHelper::GetMeasurementFrame(input);
  if (measurementFrame(0,0) || measurementFrame(0,1) || measurementFrame(0,2) ||
    measurementFrame(1,0) || measurementFrame(1,1) || measurementFrame(1,2) ||
    measurementFrame(2,0) || measurementFrame(2,1) || measurementFrame(2,2))
  {
    sprintf( valbuffer, " (%lf,%lf,%lf) (%lf,%lf,%lf) (%lf,%lf,%lf)", measurementFrame(0,0), measurementFrame(0,1), measurementFrame(0,2), measurementFrame(1,0), measurementFrame(1,1), measurementFrame(1,2), measurementFrame(2,0), measurementFrame(2,1), measurementFrame(2,2));
    itk::EncapsulateMetaData<std::string>(itkImg->GetMetaDataDictionary(),std::string("measurement frame"),std::string(valbuffer));
  }

  sprintf( valbuffer, "DWMRI");
  itk::EncapsulateMetaData<std::string>(itkImg->GetMetaDataDictionary(),std::string("modality"),std::string(valbuffer));

  if(mitk::DiffusionPropertyHelper::GetGradientContainer(input)->Size())
  {
    sprintf( valbuffer, "%1f", mitk::DiffusionPropertyHelper::GetReferenceBValue(input) );
    itk::EncapsulateMetaData<std::string>(itkImg->GetMetaDataDictionary(),std::string("DWMRI_b-value"),std::string(valbuffer));
  }

  for(unsigned int i=0; i<mitk::DiffusionPropertyHelper::GetGradientContainer(input)->Size(); i++)
  {
    sprintf( keybuffer, "DWMRI_gradient_%04d", i );

    /*if(itk::ExposeMetaData<std::string>(input->GetMetaDataDictionary(),
    std::string(keybuffer),tmp))
    continue;*/

    sprintf( valbuffer, "%1f %1f %1f", mitk::DiffusionPropertyHelper::GetGradientContainer(input)->ElementAt(i).get(0),
      mitk::DiffusionPropertyHelper::GetGradientContainer(input)->ElementAt(i).get(1), mitk::DiffusionPropertyHelper::GetGradientContainer(input)->ElementAt(i).get(2));

    itk::EncapsulateMetaData<std::string>(itkImg->GetMetaDataDictionary(),std::string(keybuffer),std::string(valbuffer));
  }

  typedef itk::VectorImage<short,3> ImageType;

  std::string ext = this->GetMimeType()->GetExtension(this->GetOutputLocation());
  ext = itksys::SystemTools::LowerCase(ext);

  // default extension is .dwi
  if( ext == "")
  {
    ext = ".nrrd";
    this->SetOutputLocation(this->GetOutputLocation() + ext);
  }

  if (ext == ".hdwi" || ext == ".nrrd" || ext == ".dwi")
  {

    MITK_INFO << "Extension " << ext;
    itk::NrrdImageIO::Pointer io = itk::NrrdImageIO::New();
    //io->SetNrrdVectorType( nrrdKindList );
    io->SetFileType( itk::ImageIOBase::Binary );
    io->UseCompressionOn();

    typedef itk::ImageFileWriter<ImageType> WriterType;
    WriterType::Pointer nrrdWriter = WriterType::New();
    nrrdWriter->UseInputMetaDataDictionaryOn();
    nrrdWriter->SetInput( itkImg );
    nrrdWriter->SetImageIO(io);
    nrrdWriter->SetFileName(this->GetOutputLocation());
    nrrdWriter->UseCompressionOn();
    nrrdWriter->SetImageIO(io);
    try
    {
      nrrdWriter->Update();
    }
    catch (itk::ExceptionObject e)
    {
      std::cout << e << std::endl;
      throw;
    }

  }
}
Exemple #9
0
  mitk::TbssImage::Pointer mitk::TbssImporter::ImportMeta()
  {
    mitk::TbssImage::Pointer tbssImg = mitk::TbssImage::New();

    m_Data = DataImageType::New();

    std::vector< std::pair<mitk::TbssImage::MetaDataFunction, int> > metaInfo;

    // Gradient images are vector images, so they will add more dimensions to the vector
    int vecLength = m_MetaFiles.size();
    //Check if there is a gradient image

    for(int i=0; i < m_MetaFiles.size(); i++)
    {
      std::pair<std::string, std::string> p = m_MetaFiles.at(i);
      if(RetrieveTbssFunction(p.first) == mitk::TbssImage::GRADIENT_X)
      {
        vecLength += 2;
      }
    }


    int currIndex = 0;


    for(int i=0; i < m_MetaFiles.size(); i++)
    {
      std::pair<std::string, std::string> p = m_MetaFiles.at(i);
      std::string function = p.first;
      std::string file = p.second;

      // Add to metainfo to give the tbss image a function-index pair
      std::pair<mitk::TbssImage::MetaDataFunction, int> pair;


      pair.first = RetrieveTbssFunction(function);
      pair.second = i;

      if(pair.first == mitk::TbssImage::GRADIENT_X)
      {
        metaInfo.push_back(std::pair<mitk::TbssImage::MetaDataFunction, int>(mitk::TbssImage::GRADIENT_X, i));
        metaInfo.push_back(std::pair<mitk::TbssImage::MetaDataFunction, int>(mitk::TbssImage::GRADIENT_Y, i+1));
        metaInfo.push_back(std::pair<mitk::TbssImage::MetaDataFunction, int>(mitk::TbssImage::GRADIENT_Z, i+2));


        VectorReaderType::Pointer fileReader = VectorReaderType::New();
        fileReader->SetFileName(file);
        itk::NrrdImageIO::Pointer io = itk::NrrdImageIO::New();
        fileReader->SetImageIO(io);
        fileReader->Update();

        VectorImageType::Pointer img = fileReader->GetOutput();

        VectorImageType::SizeType size = img->GetLargestPossibleRegion().GetSize();

        if(i==0)
        {
          // First image in serie. Properties should be used to initialize m_Data
          m_Data->SetRegions(img->GetLargestPossibleRegion().GetSize());
          m_Data->SetSpacing(img->GetSpacing());
          m_Data->SetOrigin(img->GetOrigin());
          m_Data->SetDirection(img->GetDirection());
          m_Data->SetVectorLength(vecLength);
          m_Data->Allocate();
        }


        /* Dealing with a gradient image, so the size of the vector need to be increased by 2
            since this image contains 3 volumes. Old data should not be deleted*/



        for(int x=0; x<size[0]; x++)
        {
          for(int y=0; y<size[1]; y++)
          {
            for(int z=0; z<size[2]; z++)
            {
              itk::Index<3> ix;
              ix[0] = x;
              ix[1] = y;
              ix[2] = z;

              itk::VariableLengthVector<int> vec = img->GetPixel(ix);
              itk::VariableLengthVector<float> pixel = m_Data->GetPixel(ix);
              for(int j=0; j<vec.Size(); j++)
              {
                int pos = currIndex+j;
                float f = vec.GetElement(j);
                pixel.SetElement(pos, f);

              }
              m_Data->SetPixel(ix, pixel);
            }
          }
        }

        currIndex += img->GetVectorLength();
        tbssImg->SetContainsGradient(true);
        // Read vector image and add to m_Data
      }

      else {

        metaInfo.push_back(pair);
        FileReaderType3D::Pointer fileReader = FileReaderType3D::New();
        fileReader->SetFileName(file);
        fileReader->Update();

        FloatImage3DType::Pointer img = fileReader->GetOutput();

        FloatImage3DType::SizeType size = img->GetLargestPossibleRegion().GetSize();

        if(i==0)
        {
          // First image in serie. Properties should be used to initialize m_Data
          m_Data->SetRegions(img->GetLargestPossibleRegion().GetSize());
          m_Data->SetSpacing(img->GetSpacing());
          m_Data->SetOrigin(img->GetOrigin());
          m_Data->SetDirection(img->GetDirection());
          m_Data->SetVectorLength(vecLength);         
          m_Data->Allocate();
        }

        for(int x=0; x<size[0]; x++)
        {
          for(int y=0; y<size[1]; y++)
          {
            for(int z=0; z<size[2]; z++)
            {
              itk::Index<3> ix;
              ix[0] = x;
              ix[1] = y;
              ix[2] = z;

              float f = img->GetPixel(ix);
              itk::VariableLengthVector<float> pixel = m_Data->GetPixel(ix);
              pixel.SetElement(currIndex, f);
              m_Data->SetPixel(ix, pixel);

            }
          }
        }
      }

      if(pair.first == mitk::TbssImage::MEAN_FA_SKELETON)
      {
        tbssImg->SetContainsMeanSkeleton(true);
      }
      else if(pair.first == mitk::TbssImage::MEAN_FA_SKELETON_MASK)
      {
        tbssImg->SetContainsSkeletonMask(true);
      }
      else if(pair.first == mitk::TbssImage::DISTANCE_MAP)
      {
        tbssImg->SetContainsDistanceMap(true);
      }

      currIndex++;

    }

    tbssImg->SetIsMeta(true);
    tbssImg->SetImage(m_Data);
    tbssImg->SetMetaInfo(metaInfo);
    tbssImg->InitializeFromVectorImage();

    return tbssImg;
  }
bool itkDataTensorImageReaderBase::read (const QString &path)
{
    if (this->io.IsNull())
        return false;
	
    this->readInformation ( path );
	
    qDebug() << "Read with: " << this->identifier();

    if (medAbstractData *medData = dynamic_cast<medAbstractData*>(this->data()) ) {
      
        if (medData->identifier()=="itkDataTensorImageDouble3") {

	  if (this->io->GetNumberOfComponents()==6) {

	    typedef itk::Tensor<double, 3>    TensorType;
	    typedef itk::Image<TensorType, 3> TensorImageType;

	    typedef itk::Vector<double, 6>    VectorType;
	    typedef itk::Image<VectorType, 3> VectorImageType;

	    typedef itk::ImageFileReader<VectorImageType> ReaderType;
	    
	    VectorImageType::Pointer image = 0;
	    {
	      ReaderType::Pointer reader = ReaderType::New();
	      reader->SetImageIO (this->io);
	      reader->SetFileName ( path.toAscii().constData() );
	      try {
		reader->Update();
	      }
	      catch (itk::ExceptionObject &e) {
		qDebug() << e.GetDescription();
		return false;
	      }
	      image = reader->GetOutput(); 
	    }

	    TensorImageType::Pointer tensors = TensorImageType::New();
	    TensorImageType::RegionType region = image->GetLargestPossibleRegion();
	    tensors->SetRegions   (region);
	    tensors->SetSpacing   (image->GetSpacing());
	    tensors->SetOrigin    (image->GetOrigin());
	    tensors->SetDirection (image->GetDirection());

	    try {
	        tensors->Allocate();
	    }
	    catch (itk::ExceptionObject &e) {
	        qDebug() << e.GetDescription();
		return false;
	    }

	    itk::ImageRegionConstIteratorWithIndex<VectorImageType>  itIn (image,
									   image->GetLargestPossibleRegion());
	    itk::ImageRegionIteratorWithIndex<TensorImageType> itOut(tensors,
								     tensors->GetLargestPossibleRegion());

	    while(!itOut.IsAtEnd()) {
      
	      VectorType vec = itIn.Get();
	      TensorType tensor;
	      
	      for( unsigned int j=0; j<6; j++) {
		tensor[j] = vec[j];
	      }
      
	      itOut.Set (tensor);
	      
	      ++itOut;
	      ++itIn;
	    }

	    medData->setData (tensors);
	    
	  }
	  else if (this->io->GetNumberOfComponents()==9) {

	    typedef itk::Tensor<double, 3>    TensorType;
	    typedef itk::Image<TensorType, 3> TensorImageType;

	    typedef itk::Vector<double, 9>    VectorType;
	    typedef itk::Image<VectorType, 3> VectorImageType;

	    typedef itk::ImageFileReader<VectorImageType> ReaderType;
	    
	    VectorImageType::Pointer image = 0;
	    {
	      ReaderType::Pointer reader = ReaderType::New();
	      reader->SetImageIO (this->io);
	      reader->SetFileName ( path.toAscii().constData() );
	      try {
		reader->Update();
	      }
	      catch (itk::ExceptionObject &e) {
		qDebug() << e.GetDescription();
		return false;
	      }
	      image = reader->GetOutput(); 
	    }

	    TensorImageType::Pointer tensors = TensorImageType::New();
	    TensorImageType::RegionType region = image->GetLargestPossibleRegion();
	    tensors->SetRegions   (region);
	    tensors->SetSpacing   (image->GetSpacing());
	    tensors->SetOrigin    (image->GetOrigin());
	    tensors->SetDirection (image->GetDirection());

	    try {
	        tensors->Allocate();
	    }
	    catch (itk::ExceptionObject &e) {
	        qDebug() << e.GetDescription();
		return false;
	    }

	    itk::ImageRegionConstIteratorWithIndex<VectorImageType>  itIn (image,
									   image->GetLargestPossibleRegion());
	    itk::ImageRegionIteratorWithIndex<TensorImageType> itOut(tensors,
								     tensors->GetLargestPossibleRegion());

	    while(!itOut.IsAtEnd()) {
      
	      VectorType vec = itIn.Get();
	      TensorType tensor;
	      
	      for (unsigned int i=0; i<3; i++)
		for (unsigned int j=0; j<3; j++)
		  tensor.SetComponent (i, j, vec[i*3+j]);
      
	      itOut.Set (tensor);
	      
	      ++itOut;
	      ++itIn;
	    }

	    medData->setData (tensors);
	  }
	  else {
	      qDebug() << "Unsupported number of components";
	      return false;
	  } 
	}

	else if (medData->identifier()=="itkDataTensorImageFloat3") {

	  if (this->io->GetNumberOfComponents()==6) {

	    typedef itk::Tensor<float, 3>     TensorType;
	    typedef itk::Image<TensorType, 3> TensorImageType;

	    typedef itk::Vector<float, 6>     VectorType;
	    typedef itk::Image<VectorType, 3> VectorImageType;

	    typedef itk::ImageFileReader<VectorImageType> ReaderType;
	    
	    VectorImageType::Pointer image = 0;
	    {
	      ReaderType::Pointer reader = ReaderType::New();
	      reader->SetImageIO (this->io);
	      reader->SetFileName ( path.toAscii().constData() );
	      try {
		reader->Update();
	      }
	      catch (itk::ExceptionObject &e) {
		qDebug() << e.GetDescription();
		return false;
	      }
	      image = reader->GetOutput(); 
	    }

	    TensorImageType::Pointer tensors = TensorImageType::New();
	    TensorImageType::RegionType region = image->GetLargestPossibleRegion();
	    tensors->SetRegions   (region);
	    tensors->SetSpacing   (image->GetSpacing());
	    tensors->SetOrigin    (image->GetOrigin());
	    tensors->SetDirection (image->GetDirection());

	    try {
	        tensors->Allocate();
	    }
	    catch (itk::ExceptionObject &e) {
	        qDebug() << e.GetDescription();
		return false;
	    }

	    itk::ImageRegionConstIteratorWithIndex<VectorImageType>  itIn (image,
									   image->GetLargestPossibleRegion());
	    itk::ImageRegionIteratorWithIndex<TensorImageType> itOut(tensors,
								     tensors->GetLargestPossibleRegion());

	    while(!itOut.IsAtEnd()) {
      
	      VectorType vec = itIn.Get();
	      TensorType tensor;
	      
	      for( unsigned int j=0; j<6; j++) {
		tensor[j] = vec[j];
	      }
      
	      itOut.Set (tensor);
	      
	      ++itOut;
	      ++itIn;
	    }
	    
	    medData->setData (tensors);
	  }
	  else if (this->io->GetNumberOfComponents()==9) {

	    typedef itk::Tensor<float, 3>     TensorType;
	    typedef itk::Image<TensorType, 3> TensorImageType;

	    typedef itk::Vector<float, 9>     VectorType;
	    typedef itk::Image<VectorType, 3> VectorImageType;

	    typedef itk::ImageFileReader<VectorImageType> ReaderType;
	    
	    VectorImageType::Pointer image = 0;
	    {
	      ReaderType::Pointer reader = ReaderType::New();
	      reader->SetImageIO (this->io);
	      reader->SetFileName ( path.toAscii().constData() );
	      try {
		reader->Update();
	      }
	      catch (itk::ExceptionObject &e) {
		qDebug() << e.GetDescription();
		return false;
	      }
	      image = reader->GetOutput(); 
	    }

	    TensorImageType::Pointer tensors = TensorImageType::New();
	    TensorImageType::RegionType region = image->GetLargestPossibleRegion();
	    tensors->SetRegions   (region);
	    tensors->SetSpacing   (image->GetSpacing());
	    tensors->SetOrigin    (image->GetOrigin());
	    tensors->SetDirection (image->GetDirection());

	    try {
	        tensors->Allocate();
	    }
	    catch (itk::ExceptionObject &e) {
	        qDebug() << e.GetDescription();
		return false;
	    }

	    itk::ImageRegionConstIteratorWithIndex<VectorImageType>  itIn (image,
									   image->GetLargestPossibleRegion());
	    itk::ImageRegionIteratorWithIndex<TensorImageType> itOut(tensors,
								     tensors->GetLargestPossibleRegion());

	    while(!itOut.IsAtEnd()) {
      
	      VectorType vec = itIn.Get();
	      TensorType tensor;
	      
	      for (unsigned int i=0; i<3; i++)
		for (unsigned int j=0; j<3; j++)
		  tensor.SetComponent (i, j, vec[i*3+j]);
      
	      itOut.Set (tensor);
	      
	      ++itOut;
	      ++itIn;
	    }

	    medData->setData (tensors);
	  }
	  else {
	      qDebug() << "Unsupported number of components";
	      return false;
	  } 
	}
	else {
	  qDebug() << "Unsupported data type";
	  return false;
	}
    }
    else {
      qDebug() << "No data set or could not create one";
      return false;
    }

    return true;
    
}
bool TestExtractChannel()
{
  // VectorImage
  {
  typedef itk::VectorImage<float, 2> VectorImageType;
  VectorImageType::Pointer image = VectorImageType::New();

  itk::Index<2> corner = {{0,0}};
  itk::Size<2> size = {{100,100}};
  itk::ImageRegion<2> region(corner, size);

  image->SetRegions(region);
  image->SetNumberOfComponentsPerPixel(2);
  image->Allocate();

  typedef itk::Image<float, 2> FloatScalarImageType;
  FloatScalarImageType::Pointer floatScalarImage = FloatScalarImageType::New();
  ITKHelpers::ExtractChannel(image.GetPointer(), 0, floatScalarImage.GetPointer());

  typedef itk::Image<unsigned char, 2> UnsignedCharScalarImageType;
  UnsignedCharScalarImageType::Pointer unsignedCharScalarImage = UnsignedCharScalarImageType::New();
  ITKHelpers::ExtractChannel(image.GetPointer(), 0, unsignedCharScalarImage.GetPointer());
  }

  // VectorImage different output type
  {
  typedef itk::VectorImage<float, 2> VectorImageType;
  VectorImageType::Pointer image = VectorImageType::New();

  itk::Index<2> corner = {{0,0}};
  itk::Size<2> size = {{100,100}};
  itk::ImageRegion<2> region(corner, size);

  image->SetRegions(region);
  image->SetNumberOfComponentsPerPixel(2);
  image->Allocate();

  typedef itk::Image<unsigned char, 2> UnsignedCharScalarImageType;
  UnsignedCharScalarImageType::Pointer unsignedCharScalarImage = UnsignedCharScalarImageType::New();
  ITKHelpers::ExtractChannel(image.GetPointer(), 0, unsignedCharScalarImage.GetPointer());
  }

  // Scalar Image
  {
  typedef itk::Image<float, 2> VectorImageType;
  VectorImageType::Pointer image = VectorImageType::New();

  itk::Index<2> corner = {{0,0}};
  itk::Size<2> size = {{100,100}};
  itk::ImageRegion<2> region(corner, size);

  image->SetRegions(region);
  image->Allocate();

  typedef itk::Image<float, 2> FloatScalarImageType;
  FloatScalarImageType::Pointer floatScalarImage = FloatScalarImageType::New();
  ITKHelpers::ExtractChannel(image.GetPointer(), 0, floatScalarImage.GetPointer());

  typedef itk::Image<unsigned char, 2> UnsignedCharScalarImageType;
  UnsignedCharScalarImageType::Pointer unsignedCharScalarImage = UnsignedCharScalarImageType::New();
  ITKHelpers::ExtractChannel(image.GetPointer(), 0, unsignedCharScalarImage.GetPointer());
  }

  // Image<CovariantVector>
  {
  typedef itk::Image<itk::CovariantVector<float, 3>, 2> VectorImageType;
  VectorImageType::Pointer image = VectorImageType::New();

  itk::Index<2> corner = {{0,0}};
  itk::Size<2> size = {{100,100}};
  itk::ImageRegion<2> region(corner, size);

  image->SetRegions(region);
  image->Allocate();

  typedef itk::Image<float, 2> FloatScalarImageType;
  FloatScalarImageType::Pointer floatScalarImage = FloatScalarImageType::New();
  ITKHelpers::ExtractChannel(image.GetPointer(), 0, floatScalarImage.GetPointer());
  }

  // Image<Vector>
  {
  typedef itk::Image<itk::Vector<float, 3>, 2> VectorImageType;
  VectorImageType::Pointer image = VectorImageType::New();

  itk::Index<2> corner = {{0,0}};
  itk::Size<2> size = {{100,100}};
  itk::ImageRegion<2> region(corner, size);

  image->SetRegions(region);
  image->Allocate();

  typedef itk::Image<float, 2> FloatScalarImageType;
  FloatScalarImageType::Pointer floatScalarImage = FloatScalarImageType::New();
  ITKHelpers::ExtractChannel(image.GetPointer(), 0, floatScalarImage.GetPointer());
  }

  return true;
}
QVector<double> PerfusionMapCalculatorThread::deconvolve(QVector<double> tissue)
{
    QVector<double> residuefunc(tissue.size());
    int i;
    //std::cout<<"?"<<std::endl;
    /*for(i=0;i<tissue.size();i++)
    {
        std::cout<<tissue[i]<<" "<<std::flush;
    }*/
    //std::cout<<"?"<<std::endl;
    typedef std::complex<double> complexd;
    complexd num1, num2, num3;

//     //Usant fftw
//     fftw_complex* in;
//     fftw_complex* out;
//
//     in = new fftw_complex[tissue.size()];
//     out = new fftw_complex[tissue.size()];
//
//     fftw_plan pf, pb;
//
//     pf = fftw_plan_dft_1d(tissue.size(), in, out, FFTW_FORWARD, FFTW_ESTIMATE);
//     pb = fftw_plan_dft_1d(tissue.size(), in, out, FFTW_BACKWARD, FFTW_ESTIMATE);
//
//     int i;
//     for(i=0;i<tissue.size();i++)
//     {
//         in[i][0]=tissue[i];
//         in[i][1]=0.0;
//     }
//
//     fftw_execute(pf);
//
//     for(i=0;i<tissue.size();i++)
//     {
//         num1=complexd(fftaifreal[i],fftaifimag[i]);
//         num2=complexd(out[i][0],out[i][1]);
//
//         if((reg_fact > 1e-6) || ((fabs(num1.real()) + fabs(num1.imag()))> 1e-6))
//         {
//             num3 = num2* (conj(num1) / (num1*conj(num1) + reg_fact*pow(-1,reg_exp)*pow(omega[i],2*reg_exp)));
//             in[i][0] = num3.real();
//             in[i][1] = num3.imag();
//         }
//         else
//         {
//             in[i][0] = 0.0;
//             in[i][1] = 0.0;
//         }
//     }
//     fftw_execute(pb);
//     for(i=0;i<tissue.size();i++)
//     {
//         residuefunc[i]=out[i][0]/tissue.size();
//     }
//     fftw_destroy_plan(pf);
//     fftw_destroy_plan(pb);
//     free(in);
//     free(out);

    //Usant itk's
    //std::cout<<"Usant itk's"<<std::endl;
    typedef itk::Image< double, 1 > VectorImageType;
    VectorImageType::RegionType region;
    VectorImageType::IndexType start;
    start[0]=0;
    VectorImageType::SizeType size;
    size[0] = m_sizet;  //les mostres temporals
    region.SetSize(size);
    region.SetIndex(start);
    //std::cout<<"&"<<std::endl;

    VectorImageType::Pointer tissueImage = VectorImageType::New();
    tissueImage->SetRegions(region);
    try
        {
        tissueImage->Allocate();
        }
    catch(itk::ExceptionObject & excp)
        {
        std::cerr << "Error: " << std::endl;
        std::cerr << excp << std::endl;
        return residuefunc;
        }
    //std::cout<<"$"<<std::endl;

    typedef itk::ImageRegionIterator<VectorImageType> VectorIteratorType;
    VectorIteratorType tissueIter(tissueImage, tissueImage->GetLargestPossibleRegion());
    //std::cout<<"@"<<tissueImage->GetLargestPossibleRegion().GetSize()[0]<<std::endl;

    typedef itk::VnlFFTRealToComplexConjugateImageFilter< double, 1 >  FFTFilterType;
    FFTFilterType::Pointer fftFilter = FFTFilterType::New();
    //std::cout<<"#"<<std::endl;

    tissueIter.GoToBegin();

    //std::cout<<"%"<<std::endl;
    for(i=0;i<tissue.size();i++)
    {
        tissueIter.Set(tissue[i]);
        ++tissueIter;
    }
    fftFilter->SetInput(tissueImage);

    try
        {
        fftFilter->Update();
        }
    catch(itk::ExceptionObject & excp)
        {
        std::cerr << "Error: " << std::endl;
        std::cerr << excp << std::endl;
        return residuefunc;
        }

    typedef FFTFilterType::OutputImageType ComplexImageType;
    ComplexImageType::Pointer residualFFTImage = ComplexImageType::New();
    residualFFTImage->SetRegions(region);
    residualFFTImage->Allocate();

    typedef itk::ImageRegionIterator<ComplexImageType> ComplexIteratorType;
    ComplexIteratorType fftTissueIter(fftFilter->GetOutput(), fftFilter->GetOutput()->GetLargestPossibleRegion());
    fftTissueIter.GoToBegin();

    ComplexIteratorType fftResidualIter(residualFFTImage, residualFFTImage->GetLargestPossibleRegion());
    fftResidualIter.GoToBegin();

    //std::cout<<"!"<<fftFilter->GetOutput()->GetLargestPossibleRegion().GetSize()[0]<<std::endl;
    for(i=0;i<tissue.size();i++)
    {
        num1=complexd(fftaifreal[i],fftaifimag[i]);
        num2=complexd(fftTissueIter.Get().real(),fftTissueIter.Get().imag());

        if((reg_fact > 1e-6) || ((fabs(num1.real()) + fabs(num1.imag()))> 1e-6))
        {
            num3 = num2* (conj(num1) / (num1*conj(num1) + reg_fact*pow(-1,reg_exp)*pow(omega[i],2*reg_exp)));
            fftResidualIter.Set(num3);
        }
        else
        {
            num3 = complexd(0.0, 0.0);
            fftResidualIter.Set(num3);
        }
        ++fftTissueIter;
        ++fftResidualIter;
    }

    typedef itk::VnlFFTComplexConjugateToRealImageFilter< double, 1 >  IFFTFilterType;
    IFFTFilterType::Pointer fftInverseFilter = IFFTFilterType::New();
    fftInverseFilter->SetInput(residualFFTImage);

    try
        {
        fftInverseFilter->Update();
        }
    catch(itk::ExceptionObject & excp)
        {
        std::cerr << "Error: " << std::endl;
        std::cerr << excp << std::endl;
        return residuefunc;
        }

    //std::cout<<"*"<<fftInverseFilter->GetOutput()->GetLargestPossibleRegion().GetSize()[0]<<std::endl;
    VectorIteratorType residualIter(fftInverseFilter->GetOutput(), fftInverseFilter->GetOutput()->GetLargestPossibleRegion());
    residualIter.GoToBegin();

    for(i=0;i<residuefunc.size();i++)
    {
        //if(residuefunc[i]!=residualIter.Get()) std::cout<<"Resultat residuefunc diferent: "<<residuefunc[i]<<" ," <<residualIter.Get()/tissue.size()<<std::endl;

        residuefunc[i]=residualIter.Get();
        ++residualIter;
    }
    for(i=0;i<residuefunc.size();i++)
    {
        //std::cout<<residuefunc[i]<<" "<<std::flush;
    }
    //std::cout<<"?"<<std::endl;
    return residuefunc;
}