[QT + VTK] Displaying DICOM Series Using VTK Widget and Sliderbar in Qt

This tutorial is a continuation from my previous post about displaying a single DICOM file. To install the VTK library, you should see tutorial how to install VTK library using CMAKE.
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, a slider 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. Add the following source code to your project:
mainwindow.h: 
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <vtkSmartPointer.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkDICOMImageReader.h>
#include <vtkImageViewer2.h>
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    
private slots:
    void openDCMFolder();
    void drawDCMSeries(std::string folderDCM);

    void on_btnOpenDCMFolder_clicked();

    void on_hSliderDCM_sliderMoved(int position);

private:
    Ui::MainWindow *ui;
    vtkSmartPointer<vtkDICOMImageReader> readerDCMSeries;
    vtkSmartPointer<vtkImageViewer2> imageViewerDCMSeriesX;
    int mMinSliderX;
    int mMaxSliderX;
};

#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();
}
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <vtkSmartPointer.h>
#include <vtkObjectFactory.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkActor.h>
#include <vtkImageViewer2.h>
#include <vtkDICOMImageReader.h>
#include <vtkInteractorStyleImage.h>
#include <vtkActor2D.h>
#include <vtkTextProperty.h>
#include <vtkTextMapper.h>
#include <QFileDialog>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    readerDCMSeries = vtkSmartPointer<vtkDICOMImageReader>::New();
    imageViewerDCMSeriesX = vtkSmartPointer<vtkImageViewer2>::New();
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::openDCMFolder()
{
    QString folderNameDCM = QFileDialog::getExistingDirectory(this,tr("Open DCM Folder"),QDir::currentPath(),QFileDialog::ShowDirsOnly);
    std::string stdstrFolderNameDCM = folderNameDCM.toUtf8().constData();
    drawDCMSeries(stdstrFolderNameDCM);
}

void MainWindow::drawDCMSeries(std::string folderDCM)
{
    readerDCMSeries->SetDirectoryName(folderDCM.c_str());
    readerDCMSeries->Update();
    imageViewerDCMSeriesX->SetInputConnection(readerDCMSeries->GetOutputPort());
    mMinSliderX = imageViewerDCMSeriesX->GetSliceMin();
    mMaxSliderX = imageViewerDCMSeriesX->GetSliceMax();
    ui->hSliderDCM->setMinimum(mMinSliderX);
    ui->hSliderDCM->setMaximum(mMaxSliderX);
    imageViewerDCMSeriesX->SetRenderWindow(ui->vtkRenderer->GetRenderWindow());
}

void MainWindow::on_btnOpenDCMFolder_clicked()
{
    openDCMFolder();
}

void MainWindow::on_hSliderDCM_sliderMoved(int position)
{
    imageViewerDCMSeriesX->SetSlice(position);
    imageViewerDCMSeriesX->Render();
}
DOWNLOAD THE COMPLETE SOURCE CODE : QtDCMSeriesDisplay.zip

No comments:

Post a Comment