void SuperPixelSegmentationGUI::slot_IterationComplete(unsigned int numberOfSegments)
{
  std::stringstream ss;
  ss << "Computed " << numberOfSegments << " segments." << std::endl;
  this->statusBar()->showMessage(ss.str().c_str());
  
  typedef itk::Image<itk::RGBPixel<unsigned char>, 2> RGBImageType;
  RGBImageType::Pointer colorImage = RGBImageType::New();
  colorImage->SetRegions(this->LabelImage->GetLargestPossibleRegion());
  colorImage->Allocate();

  unsigned int maxLabel = Helpers::MaxValue<LabelImageType>(this->LabelImage);
  std::vector<RGBImageType::PixelType> segmentColors;
  for(unsigned int labelId = 0; labelId <= maxLabel; ++labelId)
    {
    //std::cout << "Coloring label " << labelId << std::endl;
    float rgb[3] = {0,0,0};
    itk::ImageRegionIterator<LabelImageType> labelIterator(this->LabelImage, this->LabelImage->GetLargestPossibleRegion());
    unsigned int counter = 0;
    while(!labelIterator.IsAtEnd())
      {
      if(labelIterator.Get() == labelId)
        {
        rgb[0] += this->Image->GetPixel(labelIterator.GetIndex())[0];
        rgb[1] += this->Image->GetPixel(labelIterator.GetIndex())[1];
        rgb[2] += this->Image->GetPixel(labelIterator.GetIndex())[2];
        counter++;
        }// end if
      ++labelIterator;
      } // end while
    RGBImageType::PixelType colorPixel;
    colorPixel[0] = rgb[0]/static_cast<float>(counter);
    colorPixel[1] = rgb[1]/static_cast<float>(counter);
    colorPixel[2] = rgb[2]/static_cast<float>(counter);
    segmentColors.push_back(colorPixel);
    } // end for
    
        
  itk::ImageRegionIterator<LabelImageType> colorIterator(this->LabelImage, this->LabelImage->GetLargestPossibleRegion());
  
  while(!colorIterator.IsAtEnd())
    {
    colorImage->SetPixel(colorIterator.GetIndex(), segmentColors[colorIterator.Get()]);

    ++colorIterator;
    } // end while

  QImage qimage = HelpersQt::GetQImageRGB<RGBImageType>(colorImage);
  if(this->LabelImagePixmapItem)
    {
    this->Scene->removeItem(this->LabelImagePixmapItem);
    }
  this->LabelImagePixmapItem = this->Scene->addPixmap(QPixmap::fromImage(qimage));

  Refresh();
}
void Initialisation::savePointAsAxialImage(ImageType::Pointer initialImage, string filename)
{
    if (points_.size() > 0)
    {
        double radius = 2.0;
        //if (initialRadius_/stretchingFactor_ > radius_) radius = radius_*stretchingFactor_; // initialRadius_ majored by radius_
        //else radius = initialRadius_;
        typedef itk::ImageDuplicator< ImageType > DuplicatorType3D;
        DuplicatorType3D::Pointer duplicator = DuplicatorType3D::New();
        duplicator->SetInputImage(initialImage);
        duplicator->Update();
        ImageType::Pointer clonedImage = duplicator->GetOutput();
        
        // Intensity normalization
        RescaleFilterType::Pointer rescaleFilter = RescaleFilterType::New();
        rescaleFilter->SetInput(clonedImage);
        rescaleFilter->SetOutputMinimum(0);
        rescaleFilter->SetOutputMaximum(255);
        try {
            rescaleFilter->Update();
        } catch( itk::ExceptionObject & e ) {
            cerr << "Exception caught while normalizing input image " << endl;
            cerr << e << endl;
        }
        clonedImage = rescaleFilter->GetOutput();
        
        typedef itk::RGBPixel<unsigned char> RGBPixelType;
        typedef itk::Image<RGBPixelType, 2> RGBImageType;
		typedef itk::ExtractImageFilter< ImageType, RGBImageType > ExtractorTypeRGB;

		PointType pt; pt[0] = initialPoint_[0]; pt[1] = initialPoint_[1]; pt[2] = initialPoint_[2];
        ImageType::IndexType ind;
        clonedImage->TransformPhysicalPointToIndex(pt,ind);

		ImageType::SizeType desiredSize = clonedImage->GetLargestPossibleRegion().GetSize();
		ImageType::IndexType desiredStart;
		desiredStart[0] = 0; desiredStart[1] = ind[1]; desiredStart[2] = 0;
		desiredSize[1] = 0;
		ImageType::RegionType desiredRegion(desiredStart, desiredSize);
		ExtractorTypeRGB::Pointer filter = ExtractorTypeRGB::New();
		filter->SetExtractionRegion(desiredRegion);
		filter->SetInput(clonedImage);
		#if ITK_VERSION_MAJOR >= 4
		filter->SetDirectionCollapseToIdentity(); // This is required.
		#endif
		try {
			filter->Update();
		} catch( itk::ExceptionObject & e ) {
			std::cerr << "Exception caught while updating ExtractorTypeRGB " << std::endl;
			std::cerr << e << std::endl;
		}
		RGBImageType::Pointer image = filter->GetOutput();
        
		// draw cross
        RGBPixelType pixel; pixel[0] = 255; pixel[1] = 255; pixel[2] = 255;
		for (int x=-radius; x<=radius; x++) {
			RGBImageType::IndexType ind_x, ind_y;
			ind_x[0] = ind[0]+x; ind_x[1] = ind[2]; ind_y[0] = ind[0]; ind_y[1] = ind[2]+x;
			image->SetPixel(ind_x, pixel);
			image->SetPixel(ind_y, pixel);
		}

		typedef itk::ImageFileWriter< RGBImageType > WriterRGBType;
		itk::PNGImageIO::Pointer ioPNG = itk::PNGImageIO::New();
		WriterRGBType::Pointer writerPNG = WriterRGBType::New();
		writerPNG->SetInput(image);
		writerPNG->SetImageIO(ioPNG);
		writerPNG->SetFileName(filename);
		try {
		    writerPNG->Update();
		}
		catch( itk::ExceptionObject & e )
		{
			cout << "Exception thrown ! " << endl;
			cout << "An error ocurred during Writing PNG" << endl;
			cout << "Location    = " << e.GetLocation()    << endl;
			cout << "Description = " << e.GetDescription() << endl;
		}
    }
    else cout << "Error: Spinal cord center not detected" << endl;
}