VTK/Examples/Cxx/Qt/ImageDataToQImage

From KitwarePublic

Jump to: navigation, search

Contents

ImageDataToQImage.cxx

#include <vtkImageData.h>
#include <vtkSmartPointer.h>
 
#include <QColor>
#include <QImage>
 
#include <QApplication>
#include "SimpleViewUI.h"
 
int main(int argc, char *argv[])
{
  QApplication app(argc, argv);
 
  SimpleView mySimpleView;
  mySimpleView.show();
 
  return app.exec();
 
}

SimpleView.h

#ifndef SimpleView_H
#define SimpleView_H
 
#include "ui_SimpleViewUI.h"
 
#include <QMainWindow>
 
class SimpleView : public QMainWindow, private Ui::SimpleView
{
  Q_OBJECT
 
public:
  SimpleView();
 
public slots:
 
  void on_btnDisplayImage_clicked();
 
};
 
#endif

SimpleView.cxx

#include "SimpleViewUI.h"
 
#include <vtkImageData.h>
#include <vtkSmartPointer.h>
 
// Constructor
SimpleView::SimpleView()
{
  this->setupUi(this);
 
};
 
QImage vtkImageDataToQImage(vtkImageData* imageData);
 
void SimpleView::on_btnDisplayImage_clicked()
{
  // Create an image data
  vtkSmartPointer<vtkImageData> image = vtkSmartPointer<vtkImageData>::New();
  image->SetDimensions(50,50,1);
  image->SetNumberOfScalarComponents(3);
  image->SetScalarTypeToUnsignedChar();
  image->AllocateScalars();
 
  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;
      }
    }
 
  QGraphicsScene* scene = new QGraphicsScene();
 
  this->graphicsView->setScene(scene);
 
  QImage qImage = vtkImageDataToQImage(image);
  scene->addPixmap(QPixmap::fromImage(qImage));
 
}
 
QImage vtkImageDataToQImage(vtkImageData* imageData)
{
  if (!imageData)
    {
    return QImage();
    }
  imageData->Update();
  /// \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());
  // mirror vertically
  for(int row = 0; row < height; ++row)
    {
    for (int col = 0; col < width; ++col)
      {
      // Swap rgb
      *(rgbPtr++) = QColor(colorsPtr[0], colorsPtr[1], colorsPtr[2]).rgb();
      colorsPtr +=  3;
      }
    rgbPtr -= width * 2;
    }
  return image;
}

SimpleView.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>SimpleView</class>
 <widget class="QMainWindow" name="SimpleView">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>541</width>
    <height>583</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>SimpleView</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QGraphicsView" name="graphicsView">
    <property name="geometry">
     <rect>
      <x>140</x>
      <y>110</y>
      <width>256</width>
      <height>192</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="btnDisplayImage">
    <property name="geometry">
     <rect>
      <x>40</x>
      <y>40</y>
      <width>121</width>
      <height>27</height>
     </rect>
    </property>
    <property name="text">
     <string>Display Image</string>
    </property>
   </widget>
  </widget>
  <action name="actionOpenFile">
   <property name="enabled">
    <bool>true</bool>
   </property>
   <property name="text">
    <string>Open File...</string>
   </property>
  </action>
  <action name="actionExit">
   <property name="text">
    <string>Exit</string>
   </property>
  </action>
  <action name="actionPrint">
   <property name="text">
    <string>Print</string>
   </property>
  </action>
  <action name="actionHelp">
   <property name="text">
    <string>Help</string>
   </property>
  </action>
  <action name="actionSave">
   <property name="text">
    <string>Save</string>
   </property>
  </action>
 </widget>
 <resources/>
 <connections/>
</ui>


CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
PROJECT(ImageDataToQImage)
 
FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})
 
 
FIND_PACKAGE(Qt4 REQUIRED)
INCLUDE(${QT_USE_FILE})
 
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}  ${CMAKE_CURRENT_SOURCE_DIR})
 
QT4_WRAP_UI(UISrcs SimpleViewUI.ui)
QT4_WRAP_CPP(MOCSrcs SimpleViewUI.h)
 
ADD_EXECUTABLE(ImageDataToQImage ImageDataToQImage.cxx SimpleViewUI.cxx ${UISrcs} ${MOCSrcs})
TARGET_LINK_LIBRARIES(ImageDataToQImage ${VTK_LIBRARIES} ${QT_LIBRARIES})
Personal tools