Beispiel #1
0
ImageBinary::Pointer label2mask(Image::Pointer label, unsigned int element)
{
	ImageBinary::Pointer mask = ImageBinary::New();
	Image::RegionType region = label->GetLargestPossibleRegion();
	mask->SetRegions(region);
	mask->Allocate();
	mask->FillBuffer(0);


	ConstIterator iterator_label(label, label->GetLargestPossibleRegion());
	IteratorBinary iterator_mask(mask, mask->GetLargestPossibleRegion());	

	try
	{
		while(!iterator_label.IsAtEnd())
		{
			if(iterator_label.Get() == element && element || iterator_label.Get() != 0 && !element)
				iterator_mask.Set(1);
			++iterator_label; ++iterator_mask;
		}
	}
	catch(itk::ExceptionObject & err)
	{
		std::cerr<<"Exception caught, outside bounds using iterators"<<std::endl;
		std::cerr<<err<<std::endl;
	}

	return mask;
}
Beispiel #2
0
void job_test()
{
	typedef itk::Image<int,3> Image;
	typedef itk::ImageFileReader<Image> Reader;

	// Read in the image (when debugging in VC++, it may be necessary to set the working directory to "$(TargetDir)").
	std::cout << "Loading input image...\n";
	Reader::Pointer reader = Reader::New();
	reader->SetFileName("../resources/test.bmp");
	reader->Update();
	Image::Pointer image = reader->GetOutput();

	// Create a DICOM volume (obviously not a proper one, as the image being read in is actually a greyscale one).
	DICOMVolume_Ptr volume(new DICOMVolume(image));

	// Set the segmentation options.
	itk::Size<3> size = image->GetLargestPossibleRegion().GetSize();
	WindowSettings windowSettings(40, 400);	// dummy window settings
	CTSegmentationOptions options(30, CTSegmentationOptions::INPUTTYPE_HOUNSFIELD, size, 10, windowSettings);

	// Build the IPF.
	std::cout << "Building IPF...\n";
	typedef CTIPFBuilder::IPF_Ptr IPF_Ptr;
	IPF_Ptr ipf;
	Job_Ptr job(new CTIPFBuilder(volume, options, ipf));
	Job::execute_in_thread(job);
	while(!job->is_finished());

	// Output the mosaic images for each of the partition forest layers.
	std::cout << "Outputting mosaic images...\n";
	for(int i=0; i<=ipf->highest_layer(); ++i)
	{
		output_mosaic_image(ipf, i, size[0], size[1]);
	}
}
Beispiel #3
0
float get_mean(Image::Pointer im)
{
	ConstIterator it;
	it = ConstIterator(im, im->GetLargestPossibleRegion());
	
	float mean = 0, N=0;

	try
	{
		while(!it.IsAtEnd())
		{
			if(it.Get()!=0)
			{
				mean = N/(N+1)*mean + it.Get()/(N+1);
				++N;
			}
			++it;
		}
	}
	catch( itk::ExceptionObject & err)
	{
		std::cout<<"Error calculating mean, iterator error"<<std::endl;
		std::cout<<err<<std::endl;
	}

	return mean;
}
Beispiel #4
0
Image::Pointer binary_and(Image::Pointer input, ImageBinary::Pointer mask)
{
	Image::Pointer output = Image::New();
	Image::RegionType region = input->GetLargestPossibleRegion();
	output->SetRegions(region);

	try
	{
		output->Allocate();
	}
	catch(itk::ExceptionObject & err)
	{
		std::cout<<"Error in binary_and. Allocation failed"<<std::endl;
		std::cout<<err<<std::endl;
		return NULL;
	}

	ConstIteratorBinary iterator_mask(mask, mask->GetLargestPossibleRegion());
	Iterator iterator_input(input, input->GetLargestPossibleRegion());
	Iterator iterator_output(output, output->GetLargestPossibleRegion());

	// all images should be the same size
	
	try
	{
		while(!iterator_input.IsAtEnd())
		{
			if(iterator_mask.Get() == 0)
				iterator_output.Set(0);
			else
				iterator_output.Set(iterator_input.Get());
			++iterator_input; ++iterator_mask; ++iterator_output;
		}
	}
	catch(itk::ExceptionObject & err)
	{
		std::cout<<"Iterator error"<<std::endl;
		std::cout<<err<<std::endl;
	}
	
	return output;
}
Beispiel #5
0
unsigned int get_volume(Image::Pointer im)
{
	ConstIterator it;
	it = ConstIterator(im, im->GetLargestPossibleRegion());
	
	unsigned int volume;

	try
	{
		while(!it.IsAtEnd())
		{
			if(it.Get()!=0)
				++volume;
			++it;
		}
	}
	catch( itk::ExceptionObject & err)
	{
		std::cout<<"Error calculating volume, iterator error"<<std::endl;
		std::cout<<err<<std::endl;
	}

	return volume;
}
Beispiel #6
0
unsigned int get_max(Image::Pointer im)
{
	ConstIterator it;
	it = ConstIterator(im, im->GetLargestPossibleRegion());
	
	unsigned int max = 0;

	try
	{
		while(!it.IsAtEnd())
		{
			if(it.Get()>max)
				max = it.Get();
			++it;
		}
	}
	catch( itk::ExceptionObject & err)
	{
		std::cout<<"Error calculating maximum, iterator error"<<std::endl;
		std::cout<<err<<std::endl;
	}

	return max;
}
  void ToFNrrdImageWriter::ConvertStreamToNrrdFormat( std::string fileName )
  {
    int CaptureWidth = 0;
    int CaptureHeight = 0;
    int PixelNumber = 0;
    int ImageSizeInBytes = 0;
    if (fileName==this->m_RGBImageFileName)
    {
        CaptureWidth = this->m_RGBCaptureWidth;
        CaptureHeight = this->m_RGBCaptureHeight;
        PixelNumber = this->m_RGBPixelNumber;
        ImageSizeInBytes = this->m_RGBImageSizeInBytes;
    } else
    {
        CaptureWidth = this->m_ToFCaptureWidth;
        CaptureHeight = this->m_ToFCaptureHeight;
        PixelNumber = this->m_ToFPixelNumber;
        ImageSizeInBytes = this->m_ToFImageSizeInBytes;
    }
    Image::Pointer imageTemplate = Image::New();
    int dimension ;
    unsigned int* dimensions;
    if(m_ToFImageType == ToFImageType2DPlusT)
    {
      dimension = 4;
      dimensions = new unsigned int[dimension];
      dimensions[0] = CaptureWidth;
      dimensions[1] = CaptureHeight;
      dimensions[2] = 1;
      dimensions[3] = this->m_NumOfFrames;
    }
    else if( m_ToFImageType == ToFImageType3D)
    {
      dimension = 3;
      dimensions = new unsigned int[dimension];
      dimensions[0] = CaptureWidth;
      dimensions[1] = CaptureHeight;
      dimensions[2] = this->m_NumOfFrames;
    }
    else
    {
      throw std::logic_error("No image type set, please choose between 2D+t and 3D!");
    }
    float* floatData;
    unsigned char* rgbData;
    if (fileName==this->m_RGBImageFileName)
    {
      rgbData = new unsigned char[PixelNumber*3];
      for(int i=0; i<PixelNumber*3; i++)
      {
        rgbData[i] = i + 0.0;
      }
      mitk::PixelType RGBType = MakePixelType<unsigned char, itk::RGBPixel<unsigned char>, 3>();
      imageTemplate->Initialize( RGBType,dimension, dimensions, 1);
      imageTemplate->SetSlice(rgbData, 0, 0, 0);
    }
    else
    {
      floatData = new float[PixelNumber];
      for(int i=0; i<PixelNumber; i++)
      {
        floatData[i] = i + 0.0;
      }
      mitk::PixelType FloatType = MakeScalarPixelType<float>();
      imageTemplate->Initialize( FloatType,dimension, dimensions, 1);
      imageTemplate->SetSlice(floatData, 0, 0, 0);
    }

    itk::NrrdImageIO::Pointer nrrdWriter = itk::NrrdImageIO::New();
    nrrdWriter->SetNumberOfDimensions(dimension);
    nrrdWriter->SetPixelType( imageTemplate->GetPixelType().GetPixelType());
    nrrdWriter->SetComponentType( (itk::ImageIOBase::IOComponentType) imageTemplate->GetPixelType().GetComponentType());
    if(imageTemplate->GetPixelType().GetNumberOfComponents() > 1)
    {
      nrrdWriter->SetNumberOfComponents(imageTemplate->GetPixelType().GetNumberOfComponents());
    }

    itk::ImageIORegion ioRegion( dimension );
    mitk::Vector3D spacing = imageTemplate->GetGeometry()->GetSpacing();
    mitk::Point3D origin = imageTemplate->GetGeometry()->GetOrigin();

    for(unsigned int i = 0; i < dimension; i++)
    {
      nrrdWriter->SetDimensions(i,dimensions[i]);
      nrrdWriter->SetSpacing(i,spacing[i]);
      nrrdWriter->SetOrigin(i,origin[i]);

      mitk::Vector3D direction;
      direction.Set_vnl_vector(imageTemplate->GetGeometry()->GetIndexToWorldTransform()->GetMatrix().GetVnlMatrix().get_column(i));
      vnl_vector< double > axisDirection(dimension);

      for(unsigned int j = 0; j < dimension; j++)
      {
        axisDirection[j] = direction[j]/spacing[i];
      }
      nrrdWriter->SetDirection( i, axisDirection );

      ioRegion.SetSize(i, imageTemplate->GetLargestPossibleRegion().GetSize(i) );
      ioRegion.SetIndex(i, imageTemplate->GetLargestPossibleRegion().GetIndex(i) );
    }

    nrrdWriter->SetIORegion(ioRegion);
    nrrdWriter->SetFileName(fileName);
    nrrdWriter->SetUseStreamedWriting(true);

    std::ifstream stream(fileName.c_str(), std::ifstream::binary);
    if (fileName==m_RGBImageFileName)
    {
      unsigned int size = PixelNumber*3 * this->m_NumOfFrames;
      unsigned int sizeInBytes = size * sizeof(unsigned char);
      unsigned char* data = new unsigned char[size];
      stream.read((char*)data, sizeInBytes);
      nrrdWriter->Write(data);
      stream.close();
      delete[] data;
    }
    else
    {
      unsigned int size = PixelNumber * this->m_NumOfFrames;
      unsigned int sizeInBytes = size * sizeof(float);
      float* data = new float[size];
      stream.read((char*)data, sizeInBytes);
      try
      {
        nrrdWriter->Write(data);
      }
      catch (itk::ExceptionObject* e)
      {
        MITK_ERROR<< e->what();
        return;
      }

      stream.close();
      delete[] data;
    }

    delete[] dimensions;
    if (fileName==m_RGBImageFileName)
    {
      delete[] rgbData;
    }
    else
    {
      delete[] floatData;
    }
  }