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; }
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; } }