显示图像数据和单元格中心


开发环境:

  1. Windows 11 家庭中文版
  2. Microsoft Visual Studio Community 2019
  3. VTK-9.3.0.rc0
  4. vtk-example
  5. vtk-example

demo解决问题:如何使用 VTK 来显示图像数据和单元格中心
显示图像数据和单元格中心_第1张图片

关键点:

  1. 使用 vtkCellCenters 过滤器来获取图像数据的单元格中心,将单元格中心显示为点
  vtkNew<vtkCellCenters> cellCentersFilter;
  cellCentersFilter->SetInputData(imageData);
  cellCentersFilter->VertexCellsOn();
  cellCentersFilter->Update();

  // Access the cell centers
  for (vtkIdType i = 0; i < cellCentersFilter->GetOutput()->GetNumberOfPoints();
       i++)
  {
    double p[3];
    cellCentersFilter->GetOutput()->GetPoint(i, p);
    cout << "Point " << i << " : " << p[0] << " , " << p[1] << " , " << p[2]
         << endl;
  }

  // Display the cell centers
  vtkNew<vtkDataSetMapper> centerMapper;
  centerMapper->SetInputConnection(cellCentersFilter->GetOutputPort());
  vtkNew<vtkActor> centerActor;
  centerActor->SetMapper(centerMapper);
  centerActor->GetProperty()->SetPointSize(5);
  centerActor->GetProperty()->SetColor(
      colors->GetColor3d("MistyRose").GetData());
  1. 将图像显示为线框形式
  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);
  actor->GetProperty()->SetRepresentationToWireframe();
  actor->GetProperty()->SetColor(colors->GetColor3d("Gold").GetData());

prj name: CellCenters

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main(int, char*[])
{
  vtkNew<vtkNamedColors> colors;

  // Create an image data
  vtkNew<vtkImageData> imageData;

  // Specify the size of the image data
  imageData->SetDimensions(3, 3, 2);
  imageData->SetSpacing(1.0, 1.0, 1.0);
  imageData->SetOrigin(0.0, 0.0, 0.0);

  vtkNew<vtkCellCenters> cellCentersFilter;
  cellCentersFilter->SetInputData(imageData);
  cellCentersFilter->VertexCellsOn();
  cellCentersFilter->Update();

  // Access the cell centers
  for (vtkIdType i = 0; i < cellCentersFilter->GetOutput()->GetNumberOfPoints();
       i++)
  {
    double p[3];
    cellCentersFilter->GetOutput()->GetPoint(i, p);
    cout << "Point " << i << " : " << p[0] << " , " << p[1] << " , " << p[2]
         << endl;
  }

  // Display the cell centers
  vtkNew<vtkDataSetMapper> centerMapper;
  centerMapper->SetInputConnection(cellCentersFilter->GetOutputPort());
  vtkNew<vtkActor> centerActor;
  centerActor->SetMapper(centerMapper);
  centerActor->GetProperty()->SetPointSize(5);
  centerActor->GetProperty()->SetColor(
      colors->GetColor3d("MistyRose").GetData());

  vtkNew<vtkDataSetMapper> mapper;
  mapper->SetInputData(imageData);

  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);
  actor->GetProperty()->SetRepresentationToWireframe();
  actor->GetProperty()->SetColor(colors->GetColor3d("Gold").GetData());

  vtkNew<vtkRenderWindow> renderWindow;

  vtkNew<vtkRenderer> renderer;

  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("CellCenters");

  renderer->AddActor(actor);
  renderer->AddActor(centerActor);

  renderer->SetBackground(colors->GetColor3d("DarkSlateGray").GetData());
  renderer->ResetCamera();

  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(renderWindow);
  renderWindow->Render();
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

proj name: CellCentersDemo

namespace {
vtkSmartPointer<vtkPolyData> ReadPolyData(const char* fileName);
}

int main(int argc, char* argv[])
{
  // Define the colors used in the example
  vtkNew<vtkNamedColors> colors;

  vtkColor3d actorColor = colors->GetColor3d("Banana");
  vtkColor3d pointActorColor = colors->GetColor3d("Peacock");
  vtkColor3d backgroundColor = colors->GetColor3d("Silver");

  // Read the polydata
  auto polyData = ReadPolyData(argc > 1 ? argv[1] : "");

  std::vector<double> bounds(6);
  polyData->GetBounds(&bounds[0]);
  std::vector<double> range(3);
  for (int i = 0; i < 3; ++i)
  {
    range[i] = bounds[2 * i + 1] - bounds[2 * i];
  }
  std::cout << "Range: " << range[0] << ", " << range[1] << ", " << range[2]
            << std::endl;

  vtkNew<vtkCellCenters> centers;
  centers->SetInputData(polyData);

  auto maxRange = std::max_element(range.begin(), range.end());
  double radius = 0;
  if (argc > 1)
  {
    radius = *maxRange * 0.0025;
  }
  else
  {
    radius = *maxRange * 0.01;
  }
  vtkNew<vtkSphereSource> sphere;
  sphere->SetPhiResolution(11);
  sphere->SetThetaResolution(11);
  sphere->SetRadius(radius);

  vtkNew<vtkGlyph3DMapper> pointMapper;
  pointMapper->SetInputConnection(centers->GetOutputPort());
  pointMapper->SetSourceConnection(sphere->GetOutputPort());

  vtkNew<vtkActor> pointActor;
  pointActor->SetMapper(pointMapper);
  pointActor->GetProperty()->SetColor(pointActorColor.GetData());

  vtkNew<vtkPolyDataMapper> mapper;
  mapper->SetInputData(polyData);

  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);
  actor->GetProperty()->SetColor(actorColor.GetData());
  actor->GetProperty()->EdgeVisibilityOn();
  actor->GetProperty()->SetInterpolationToFlat();

  // Setup render window, renderer, and interactor
  vtkNew<vtkRenderer> renderer;
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(renderWindow);
  renderer->AddActor(actor);
  renderer->AddActor(pointActor);

  renderer->ResetCamera();
  renderer->GetActiveCamera()->Azimuth(30);
  renderer->GetActiveCamera()->Elevation(30);
  renderer->GetActiveCamera()->Dolly(1.5);
  renderer->ResetCameraClippingRange();
  renderer->SetBackground(backgroundColor.GetData());

  renderWindow->SetSize(640, 480);
  renderWindow->SetWindowName("CellCentersDemo");
  renderWindow->Render();

  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

你可能感兴趣的:(vtk,计算机视觉,c++)