Skip to content


# ImageDataToQImage

VTKEx/Cxx/Qt/ImageDataToQImage

### Description

This example shows how a vtkImageData can be converted into a QImage.

Seealso

the QImageToImageSource example.

Question

If you have a simple question about this example contact us at VTKExProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum

### Code

ImageDataToQImage.cxx

    #include <vtkImageData.h>
    #include <vtkSmartPointer.h>

    #include <QApplication>
    #include <QColor>
    #include <QImage>

    // Create a green 50x50 imageData for demonstration purposes
    vtkSmartPointer<vtkImageData> createDemoImageData() {
      vtkSmartPointer<vtkImageData> image = vtkSmartPointer<vtkImageData>::New();
      image->SetDimensions(50, 50, 1);
      image->AllocateScalars(VTK_UNSIGNED_CHAR, 3);

      int width = image->GetDimensions()[0];
      int height = image->GetDimensions()[1];

      for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
          unsigned char *pixel =
              static_cast<unsigned char *>(image->GetScalarPointer(x, y, 0));
          pixel[0] = 0;
          pixel[1] = 255;
          pixel[2] = 0;
        }
      }

      return image;
    }

    // The actual conversion code
    QImage vtkImageDataToQImage(vtkSmartPointer<vtkImageData> imageData) {
      if (!imageData) {
        return QImage();
      }

      /// \todo retrieve just the UpdateExtent
      int width = imageData->GetDimensions()[0];
      int height = imageData->GetDimensions()[1];
      QImage image(width, height, QImage::Format_RGB32);
      QRgb *rgbPtr = reinterpret_cast<QRgb *>(image.bits()) + width * (height - 1);
      unsigned char *colorsPtr =
          reinterpret_cast<unsigned char *>(imageData->GetScalarPointer());

      // Loop over the vtkImageData contents.
      for (int row = 0; row < height; row++) {
        for (int col = 0; col < width; col++) {
          // Swap the vtkImageData RGB values with an equivalent QColor
          *(rgbPtr++) = QColor(colorsPtr[0], colorsPtr[1], colorsPtr[2]).rgb();
          colorsPtr += imageData->GetNumberOfScalarComponents();
        }

        rgbPtr -= width * 2;
      }

      return image;
    }

    int main(int argc, char *argv[]) {
      QApplication app(argc, argv);

      QImage qimage = vtkImageDataToQImage(createDemoImageData());
      qimage.save("qimage.png");

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(ImageDataToQImage)

    find_package(VTK COMPONENTS
      vtkvtkCommonCore
      vtkvtkCommonDataModel QUIET)
    if (NOT VTK_FOUND)
      message("Skipping ImageDataToQImage: ${VTK_NOT_FOUND_MESSAGE}")
      return ()
    endif()
    message (STATUS "VTK_VERSION: ${VTK_VERSION}")
    if (VTK_VERSION VERSION_LESS "8.90.0")
      # old system
      include(${VTK_USE_FILE})
      add_executable(ImageDataToQImage MACOSX_BUNDLE ImageDataToQImage.cxx )
      target_link_libraries(ImageDataToQImage PRIVATE ${VTK_LIBRARIES})
    else ()
      # include all components
      add_executable(ImageDataToQImage MACOSX_BUNDLE ImageDataToQImage.cxx )
      target_link_libraries(ImageDataToQImage PRIVATE ${VTK_LIBRARIES})
      # vtk_module_autoinit is needed
      vtk_module_autoinit(
        TARGETS ImageDataToQImage
        MODULES ${VTK_LIBRARIES}
        )
    endif ()

### Download and Build ImageDataToQImage

Click here to download ImageDataToQImage and its CMakeLists.txt file. Once the tarball ImageDataToQImage.tar has been downloaded and extracted,

    cd ImageDataToQImage/build

If VTK is installed:

    cmake ..

If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:

    cmake -DVTK_DIR:PATH=/home/me/vtk_build ..

Build the project:

    make

and run it:

    ./ImageDataToQImage

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.