使用 vtkDataImage 渲染一个简单的图像

Render a simple image using vtkDataImage

我想在 C++ 中使用 vtkImageData 渲染一个简单的图像。我知道 vtkImageDataGeometryFilter 可以完成这项工作,但我想问是否有其他方法可以在不使用 PolyData 的情况下获得相同的结果?这是我的代码,但是我在渲染时读取访问冲突,我不知道为什么...

vtkSmartPointer<vtkImageData> imageData =
vtkSmartPointer<vtkImageData>::New();
imageData->SetExtent(0, 5, 0, 5, 0, 1);
imageData->SetSpacing(1, 1, 1);
imageData->SetOrigin(0, 0, 0);
for (int i = 0; i < 5; i++)
{
    for (int j = 0; j < 5; j++)
    {
        imageData->SetScalarComponentFromFloat(i,j,0,0,0);
    }
}

vtkSmartPointer<vtkImageActor> imageActor =
    vtkSmartPointer<vtkImageActor>::New();
imageActor->SetInputData(imageData);

vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
    vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);

renderer->AddActor(imageActor);

renderWindow->Render();
renderWindowInteractor->Start();

在此先感谢您的帮助^^

终于找到了。 "imageData->AllocateScalars(VTK_FLOAT,3);" 行丢失了。以下代码显示一个绿色方块:

    vtkSmartPointer<vtkImageData> imageData =
    vtkSmartPointer<vtkImageData>::New();
imageData->SetExtent(0, 50, 0, 50, 0, 1);
imageData->SetSpacing(1, 1, 1);
imageData->SetOrigin(0, 0, 0);
imageData->AllocateScalars(VTK_FLOAT,3);
for (int i = 0; i < 50; i++)
{
    for (int j = 0; j < 50; j++)
    {
        imageData->SetScalarComponentFromFloat(i,j,0,1,255);
    }
}

vtkSmartPointer<vtkImageActor> imageActor =
    vtkSmartPointer<vtkImageActor>::New();
imageActor->SetInputData(imageData);

vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
    vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);

renderer->AddActor(imageActor);
renderer->SetBackground(1, 0, 0.5);
renderWindow->Render();
renderWindowInteractor->Start();