How to use Dcmtk in Qt?

Mahtab picture Mahtab · Feb 19, 2011 · Viewed 9.7k times · Source

I am working on a project where I have to read a dicom image.

I am unable to install dcmtk. I am using win7 64-bit and vs2010.

Please explain the procedure to include dcmtk in my program.

Answer

well picture well · Sep 3, 2012

To use the Windows + DCMTK + QT, you need to execute these follow steps:

  • Compile the DCMTK (Step 1.A)
  • Create your sample application (Step 2)
  • Create your QT project file referring the compiled/installed DCMTK Libs (Step 3.B)
  • Compile your application in your IDE (Step 4.B)

If you are reading this and don't want to use the Qt, I am answering a version without Qt as well.

Windows+VisualStudio+DCMTK: Steps 1.A, 2, 3.A and 4.A

Linux+GCC+DCMTK: Steps 1.B, 2, N/A, 4.C


1) Compile the DCMTK

First of all, to use the DCMTK library in your application, you should compile the DCMTK source code to generate the libraries:

While I am writing this, the last available version is 3.6.0. So, we should download it:

ftp://dicom.offis.de/pub/dicom/offis/software/dcmtk/dcmtk360/dcmtk-3.6.0.zip

After the download is finished, you need to unzip the folder. The DCMTK source doesn't have a project file, but it is not a problem, there is a CMakelist file responsible for generating a project file in your desirable Operational System/Compiler. If you are not familiar with CMake tool, you can read more here (https://cmake.org/)

1.A) Compiling on Windows/Visual Studio 2012

1.A.1) You need to have an installed compiler, in my case, it was the Visual Studio 2012

1.A.2) Run the CMake tool to generate a project file to Visual Studio 2012. You need be able to fill the source DCMTK directory.

1.A.3) Now, execute the VisualStudio 2012, open the file sln created in the previous step (2) and compile the target ALL_BUILD

1.A.4) Re-Execute VisualStudio in Admin mode (because of permission C:\Program Files) to compile the target INSTALL (it will copy and install the DCMTK to default path: C:/Program Files/DCMTK/, we can reference it such PATH_WHERE_DCMTK_WAS_INSTALLED)

(1.B) Compiling on GNU/Linux GCC

I have tested at Ubuntu/CentOS. The first, you should go to DCMTK Source and run these following three commands:

$ ./configure --prefix=path_to_dcmtk
$ make all
$ sudo make install

example: path_to_dcmtk = /home/user/dcmtk


2) Creating your sample application

Create a file called your_sample/testapp.cxx with the following content. This a demonstration found in DCMTK Forum to open a DICOM file and print the patient's name.

#include "dcmtk/dcmdata/dctk.h"
#include <iostream>
using namespace std;

int main()
{
   DcmFileFormat fileformat;
   OFCondition status = fileformat.loadFile("test.dcm");
   if (status.good())
   {
      OFString patientsName;
      if (fileformat.getDataset()->findAndGetOFString(DCM_PatientName, patientsName).good())
      {
         cout << "Patient's Name: " << patientsName << endl;
      }else{
         cerr << "Error: cannot access Patient's Name!" << endl;
      }

   }else{
      cerr << "Error: cannot read DICOM file (" << status.text() << ")" << endl;
   }
   return 0;
}

3) Creating the Application Project using the VisuaStudio as Compiler

The created file at the previous step needs to be placed in a project. You can choose between the option Windows VisualStudio (3.1) and Windows Qt (3.2). If you are using Linux, you can skip this step.

3.A) Windows with Visual Studio 2012 IDE

To create a Visual Studio project, you can use the Wizard and set the necessary settings such: Linker, Libraries, etc. However, to make easier on this answer, I will use the CMakeList.txt to create a Project for Visual Studio 2012. So, please, create a file called your_sample/CmakeList.txt with the following content:

PROJECT(testapp)

SET(DCMTK_DIR ABSOLUTE_PATH_WHERE_DCMTK_WAS_INSTALLED)
#an example: SET(DCMTK_DIR "C:\\Users\\test\\test_dcmtk\\DCMTK")

# settings for Microsoft Visual C++ 6
SET(CMAKE_C_FLAGS "/nologo /W3 /GX /Gy /YX")
SET(CMAKE_C_FLAGS_DEBUG "/MTd /Z7 /Od")
SET(CMAKE_C_FLAGS_RELEASE "/MT /O2")
SET(CMAKE_CXX_FLAGS "/nologo /W3 /GX /Gy /YX")
SET(CMAKE_CXX_FLAGS_DEBUG "/MTd /Z7 /Od")
SET(CMAKE_CXX_FLAGS_RELEASE "/MT /O2")

ADD_DEFINITIONS(-D_REENTRANT)

INCLUDE_DIRECTORIES(${DCMTK_DIR}/include)
LINK_DIRECTORIES(${DCMTK_DIR}/lib)

ADD_EXECUTABLE(testapp testapp)
TARGET_LINK_LIBRARIES(testapp netapi32 wsock32 ofstd dcmdata)

3.B) Windows QtCreator IDE using the VisuaStudio as Compiler

To create a project file for QtCreator IDE. You need to create a file called your_sample/my_project.pro with the following content:

SOURCES += testapp.cxx
CONFIG += debug console
DEFINES += _REENTRANT

QMAKE_CFLAGS_RELEASE -= -MD
QMAKE_CFLAGS_RELEASE = -MT
QMAKE_CFLAGS_DEBUG -= -MDd
QMAKE_CFLAGS_DEBUG = -MTd
QMAKE_CXXFLAGS_RELEASE -= -MD
QMAKE_CXXFLAGS_RELEASE += -MT
QMAKE_CXXFLAGS_DEBUG -= -MDd
QMAKE_CXXFLAGS_DEBUG += -MTd

INCLUDEPATH += (RELATIVE_PATH_WHERE_DCMTK_WAS_INSTALLED)/include
#an example: INCLUDEPATH += ../../../test_dcmtk/DCMTK/include

LIBS += -L"(RELATIVE_PATH_WHERE_YOU_INSTALLED_DCMTK)/lib" \
-ladvapi32 \
-ldcmdata \
-loflog \
-lofstd \
-lws2_32 \
-lnetapi32 \
-lwsock32
#an example: LIBS += -L"../../../test_dcmtk/DCMTK/lib" \

4.A) Windows with Visual Studio 2012 IDE

Open the project file at VisualStudio and click on Build.

4.B) Windows with QtCreator IDE using the VisuaStudio as Compiler

Open the project file at QT and click on Build.

4.C) GNU/Linux - Command Line with GCC

g++ testapp.cxx -DHAVE_CONFIG_H -I/path_to_dcmtk/include -L/path_to_dcmtk/lib -pthread -ldcmdata -lz -loflog -lofstd -o main

Please, note that whether you have compiled the libraries in DEBUG mode, your application should be compiled in DEBUG mode as well.


References