Open Image with ITK and send it to VTK importer

I want to open a .dcm file with ITK and send the information to VTK but I am not being able to do it. Here is my coode:

    // 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);



   
    // PARSE TO VTK
 
    
    
    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->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();

I get this messagem when I run the program:

2023-10-12 16:17:17.434 ( 0.735s) [ 63FF4640] vtkImageData.cxx:1544 ERR| vtkImageData (0x558706c46c30): GetScalarPointer: Pixel (0, 8, 0) not in memory.
Current extent= (0, 0, 0, 0, 0, 0)
2023-10-12 16:17:17.434 ( 0.735s) [ 637F3640] vtkImageData.cxx:1544 ERR| vtkImageData (0x558706c46c30): GetScalarPointer: Pixel (0, 16, 0) not in memory.
Current extent= (0, 0, 0, 0, 0, 0)
2023-10-12 16:17:17.434 ( 0.735s) [ 627F1640] vtkImageData.cxx:1544 ERR| vtkImageData (0x558706c46c30): GetScalarPointer: Pixel (0, 32, 0) not in memory.
Current extent= (0, 0, 0, 0, 0, 0)
2023-10-12 16:17:17.434 ( 0.735s) [ 61FF0640] vtkImageData.cxx:1544 ERR| vtkImageData (0x558706c46c30): GetScalarPointer: Pixel (0, 48, 0) not in memory.
Current extent= (0, 0, 0, 0, 0, 0)
Falha de segmentação (imagem do núcleo gravada)

Thanks!

Try adding this before the Update():

    vtkImporter->SetDataExtentToWholeExtent();

Thank you, that was the issue…