用于体积渲染的vtkgdcmimagereader

时间:2013-01-25 13:22:22

标签: 3d vtk dicom volume-rendering gdcm

我想阅读一系列2D dicom图像。然后,我将它显示为3D卷。为此,我按照以下步骤操作:

1- ipp分拣机 2- setfilenames 3- vtkimagechangeinformation

之后,我尝试使用vtkimageviewer2将它们可视化。 我发送了几乎所有我编写的代码。我很确定它会加载并对图像进行排序。但是它会出现像

这样的错误
  

排序成功:0.5

     

排序成功:1.5

     

程序意外结束。

这是我的代码:

vtkGDCMImageReader *dicomReader =
vtkGDCMImageReader::New();

std::vector<std::string> filenames;
const char *filename = "/home/telemed/Downloads/Dentascan";

gdcm::Directory d;
d.Load( filename, false);

filenames = d.GetFilenames();

gdcm::IPPSorter s;

s.SetComputeZSpacing( true );

s.SetZSpacingTolerance( 1e-3 );

bool b = s.Sort( filenames );

if( !b )

{

    std::cerr << "Failed to sort:" << "s" << std::endl;

    return 1;

}

std::cout << "Sorting succeeded:" << s.GetZSpacing() << std::endl;

//s.Print( std::cout );

const std::vector<std::string> & sorted = s.GetFilenames();


vtkStringArray *files =vtkStringArray::New();

std::vector< std::string >::const_iterator it = sorted.begin();

for( ; it != sorted.end(); ++it)

{

    const std::string &f = *it;


    files->InsertNextValue( f.c_str() );

}

dicomReader->SetFileNames( files );

//dicomReader->Update();

vtkImageChangeInformation *changer =

vtkImageChangeInformation::New();

changer->SetInputConnection(dicomReader->GetOutputPort());

changer->SetOutputSpacing(VTK_DOUBLE_MAX,VTK_DOUBLE_MAX, 2.0);

changer->Update();


vtkInteractorStyleImage *imageStyle =vtkInteractorStyleImage::New();

vtkRenderWindowInteractor *interactor =vtkRenderWindowInteractor::New();

/ Calculate the center of the volume

dicomReader->GetOutput()->UpdateInformation();
int extent[6];

double spacing[3];

double origin[3];

dicomReader->GetOutput()->GetWholeExtent(extent);

dicomReader->GetOutput()->GetSpacing(spacing);

dicomReader->GetOutput()->GetOrigin(origin);

spacing[2] = 1.5;

vtkImageData* image = vtkImageData ::New();

image->SetSpacing(spacing);

std::cout << "Sorting succeeded:" << spacing[2] << std::endl;

double center[3];

center[0] = origin[0] + spacing[0] * 0.5 * (extent[0] + extent[1]);

center[1] = origin[1] + spacing[1] * 0.5 * (extent[2] + extent[3]);

center[2] = origin[2] + spacing[2] * 0.5 * (extent[4] + extent[5]);

// Matrices for axial, coronal, sagittal, oblique view orientations

static double axialElements[16] = {
    1, 0, 0, center[0],

    0, 1, 0, center[1],

    0, 0, 1, center[2],

    0, 0, 0, 1 };

static double coronalElements[16] = {

    1, 0, 0, center[0],

    0, 0, 1, center[1],

    0,-1, 0, center[2],

    0, 0, 0, 1 };

static double sagittalElements[16] = {

    0, 0,-1, center[0],

    1, 0, 0, center[1],

    0,-1, 0, center[2],

    0, 0, 0, 1 };

// Set the slice orientation

vtkMatrix4x4 *resliceAxes = vtkMatrix4x4::New();

resliceAxes->DeepCopy(coronalElements);


vtkImageReslice *Reslice = vtkImageReslice::New();

Reslice->SetInputConnection(changer->GetOutputPort());

Reslice->SetOutputDimensionality(3);

Reslice->SetResliceAxes(resliceAxes);

Reslice->SetInterpolationModeToLinear();

vtkImageViewer2 *viewer = vtkImageViewer2::New();

viewer->SetupInteractor(interactor);

viewer->SetSize(500,500);

viewer->SetColorWindow(1024);

viewer->SetColorLevel(800);

viewer->SetInputConnection(Reslice->GetOutputPort());

// Create a greyscale lookup table

vtkLookupTable *table = vtkLookupTable::New();

table->SetRange(0, 200); // image intensity range

table->SetValueRange(0.0, 1.0); // from black to white

table->SetSaturationRange(0.0, 0.0); // no color saturation

table->SetRampToLinear();

table->Build();

// Map the image through the lookup table

vtkImageMapToColors *color = vtkImageMapToColors::New();

color->SetLookupTable(table);

color->SetInputConnection(Reslice->GetOutputPort());

// Display the image

vtkImageActor *actor = vtkImageActor::New();

actor->SetInput(color->GetOutput());

vtkRenderer *renderer = vtkRenderer::New();

renderer->AddActor(actor);

vtkRenderWindow *window = vtkRenderWindow::New();

window->AddRenderer(renderer);

interactor->SetInteractorStyle(imageStyle);

window->SetInteractor(interactor);

window->Render();

interactor->Start();

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

您是否可以尝试简化代码并确定问题的来源。使用分而治之的方法。注释掉你的代码的后半部分,重新​​编译并运行它。

答案 1 :(得分:0)

尝试实施VTK_gdcmorthoplanes。