[Qt + VTK] Displaying Single DICOM File Using VTK Widget in Qt

To install the VTK library, you should see tutorial how to install VTK library using CMAKE.
Read this post if you want to read a series of DICOM files using sliderbar.
1. Create a new "Qt GUI Application" Project in Qt
2. Open the .pro file, add the following items in it:
LIBS += -L/usr/local/lib/vtk-5.10 #folder to your VTK library

LIBS += -lvtkCommon \
        -lvtkRendering \
        -lvtkVolumeRendering \
        -lQVTK \
        -lvtkIO \
        -lvtkFiltering \
        -lvtkgdcm
INCLUDEPATH += /usr/local/include/vtk-5.10 #folder to your VTK include files

3. Open the ui designer in Qt Creator (just double click the .ui file)
4. Add a button and a QWidget, right click on QWidget choose "promote to...".
5. A new window appears, in the "Promoted class name" text box insert "QVTKWidget" and in the Header file section, input "QVTKWidget.h" (case-sensitive). Don't mess with the picture below, in my picture the Base class name is QPushButton, but it should appears as QWidget.
6. Just add the following source code to your project:
mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <vtkSmartPointer.h>
#include <vtkImageViewer2.h>
#include <vtkDICOMImageReader.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <QVTKWidget.h>
#include <QFileDialog>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::openDCMFile()
{
    QString fileNameDCM = QFileDialog::getOpenFileName(this,tr("Open File DCM"),QDir::currentPath(),tr("DCM Files (*.dcm )"));
    std::string stdstrFileNameDCM = fileNameDCM.toUtf8().constData();
    drawDICOMImg(stdstrFileNameDCM);
}

void MainWindow::drawDICOMImg(std::string fileDICOM)
{
    vtkSmartPointer<vtkDICOMImageReader> reader =
        vtkSmartPointer<vtkDICOMImageReader>::New();
      reader->SetFileName(fileDICOM.c_str());
      reader->Update();

      vtkSmartPointer<vtkImageViewer2> imageViewer =
        vtkSmartPointer<vtkImageViewer2>::New();
      imageViewer->SetInputConnection(reader->GetOutputPort());
      imageViewer->SetRenderWindow(ui->vtkRenderer->GetRenderWindow());
      imageViewer->Render();

}

void MainWindow::on_btnOpenDCM_clicked()
{
    openDCMFile();
}

mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    
private slots:
    void openDCMFile();
    void drawDICOMImg(std::string fileDICOM);
    void on_btnOpenDCM_clicked();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
main.cpp:
#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    
    return a.exec();
}
DOWNLOAD THE COMPLETE SOURCE CODE: QtSingleDCMDisplay.zip

9 comments:

  1. I have tried this code but I am not able to load the images. Not sure if the images I am trying to load are not good or what, but I am always getting this error:

    ERROR: In devel/VTK5.10.1/IO/vtkDICOMImageReader.cxx, line 346
    vtkDICOMImageReader (0x245aeb0): There was a problem retrieving data from: images/SAMPLE_CV_IMAGES/SAMPLE_IMAGES/imagesdc/I_000033.dcm

    ReplyDelete
  2. What Qt version do you use?
    Sometimes errors happen when using Qt version 5.0.x and above.
    It's recommended that you use Qt version 4.8.x.

    ReplyDelete
  3. I am using Qt 4.8.4. Not sure what the problem may be, I will try with different images and place some logs to see where it breaks.

    ReplyDelete
  4. Problem seems to be after calling readerDCMSeries->Update();

    Also I could collect more information, but not sure how useful is:

    ERROR: In /home/manuel/Documents/Tesis/devel/VTK5.10.1/Filtering/vtkExecutive.cxx, line 756
    vtkStreamingDemandDrivenPipeline (0x193ccb0): Algorithm vtkDICOMImageReader(0x195be50) returned failure for request: vtkInformation (0x1bcf560)
    Debug: Off
    Modified Time: 1702
    Reference Count: 1
    Registered Events: (none)
    Request: REQUEST_DATA
    FORWARD_DIRECTION: 0
    ALGORITHM_AFTER_FORWARD: 1
    FROM_OUTPUT_PORT: 0

    ReplyDelete
  5. You may try my DCM Files : http://www.mediafire.com/?xysq945ys335vfi .
    With my DCM files, my program run flawlessly.

    Maybe you should download the ready-to-run source code from this post : http://www.mediafire.com/?6bmg7jbbcvjpfmj
    I think the format was changed when posted to blogger, so downloading the ready-to-run source code should be a better bet.

    ReplyDelete
  6. Hello, I have tried this program but it does not work !

    ReplyDelete
  7. The same error as manuel
    ERROR: In /home/khelifi/VTK/source/IO/vtkDICOMImageReader.cxx, line 138
    vtkDICOMImageReader (0xf7bc50): Unable to open file

    ERROR: In /home/khelifi/VTK/source/IO/vtkDICOMImageReader.cxx, line 283
    vtkDICOMImageReader (0xf7bc50): There was a problem retrieving data from:

    ERROR: In /home/khelifi/VTK/source/Filtering/vtkExecutive.cxx, line 756
    vtkStreamingDemandDrivenPipeline (0xf7c620): Algorithm vtkDICOMImageReader(0xf7bc50) returned failure for request: vtkInformation (0xff88c0)
    Debug: Off
    Modified Time: 159
    Reference Count: 1
    Registered Events: (none)
    Request: REQUEST_DATA
    FROM_OUTPUT_PORT: 0
    FORWARD_DIRECTION: 0
    ALGORITHM_AFTER_FORWARD: 1

    ReplyDelete
  8. I'm so sorry I don't know why is that happening, because in my case it works without any errors.
    I'm a no DICOM either vtk expert.

    ReplyDelete
  9. Hi ! I am really interesting in your post here !
    I have been trying to integrate VTK 5.10.1 in Qt creator 5 using 4.8.5
    SDK for 3 days, but no success. I do not know how to specify library
    on windows 7 . I read your other post about VTK's integration.
    I know you are not an expert but since you post something on it can
    you give some idead that might help . Thanks .

    ReplyDelete