c++ 使用vtkDataImage渲染简单图像

wfauudbj  于 2023-01-06  发布在  其他
关注(0)|答案(1)|浏览(145)

我想在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();

提前感谢您的帮助^^

cbeh67ev

cbeh67ev1#

终于找到了bug,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();

相关问题