void Initialisation::savePointAsBinaryImage(ImageType::Pointer initialImage, string filename, OrientationType orientation)
{
    if (points_.size() > 0)
    {
        typedef itk::Image< unsigned char, 3 > BinaryImageType;
        BinaryImageType::Pointer binary = BinaryImageType::New();
        ImageType::RegionType region;
        ImageType::IndexType start;
        start[0] = 0; start[1] = 0; start[2] = 0;
        ImageType::SizeType size, imSize = initialImage->GetLargestPossibleRegion().GetSize();
        size[0] = imSize[0]; size[1] = imSize[1]; size[2] = imSize[2];
        region.SetSize(size);
        region.SetIndex(start);
        binary->CopyInformation(initialImage);
        binary->SetRegions(region);
        binary->Allocate();
        binary->FillBuffer(false);
        
        typedef ImageType::IndexType IndexType;
        ContinuousIndex ind;
        IndexType ind2;
        unsigned int pSize = points_.size();
        unsigned int indexMiddle = 0;
        for (unsigned int i=0; i<pSize; i++) {
            if (points_[i][1] == startSlice_)
                indexMiddle = i;
        }
        ind[0] = points_[indexMiddle][0]; ind[1] = points_[indexMiddle][1]; ind[2] = points_[indexMiddle][2];
        PointType pt;
        inputImage_->TransformContinuousIndexToPhysicalPoint(ind, pt);
        initialImage->TransformPhysicalPointToIndex(pt, ind2);
        binary->SetPixel(ind2,true);
        
        OrientImage<BinaryImageType> orientationFilter;
        orientationFilter.setInputImage(binary);
        orientationFilter.orientation(orientation);
        binary = orientationFilter.getOutputImage();
        
        ImageIterator it( binary, binary->GetRequestedRegion() );
        it.GoToBegin();
        while(!it.IsAtEnd())
        {
            if (it.Get()==true)
            {
                ind2 = it.GetIndex();
                break;
            }
            ++it;
        }
        if (verbose_) cout << "Center of spinal cord saved on pixel : " << ind2 << endl;
        
        WriterBinaryType::Pointer writer = WriterBinaryType::New();
        itk::NiftiImageIO::Pointer io = itk::NiftiImageIO::New();
        writer->SetImageIO(io);
        writer->SetFileName(filename);
        writer->SetInput(binary);
        try {
            writer->Write();
        } catch( itk::ExceptionObject & e ) {
            std::cerr << "Exception caught while writing image " << std::endl;
            std::cerr << e << std::endl;
        }
    }
    else cout << "Error: Spinal cord center not detected" << endl;
}
예제 #2
0
double Mesh::computeStandardDeviationFromPixelsInside(ImageType::Pointer image)
{
    MeshTypeB::Pointer mesh = MeshTypeB::New();
    typedef itk::Point< double, 3 > PointType;
    PointType pnt;
    ImageType::IndexType index;
    CVector3 p, n, min(10000,10000,10000), max;
    for (unsigned int i=0; i<points_.size(); i++) {
        p = points_[i]->getPosition();
        pnt[0] = p[0]; pnt[1] = p[1]; pnt[2] = p[2];
        mesh->SetPoint(i,pnt);
        
        image->TransformPhysicalPointToIndex(pnt, index);
        if (index[0]<min[0]) min[0]=index[0];
        if (index[1]<min[1]) min[1]=index[1];
        if (index[2]<min[2]) min[2]=index[2];
        if (index[0]>max[0]) max[0]=index[0];
        if (index[1]>max[1]) max[1]=index[1];
        if (index[2]>max[2]) max[2]=index[2];
    }
    for (unsigned int i=0; i<triangles_.size(); i+=3)
    {
        CellTypeB::CellAutoPointer triangle;
        triangle.TakeOwnership(new CellTypeB);
        triangle->SetPointId(0,triangles_[i]);
        triangle->SetPointId(1,triangles_[i+1]);
        triangle->SetPointId(2,triangles_[i+2]);
        mesh->SetCell((int)(i+1)/3,triangle);
    }
    
    
    CastFilterType::Pointer castFilter = CastFilterType::New();
    castFilter->SetInput(image);
    BinaryImageType::Pointer im = castFilter->GetOutput();
    
    BinaryImageType::IndexType indexRegion;
    BinaryImageType::SizeType sizeRegion;
    sizeRegion[0] = max[0]-min[0]+5; // adding two pixels at each side to be sure
    sizeRegion[1] = max[1]-min[1]+5;
    sizeRegion[2] = max[2]-min[2]+5;
    indexRegion[0] = min[0]-2;
    indexRegion[1] = min[1]-2;
    indexRegion[2] = min[2]-2;
    
    BinaryImageType::RegionType region(indexRegion,sizeRegion);
    im->SetRegions(region);
    
    MeshFilterType::Pointer meshFilter = MeshFilterType::New();
    meshFilter->SetInput(mesh);
    meshFilter->SetInfoImage(im);
    /*meshFilter->SetDirection(image->GetDirection());
    meshFilter->SetSpacing(image->GetSpacing());
    meshFilter->SetOrigin(image->GetOrigin());
    meshFilter->SetSize(sizeRegion);
    meshFilter->SetIndex(indexRegion);*/
    meshFilter->SetTolerance(1.0);
    meshFilter->SetInsideValue(1.0);
    meshFilter->SetOutsideValue(0.0);
    try {
        meshFilter->Update();
    }
    catch( itk::ExceptionObject & e )
    {
        cout << "Exception thrown ! " << endl;
        cout << "An error ocurred during creating binary image" << endl;
        cout << "Location    = " << e.GetLocation()    << endl;
        cout << "Description = " << e.GetDescription() << endl;
    }
    
    MeshFilterType::OutputImageType::Pointer binary = meshFilter->GetOutput();
    
    vector<double> valueVoxels;
    
    typedef ImageType::IndexType IndexType;
    IndexType ind;
    ImageIterator it( binary, binary->GetRequestedRegion() );
    it.GoToBegin();
    while(!it.IsAtEnd())
    {
        if (it.Get()==true)
        {
            ind = it.GetIndex();
            valueVoxels.push_back(image->GetPixel(ind));
        }
        ++it;
    }
    
    double mean = 0.0, sum = 0.0, std = 0.0, nbVoxels = valueVoxels.size();
    for (int i=0; i<nbVoxels; i++)
        mean += valueVoxels[i];
    mean /= nbVoxels;
    for (int i=0; i<nbVoxels; i++) {
        sum += valueVoxels[i]*valueVoxels[i];
    }
    std = sqrt(sum/nbVoxels-mean*mean);
    
    /*ofstream myfile;
	myfile.open("StandardDeviation.txt", ios_base::app);
	myfile << ind[1] << " " << mean << " " << std << endl;
	myfile.close();*/
    
    return mean;
}
예제 #3
0
void mitk::SurfaceStampImageFilter::SurfaceStampProcessing(itk::Image<TPixel, 3> *input, MeshType *mesh)
{
  typedef itk::Image<TPixel, 3> ImageType;
  typedef itk::Image<unsigned char, 3> BinaryImageType;

  typedef itk::TriangleMeshToBinaryImageFilter<mitk::SurfaceStampImageFilter::MeshType, BinaryImageType> FilterType;

  BinaryImageType::Pointer binaryInput = BinaryImageType::New();
  binaryInput->SetSpacing(input->GetSpacing());
  binaryInput->SetOrigin(input->GetOrigin());
  binaryInput->SetDirection(input->GetDirection());
  binaryInput->SetRegions(input->GetLargestPossibleRegion());
  binaryInput->Allocate();
  binaryInput->FillBuffer(0);

  FilterType::Pointer filter = FilterType::New();
  filter->SetInput(mesh);
  filter->SetInfoImage(binaryInput);
  filter->SetInsideValue(1);
  filter->SetOutsideValue(0);
  filter->Update();

  BinaryImageType::Pointer resultImage = filter->GetOutput();
  resultImage->DisconnectPipeline();

  mitk::Image::Pointer outputImage = this->GetOutput();
  typename ImageType::Pointer itkOutputImage;
  mitk::CastToItkImage(outputImage, itkOutputImage);

  typedef itk::ImageRegionConstIterator<BinaryImageType> BinaryIteratorType;
  typedef itk::ImageRegionConstIterator<ImageType> InputIteratorType;
  typedef itk::ImageRegionIterator<ImageType> OutputIteratorType;

  BinaryIteratorType sourceIter(resultImage, resultImage->GetLargestPossibleRegion());
  sourceIter.GoToBegin();

  InputIteratorType inputIter(input, input->GetLargestPossibleRegion());
  inputIter.GoToBegin();

  OutputIteratorType outputIter(itkOutputImage, itkOutputImage->GetLargestPossibleRegion());
  outputIter.GoToBegin();

  typename ImageType::PixelType inputValue;
  unsigned char sourceValue;

  auto fgValue = static_cast<typename ImageType::PixelType>(m_ForegroundValue);
  auto bgValue = static_cast<typename ImageType::PixelType>(m_BackgroundValue);

  while (!sourceIter.IsAtEnd())
  {
    sourceValue = static_cast<unsigned char>(sourceIter.Get());
    inputValue = static_cast<typename ImageType::PixelType>(inputIter.Get());

    if (sourceValue != 0)
      outputIter.Set(fgValue);
    else if (m_OverwriteBackground)
      outputIter.Set(bgValue);
    else
      outputIter.Set(inputValue);

    ++sourceIter;
    ++inputIter;
    ++outputIter;
  }
}