// Convert a vector ITK image to a VTK image for display void ITKImagetoVTKImage(FloatVectorImageType::Pointer image, vtkImageData* outputImage) { std::cout << "ITKImagetoVTKImage()" << std::endl; if(image->GetNumberOfComponentsPerPixel() >= 3) { ITKImagetoVTKRGBImage(image, outputImage); } else { ITKImagetoVTKMagnitudeImage(image, outputImage); } }
// Convert a vector ITK image to a VTK image for display void ITKImagetoVTKRGBImage(FloatVectorImageType::Pointer image, vtkImageData* outputImage) { // This function assumes an ND (with N>3) image has the first 3 channels as RGB and extra information in the remaining channels. //std::cout << "ITKImagetoVTKRGBImage()" << std::endl; if(image->GetNumberOfComponentsPerPixel() < 3) { std::stringstream ss; ss << "The input image has " << image->GetNumberOfComponentsPerPixel() << " components, but at least 3 are required."; throw std::runtime_error(ss.str()); } // Setup and allocate the image data outputImage->SetNumberOfScalarComponents(3); outputImage->SetScalarTypeToUnsignedChar(); outputImage->SetDimensions(image->GetLargestPossibleRegion().GetSize()[0], image->GetLargestPossibleRegion().GetSize()[1], 1); outputImage->AllocateScalars(); // Copy all of the input image pixels to the output image itk::ImageRegionConstIteratorWithIndex<FloatVectorImageType> imageIterator(image,image->GetLargestPossibleRegion()); imageIterator.GoToBegin(); while(!imageIterator.IsAtEnd()) { unsigned char* pixel = static_cast<unsigned char*>(outputImage->GetScalarPointer(imageIterator.GetIndex()[0], imageIterator.GetIndex()[1],0)); for(unsigned int component = 0; component < 3; component++) { pixel[component] = static_cast<unsigned char>(imageIterator.Get()[component]); } ++imageIterator; } }