Need Help with VTK Project: Loading Medical Images and draw line on it, vtkImagedata, vtklinewidget2 qt and cpp

I’m working on a project that involves loading medical image data in VTK (Visualization Toolkit), using Qt and C++, and rendering it using VTKImageViewer2. Additionally, I need to draw a line on the displayed image using VTKLineWidget2. I’ve added two renderers to the render window with layered indexing. Unfortunately, only the image is being displayed. Below, I have provided the code.

QVTKOpenGLWidget *m_pvtkWidget = new QVTKOpenGLWidget;
m_pvtkWidget->setStyleSheet("background-color: black");
vtkNew <vtkGenericOpenGLRenderWindow> m_openGlRenWin;

m_pvtkWidget->SetRenderWindow(m_openGlRenWin);


vtkSmartPointer<vtkDICOMImageReader> reader = vtkSmartPointer<vtkDICOMImageReader>::New();
QString filep= "path to dicom file";
reader->SetFileName(filep.toUtf8().constData());
reader->Update();
vtkSmartPointer<vtkImageData> imageData = reader->GetOutput();  

vtkSmartPointer<vtkRenderer> m_rendererMainScene = vtkSmartPointer<vtkRenderer>::New();

    m_rendererMainScene->GradientBackgroundOn();
    m_rendererMainScene->SetLayer(0); // Layer 0 has a background

   vtkSmartPointer<vtkRenderer> m_rendererForeground = vtkSmartPointer<vtkRenderer>::New();
 
    m_rendererForeground->SetLayer(1);

     m_pvtkWidget->GetRenderWindow()->AddRenderer(m_rendererMainScene);
     m_pvtkWidget->GetRenderWindow()->AddRenderer(m_rendererForeground);

    vtkNew<vtkImageActor> imageActor;
     imageActor->GetMapper()->SetInputData(imageData );

 
    m_rendererMainScene->AddActor(imageActor);

    vtkSmartPointer<vtkLineWidget2> lineWidget = vtkSmartPointer<vtkLineWidget2>::New();
    double pointPos1[3];
    double pointPos2[3];
    pointPos1[0]=50;
    pointPos1[1]=50;
    pointPos1[2]=0;
    pointPos2[0]=150;
    pointPos2[1]=150;
    pointPos2[2]=0;
    lineWidget->CreateDefaultRepresentation();
    lineWidget->GetLineRepresentation()->SetLineColor(255, 0,0);
    lineWidget->GetLineRepresentation()->GetLineProperty()->SetLineWidth(.25);
    lineWidget->GetLineRepresentation()->SetPoint1WorldPosition(pointPos1);
    lineWidget->GetLineRepresentation()->SetPoint2WorldPosition(pointPos2);
    lineWidget->GetRepresentation()->SetHandleSize(0);
    lineWidget->GetEventTranslator()->ClearEvents();
       lineWidget->SetInteractor(m_pvtkWidget->GetRenderWindow()->GetInteractor());

    m_rendererForeground->AddActor(lineWidget->GetLineRepresentation());
    lineWidget->On();

   m_pvtkWidget->GetRenderWindow()->Render();
    m_pvtkWidget->GetRenderWindow()->GetInteractor()->Start();

Could you please take a look at it and kindly help me sort it out?

Hello,

I believe this is missing:

   m_rendererForeground->SetActiveCamera( m_rendererMainScene->GetActiveCamera() );

best,

PC

This is exactly what 3D Slicer has been designed for. It looks like a really simple task, but even just loading a wide range of DICOM data robustly (including segmentations, structure sets, transforms, 4D/5D data, etc.; handle loading of images with variable slice spacing, tilted gantry, etc.), render it in slice and 3D views, and segmenting and annotating is a very large project that should not be restarted from scratch. 3D Slicer is open-source, completely freely usable and modifiable/extensible for any purpose.

If you want to do just a very simple application and you are sure that it will not need to evolve to a more complete, feature-rich tool then it is probably better to use web technologies (vtk.js, OHIF, maybe trame, etc.) as users will refuse to install an application just to do some simple things.

Thank you @lassoan .