[vtkusers] Moving through mha image slices using QvtkWidget and VtkImageViewer2

Vijaya Rani vijayarani.shanmugavadivu at appasamy.com
Thu Sep 24 07:25:58 EDT 2015


Hi
I am trying to display a mha image in a qvtkwidget based on the sample code
given in the following  link
http://www.vtk.org/Wiki/VTK/Examples/Cxx/IO/ReadDICOMSeries .I need to move
through the slices 
of the mha image with mouse wheel.I followed the link
http://public.kitware.com/pipermail/vtkusers/2014-October/085403.html and
tried to set the rendering 
environment.The problem is when i try to move through images by mouse wheel
the imageviewer is not setting the next slice to the qvtkwidget and
application is getting crashed. Below is my code and the output.
#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 <sstream>
#include <vtkActor.h>
#include <vtkDataSet.h>
#include <vtkDataSetMapper.h>
#include <vtkImageBlend.h>
#include <vtkImageData.h>
#include <vtkImageMapToColors.h>
#include <vtkLookupTable.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkImageViewer2.h>
#include <vtkMetaImageReader.h>
#include <vtkImageCast.h>
#include <vtkTransform.h>
#include <vtkMetaImageWriter.h>

// helper class to format slice status message
class StatusMessage {
public:
   static std::string Format(int slice, int maxSlice) {
      std::stringstream tmp;
      tmp << "Slice Number  " << slice + 1 << "/" << maxSlice + 1;
      return tmp.str();
   }
};

// Define own interaction style
class myVtkInteractorStyleImage : public vtkInteractorStyleImage
{
public:
   static myVtkInteractorStyleImage* New();
   vtkTypeMacro(myVtkInteractorStyleImage, vtkInteractorStyleImage)

protected:
   vtkImageViewer2* _ImageViewer;
   vtkTextMapper* _StatusMapper;
   int _Slice;
   int _MinSlice;
   int _MaxSlice;

public:
   void SetImageViewer(vtkImageViewer2* imageViewer) {
      _ImageViewer = imageViewer;
      _MinSlice = imageViewer->GetSliceMin();
      _MaxSlice = imageViewer->GetSliceMax();
      _Slice = _MinSlice;
      cout << "Slicer: Min = " << _MinSlice << ", Max = " << _MaxSlice <<
std::endl;
   }

   void SetStatusMapper(vtkTextMapper* statusMapper) {
      _StatusMapper = statusMapper;
   }


protected:
   void MoveSliceForward() {
       cout << "MoveSliceForward::Slice = " << _Slice <<"  "<<_MaxSlice<<
std::endl;
      if(_Slice < _MaxSlice) {
         _Slice += 1;
         cout << "MoveSliceForward::Slice = " << _Slice << std::endl;
         _ImageViewer->SetSlice(_Slice);
         std::string msg = StatusMessage::Format(_Slice, _MaxSlice);
         _StatusMapper->SetInput(msg.c_str());
         _ImageViewer->Render();
      }
   }

   void MoveSliceBackward() {
       cout << "MoveSliceBackward::Slice = " << _Slice <<"  "<<_MinSlice<<
std::endl;
      if(_Slice > _MinSlice) {
         _Slice -= 1;
         cout << "MoveSliceBackward::Slice = " << _Slice << std::endl;
         _ImageViewer->SetSlice(_Slice);
         std::string msg = StatusMessage::Format(_Slice, _MaxSlice);
         _StatusMapper->SetInput(msg.c_str());
         _ImageViewer->Render();
      }
   }

   virtual void OnMouseWheelForward() {
      std::cout << "Scrolled mouse wheel forward." << std::endl;
      MoveSliceForward();
   }

   virtual void OnMouseWheelBackward() {
      std::cout << "Scrolled mouse wheel backward." << std::endl;
      MoveSliceBackward();
   }
};
vtkStandardNewMacro(myVtkInteractorStyleImage)


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    vtkSmartPointer<vtkLookupTable> imgFirstColorMap =
    vtkSmartPointer<vtkLookupTable>::New();
    imgFirstColorMap->SetRange( 0.0, 255.0 );
    imgFirstColorMap->SetHueRange( 1.0, .1 );
    imgFirstColorMap->SetValueRange( 0.4, 0.8 );
    imgFirstColorMap->Build();

    vtkSmartPointer<vtkMetaImageReader> reader =
vtkSmartPointer<vtkMetaImageReader>::New();
    reader->SetFileName("/home/user/Desktop/sample.mha");
    reader->Update();

    vtkSmartPointer<vtkImageMapToColors> firstColorMapper = 
vtkSmartPointer<vtkImageMapToColors>::New();
    firstColorMapper->SetInputConnection( reader->GetOutputPort() );
    firstColorMapper->SetLookupTable( imgFirstColorMap );

    vtkSmartPointer<vtkImageViewer2> imageViewer = 
vtkSmartPointer<vtkImageViewer2>::New();

    vtkRenderWindow* renwin = ui->qvtkWidget->GetRenderWindow();

    vtkRenderWindowInteractor* iact =renwin->GetInteractor();

    vtkSmartPointer<myVtkInteractorStyleImage> myInteractorStyle
=vtkSmartPointer<myVtkInteractorStyleImage>::New();


    imageViewer->SetRenderWindow(renwin);
    imageViewer->SetupInteractor(iact);


    imageViewer->SetInputConnection(firstColorMapper->GetOutputPort());
    myInteractorStyle->SetImageViewer(imageViewer);
    iact->SetInteractorStyle(myInteractorStyle);
    iact->SetRenderWindow(imageViewer->GetRenderWindow());
    iact->Initialize();
    iact->Start();
    imageViewer->Render();
    imageViewer->GetRenderer()->ResetCamera();
    imageViewer->Render();
}

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


Slicer: Min = 0, Max = 127
ERROR: In /home/linuxadmin/VTK5.10.1/GUISupport/Qt/QVTKInteractor.cxx, line
147
QVTKInteractor (0x1c861e0): QVTKInteractor cannot control the event loop.

Scrolled mouse wheel forward.
MoveSliceForward::Slice = 0  127
MoveSliceForward::Slice = 1
The program has unexpectedly finished.


Kindly help to rectify the issue and need to know whether the qvtkwidget and
image viewer rendering is correct.




--
View this message in context: http://vtk.1045678.n5.nabble.com/Moving-through-mha-image-slices-using-QvtkWidget-and-VtkImageViewer2-tp5734108.html
Sent from the VTK - Users mailing list archive at Nabble.com.


More information about the vtkusers mailing list