[Insight-users] Problem with volume rendering using ITK, VTK and DICOM files
Phrenology for Bipolarity
enxeltheeagle151 at msn.com
Thu May 19 18:21:30 EDT 2011
Hi all,
I'm trying to visualize a DICOM series of files with VTK but when the volume is rendered everything is wrapped within a big black box.
This is what I get: http://img64.imageshack.us/img64/3506/visualize.png
And the volume I should be getting is inside it.
Can someone tell me what I'm doing wrong please? Or give some advice to fix this problem?
Below is my code, which is a combination of "InsightToolkit\Examples\IO\DicomSeriesReadImageWrite2.cxx" and "InsightApplications\Auxiliary\vtk\itkReadITKImageShowVTK.cxx" with some adjustments.
-------------------------------------------------------------------------
#include "itkOrientedImage.h"
#include "itkGDCMImageIO.h"
#include "itkGDCMSeriesFileNames.h"
#include "itkImageSeriesReader.h"
#include "itkImageFileWriter.h"
#include "itkCommand.h"
#include "itkImage.h"
#include "itkVTKImageExport.h"
#include "itkVTKImageImport.h"
#include "itkCurvatureFlowImageFilter.h"
#include "itkCastImageFilter.h"
#include "itkRGBPixel.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "vtkImageData.h"
#include "vtkImageImport.h"
#include "vtkImageExport.h"
#include "vtkImageActor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkInteractorStyleImage.h"
#include "vtkPiecewiseFunction.h"
#include "vtkColorTransferFunction.h"
#include "vtkVolumeProperty.h"
#include "vtkVolumeTextureMapper3D.h"
#include "vtkFixedPointVolumeRayCastMapper.h"
#include "vtkSmartPointer.h"
template <typename ITK_Exporter, typename VTK_Importer>
void ConnectPipelines(ITK_Exporter exporter, VTK_Importer* importer)
{
importer->SetUpdateInformationCallback(exporter->GetUpdateInformationCallback());
importer->SetPipelineModifiedCallback(exporter->GetPipelineModifiedCallback());
importer->SetWholeExtentCallback(exporter->GetWholeExtentCallback());
importer->SetSpacingCallback(exporter->GetSpacingCallback());
importer->SetOriginCallback(exporter->GetOriginCallback());
importer->SetScalarTypeCallback(exporter->GetScalarTypeCallback());
importer->SetNumberOfComponentsCallback(exporter->GetNumberOfComponentsCallback());
importer->SetPropagateUpdateExtentCallback(exporter->GetPropagateUpdateExtentCallback());
importer->SetUpdateDataCallback(exporter->GetUpdateDataCallback());
importer->SetDataExtentCallback(exporter->GetDataExtentCallback());
importer->SetBufferPointerCallback(exporter->GetBufferPointerCallback());
importer->SetCallbackUserData(exporter->GetCallbackUserData());
}
template <typename VTK_Exporter, typename ITK_Importer>
void ConnectPipelines(VTK_Exporter* exporter, ITK_Importer importer)
{
importer->SetUpdateInformationCallback(exporter->GetUpdateInformationCallback());
importer->SetPipelineModifiedCallback(exporter->GetPipelineModifiedCallback());
importer->SetWholeExtentCallback(exporter->GetWholeExtentCallback());
importer->SetSpacingCallback(exporter->GetSpacingCallback());
importer->SetOriginCallback(exporter->GetOriginCallback());
importer->SetScalarTypeCallback(exporter->GetScalarTypeCallback());
importer->SetNumberOfComponentsCallback(exporter->GetNumberOfComponentsCallback());
importer->SetPropagateUpdateExtentCallback(exporter->GetPropagateUpdateExtentCallback());
importer->SetUpdateDataCallback(exporter->GetUpdateDataCallback());
importer->SetDataExtentCallback(exporter->GetDataExtentCallback());
importer->SetBufferPointerCallback(exporter->GetBufferPointerCallback());
importer->SetCallbackUserData(exporter->GetCallbackUserData());
}
void visualize(std::string dcm)
{
typedef itk::RGBPixel< signed short > PixelType;
const unsigned int Dimension = 3;
typedef itk::OrientedImage< PixelType, 3 > ImageType;
typedef itk::ImageSeriesReader< ImageType > ReaderType;
ReaderType::Pointer reader = ReaderType::New();
typedef itk::GDCMImageIO ImageIOType;
ImageIOType::Pointer dicomIO = ImageIOType::New();
reader->SetImageIO( dicomIO );
typedef itk::GDCMSeriesFileNames NamesGeneratorType;
NamesGeneratorType::Pointer nameGenerator = NamesGeneratorType::New();
nameGenerator->SetUseSeriesDetails( true );
nameGenerator->AddSeriesRestriction("0008|0021" );
nameGenerator->SetDirectory(directoryPath);
try{
std::cout << std::endl << "The directory: " << std::endl;
std::cout << std::endl << directoryPath << std::endl << std::endl;
std::cout << "Contains the following DICOM Series: ";
std::cout << std::endl << std::endl;
typedef std::vector< std::string > SeriesIdContainer;
const SeriesIdContainer & seriesUID = nameGenerator->GetSeriesUIDs();
SeriesIdContainer::const_iterator seriesItr = seriesUID.begin();
SeriesIdContainer::const_iterator seriesEnd = seriesUID.end();
while( seriesItr != seriesEnd ) {
std::cout << seriesItr->c_str() << std::endl;
seriesItr++;
}
std::string seriesIdentifier;
seriesIdentifier = seriesUID.begin()->c_str();
std::cout << std::endl << std::endl;
std::cout << "Now reading series: " << std::endl << std::endl;
std::cout << seriesIdentifier << std::endl;
std::cout << std::endl << std::endl;
typedef std::vector< std::string > FileNamesContainer;
FileNamesContainer fileNames;
fileNames = nameGenerator->GetFileNames( seriesIdentifier );
FileNamesContainer::const_iterator iterator = fileNames.begin();
FileNamesContainer::const_iterator final = fileNames.end();
while( iterator != final )
{
std::cout << iterator->c_str() << std::endl;
iterator++;
}
reader->SetFileNames( fileNames );
try
{
reader->Update();
}
catch (itk::ExceptionObject &ex)
{
std::cout << ex << std::endl;
return EXIT_FAILURE;
}
}
catch (itk::ExceptionObject &ex)
{
std::cout << ex << std::endl;
return EXIT_FAILURE;
}
typedef itk::VTKImageExport< ImageType > ExportFilterType;
ExportFilterType::Pointer itkExporter = ExportFilterType::New();
itkExporter->SetInput( reader->GetOutput() );
vtkImageImport* vtkImporter = vtkImageImport::New();
ConnectPipelines(itkExporter, vtkImporter);
typedef itk::VTKImageImport< ImageType > ImportFilterType;
ImportFilterType::Pointer itkImporter = ImportFilterType::New();
vtkImageExport* vtkExporter = vtkImageExport::New();
ConnectPipelines(vtkExporter, itkImporter);
vtkExporter->SetInput( vtkImporter->GetOutput() );
vtkPiecewiseFunction* opacityTransferFunction = vtkPiecewiseFunction::New();
opacityTransferFunction->AddPoint(0, 0.00);
opacityTransferFunction->AddPoint(500, 0.15);
opacityTransferFunction->AddPoint(1000, 0.15);
opacityTransferFunction->AddPoint(1150, 0.85);
vtkColorTransferFunction* colorTransferFunction = vtkColorTransferFunction::New();
colorTransferFunction->AddRGBPoint(0, 0.0, 0.0, 0.0);
colorTransferFunction->AddRGBPoint(500, 1.0, 0.5, 0.3);
colorTransferFunction->AddRGBPoint(1000, 1.0, 0.5, 0.3);
colorTransferFunction->AddRGBPoint(1150, 1.0, 1.0, 0.9);
vtkVolume* volume = vtkVolume::New();
vtkVolumeProperty* volumeProperty = vtkVolumeProperty::New();
vtkFixedPointVolumeRayCastMapper* volumeMapperSoftware = vtkFixedPointVolumeRayCastMapper::New();
volumeProperty->SetScalarOpacity(opacityTransferFunction);
volumeProperty->SetColor(colorTransferFunction);
volumeProperty->SetInterpolationTypeToLinear();
volumeProperty->ShadeOff();
volume->SetProperty(volumeProperty);
volumeMapperSoftware->SetInput(vtkImporter->GetOutput());
volumeMapperSoftware->SetSampleDistance(1.0);
volume->SetMapper(volumeMapperSoftware);
vtkInteractorStyleTrackballCamera * interactorStyle = vtkInteractorStyleTrackballCamera::New();
vtkRenderer* renderer = vtkRenderer::New();
vtkRenderWindow* renWin = vtkRenderWindow::New();
vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::New();
renWin->SetSize(500, 500);
renWin->AddRenderer(renderer);
iren->SetRenderWindow(renWin);
iren->SetInteractorStyle(interactorStyle);
renderer->AddVolume(volume);
renderer->SetBackground(0.5, 0.5, 0.5);
renWin->Render();
iren->Start();
vtkImporter->Delete();
vtkExporter->Delete();
renWin->Delete();
renderer->Delete();
iren->Delete();
}
int main(int argc, char * argv [] )
{
if( argc < 2 )
{
std::cerr << "Usage: " << argv[0] << " path to DICOM series" << std::endl;
return 1;
}
visualize(argv[1]);
return 0;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20110519/e23be0af/attachment.htm>
More information about the Insight-users
mailing list