I am following imageimport exemple but I can’t visualize the image…
// VTK
#include <vtkSmartPointer.h>
#include <vtkDICOMImageReader.h>
#include <vtkImageViewer2.h>
#include <vtkRenderWindow.h>
#include "vtkPointData.h"
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkImageData.h>
#include <vtkImageImport.h>
#include <vtkImageMapper3D.h>
#include <vtkCamera.h>
#include <vtkImageActor.h>
#include <vtkInteractorStyleImage.h>
//#include <vtkXMLImageDataWriter.h>
// ITK
#include <itkImage.h>
#include <itkImageFileReader.h>
//#include <itkImageToVTKImageFilter.h>
int main(int argc, char* argv[])
{
// Check for the correct number of command-line arguments
if (argc != 2)
{
std::cerr << "Usage: " << argv[0] << " <DICOM_directory>" << std::endl;
return EXIT_FAILURE;
}
// Define the image type using ITK
using PixelType = unsigned short;
using ImageType = itk::Image<PixelType, 3>;
// Create an ITK DICOM image reader
using ImageReaderType = itk::ImageFileReader<ImageType>;
ImageReaderType::Pointer imageReader = ImageReaderType::New();
imageReader->SetFileName(argv[1]);
try {
imageReader->Update();
} catch (itk::ExceptionObject &ex) {
std::cerr << "ITK Error: " << ex << std::endl;
return 1;
}
// Get the ITK image
ImageType::Pointer itkImage = imageReader->GetOutput();
ImageType::SpacingType spacing;
spacing[0] = 0.878690; // Spacing along the X-axis (in mm)
spacing[1] = 0.878690; // Spacing along the Y-axis (in mm)
spacing[2] = 1.25; // Spacing along the Z-axis (in mm)
itkImage->SetSpacing(spacing);
ImageType::PointType origin;
origin[0] = 0.0; // X-coordinate of the origin (in mm)
origin[1] = 0.0; // Y-coordinate of the origin (in mm)
origin[2] = 0.0; // Z-coordinate of the origin (in mm)
itkImage->SetOrigin(origin);
ImageType::DirectionType direction;
direction[0][0]=1;
direction[1][1]=1;
direction[2][2]=1;
// Set the direction matrix for the image
itkImage->SetDirection(direction);
vtkSmartPointer<vtkImageImport> vtkImporter =
vtkSmartPointer<vtkImageImport>::New();
vtkImporter->SetDataSpacing(itkImage->GetSpacing()[0], itkImage->GetSpacing()[1],
itkImage->GetSpacing()[2]);
vtkImporter->SetDataOrigin(itkImage->GetOrigin()[0], itkImage->GetOrigin()[1], itkImage-
>GetOrigin()[2]);
vtkImporter->SetDataExtent(
0, itkImage->GetLargestPossibleRegion().GetSize()[0] - 1,
0, itkImage->GetLargestPossibleRegion().GetSize()[1] - 1,
0, itkImage->GetLargestPossibleRegion().GetSize()[2] - 1);
vtkImporter->SetWholeExtent(
itkImage->GetLargestPossibleRegion().GetIndex()[0],
itkImage->GetLargestPossibleRegion().GetIndex()[0] + itkImage->GetLargestPossibleRegion().GetSize()[0] - 1,
itkImage->GetLargestPossibleRegion().GetIndex()[1],
itkImage->GetLargestPossibleRegion().GetIndex()[1] + itkImage->GetLargestPossibleRegion().GetSize()[1] - 1,
itkImage->GetLargestPossibleRegion().GetIndex()[2],
itkImage->GetLargestPossibleRegion().GetIndex()[2] + itkImage->GetLargestPossibleRegion().GetSize()[2] - 1);
vtkImporter->SetImportVoidPointer(itkImage->GetBufferPointer());
vtkImporter->SetDataScalarTypeToUnsignedShort();
vtkImporter->Update();
vtkImporter->GetErrorCode();
// Visualize
// Create an actor
vtkNew<vtkImageActor> actor;
actor->SetInputData(vtkImporter->GetOutput());
actor->GetMapper()->SetInputData(vtkImporter->GetOutput()); // Use GetMapper to access the mapper
vtkNew<vtkNamedColors> colors;
// Setup renderer
vtkNew<vtkRenderer> renderer;
renderer->AddActor(actor);
renderer->ResetCamera();
renderer->SetBackground(colors->GetColor3d("SaddleBrown").GetData());
renderer->GetActiveCamera()->ParallelProjectionOn(); // Use parallel projection
renderer->GetActiveCamera()->SetPosition(264, 34, 256); // Set camera position
renderer->GetActiveCamera()->SetFocalPoint(0, 0, 0); // Set focal point
renderer->GetActiveCamera()->SetViewUp(0, 1, 0); // Set view up direction
//vtkRenderWindow* renderWindow = vtkRenderWindow::New();
// Setup render window
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("ImageImport");
// Setup render window interactor
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
vtkNew<vtkInteractorStyleImage> style;
renderWindowInteractor->SetInteractorStyle(style);
// Render and start interaction
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindow->Render();
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
It compiles but I can’t visualize anything