[vtkusers] Fwd: Slice Viewer, only black screen
Роман Глуховский
roman_glu at mail.ru
Mon Jun 23 07:24:09 EDT 2014
Anybody who can tell me whats wrong with my code???
Wed, 18 Jun 2014 02:06:35 +0400 от Роман Глуховский <roman_glu at mail.ru>:
>Sorry my fault.
>
>I use VTK 6.0.0, QT 4.8.1.
>
>I want to include a SliceViewer in my QT application, when i compile my programm i get no
>error. The programm loads the DICOM files from the folder, but i get only a black screen, with
>the correct number of slices.
>
>I take the SliceViewer from this examaple:
>http://www.vtk.org/Wiki/VTK/Examples/Cxx/IO/ReadDICOMSeries .
>The example works, but not in my application.
>
>Here is my code:
>------------------------------mainwindow.h-------------------------
>#ifndef MAINWINDOW_H
>#define MAINWINDOW_H
>
>#include <QMainWindow>
>
>// some standard vtk headers
>#include <vtkSmartPointer.h>
>#include <vtkObjectFactory.h>
>#include <vtkRenderWindow.h>
>#include <vtkRenderWindowInteractor.h>
>#include <vtkRenderer.h>
>#include <vtkActor.h>
>// headers needed for this example
>#include <vtkImageViewer2.h>
>#include <vtkDICOMImageReader.h>
>#include <vtkInteractorStyleImage.h>
>#include <vtkActor2D.h>
>#include <vtkTextProperty.h>
>#include <vtkTextMapper.h>
>// needed to easily convert int to std::string
>#include <sstream>
>
>
>namespace Ui {
>class MainWindow;
>}
>
>class MainWindow : public QMainWindow
>{
> Q_OBJECT
>
>public:
> explicit MainWindow(QWidget *parent = 0);
> ~MainWindow();
>
>private slots:
> void on_pushButton_clicked();
>
>private:
> Ui::MainWindow *ui;
>};
>
>
>#endif // MAINWINDOW_H
>
>
>----------------------mainwindow.cpp--------------------------------
>#include "mainwindow.h"
>#include "ui_mainwindow.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() {
> 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() {
> 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 OnKeyDown() {
> std::string key = this->GetInteractor()->GetKeySym();
> if(key.compare("Up") == 0) {
> //cout << "Up arrow key was pressed." << endl;
> MoveSliceForward();
> }
> else if(key.compare("Down") == 0) {
> //cout << "Down arrow key was pressed." << endl;
> MoveSliceBackward();
> }
> // forward event
> vtkInteractorStyleImage::OnKeyDown();
> }
>
>
> virtual void OnMouseWheelForward() {
> //std::cout << "Scrolled mouse wheel forward." << std::endl;
> MoveSliceForward();
> // don't forward events, otherwise the image will be zoomed
> // in case another interactorstyle is used (e.g. trackballstyle, ...)
> // vtkInteractorStyleImage::OnMouseWheelForward();
> }
>
>
> virtual void OnMouseWheelBackward() {
> //std::cout << "Scrolled mouse wheel backward." << std::endl;
> if(_Slice > _MinSlice) {
> MoveSliceBackward();
> }
> // don't forward events, otherwise the image will be zoomed
> // in case another interactorstyle is used (e.g. trackballstyle, ...)
> // vtkInteractorStyleImage::OnMouseWheelBackward();
> }
>};
>
>vtkStandardNewMacro(myVtkInteractorStyleImage);
>
>
>MainWindow::MainWindow(QWidget *parent) :
> QMainWindow(parent),
> ui(new Ui::MainWindow)
>{
> ui->setupUi(this);
>}
>
>MainWindow::~MainWindow()
>{
> delete ui;
>}
>
>void MainWindow::on_pushButton_clicked()
>{
> //std::string folder = argv[1];
> //std::string folder = "/home/joe/Daten/dicom-data/Angio";
> //std::string folder = "/home/joe/Daten/DicomTestImages";
>
> //std::cout << std::endl << "Die Daten werden aus dem Ordner: " << folder << " geladen" << std::endl;
> // Read all the DICOM files in the specified directory.
> vtkSmartPointer<vtkDICOMImageReader> reader =
> vtkSmartPointer<vtkDICOMImageReader>::New();
> reader->SetDirectoryName("/home/joe/Daten/dicom-data/Angio");
> reader->Update();
>
> char* fname = reader->GetDirectoryName();
> std::cout << std::endl << "Die Daten werden aus dem Ordner: " << fname << " geladen" << std::endl;
>
> // Visualize
> vtkSmartPointer<vtkImageViewer2> imageViewer =
> vtkSmartPointer<vtkImageViewer2>::New();
> imageViewer->SetInputConnection(reader->GetOutputPort());
>
> // slice status message
> vtkSmartPointer<vtkTextProperty> sliceTextProp = vtkSmartPointer<vtkTextProperty>::New();
> sliceTextProp->SetFontFamilyToCourier();
> sliceTextProp->SetFontSize(20);
> sliceTextProp->SetVerticalJustificationToBottom();
> sliceTextProp->SetJustificationToLeft();
>
> vtkSmartPointer<vtkTextMapper> sliceTextMapper = vtkSmartPointer<vtkTextMapper>::New();
> std::string msg = StatusMessage::Format(imageViewer->GetSliceMin(), imageViewer->GetSliceMax());
> sliceTextMapper->SetInput(msg.c_str());
> sliceTextMapper->SetTextProperty(sliceTextProp);
>
> vtkSmartPointer<vtkActor2D> sliceTextActor = vtkSmartPointer<vtkActor2D>::New();
> sliceTextActor->SetMapper(sliceTextMapper);
> sliceTextActor->SetPosition(15, 10);
>
> // usage hint message
> vtkSmartPointer<vtkTextProperty> usageTextProp = vtkSmartPointer<vtkTextProperty>::New();
> usageTextProp->SetFontFamilyToCourier();
> usageTextProp->SetFontSize(14);
> usageTextProp->SetVerticalJustificationToTop();
> usageTextProp->SetJustificationToLeft();
>
> vtkSmartPointer<vtkTextMapper> usageTextMapper = vtkSmartPointer<vtkTextMapper>::New();
> usageTextMapper->SetInput("- Slice with mouse wheel\n or Up/Down-Key\n- Zoom with pressed right\n mouse button while dragging");
> usageTextMapper->SetTextProperty(usageTextProp);
>
> vtkSmartPointer<vtkActor2D> usageTextActor = vtkSmartPointer<vtkActor2D>::New();
> usageTextActor->SetMapper(usageTextMapper);
> usageTextActor->GetPositionCoordinate()->SetCoordinateSystemToNormalizedDisplay();
> usageTextActor->GetPositionCoordinate()->SetValue( 0.05, 0.95);
>
> // create an interactor with our own style (inherit from vtkInteractorStyleImage)
> // in order to catch mousewheel and key events
> vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
> vtkSmartPointer<vtkRenderWindowInteractor>::New();
>
> vtkSmartPointer<myVtkInteractorStyleImage> myInteractorStyle =
> vtkSmartPointer<myVtkInteractorStyleImage>::New();
>
> // make imageviewer2 and sliceTextMapper visible to our interactorstyle
> // to enable slice status message updates when scrolling through the slices
> myInteractorStyle->SetImageViewer(imageViewer);
> myInteractorStyle->SetStatusMapper(sliceTextMapper);
>
> imageViewer->SetupInteractor(renderWindowInteractor);
> // make the interactor use our own interactorstyle
> // cause SetupInteractor() is defining it's own default interatorstyle
> // this must be called after SetupInteractor()
> renderWindowInteractor->SetInteractorStyle(myInteractorStyle);
> // add slice status message and usage hint message to the renderer
> imageViewer->GetRenderer()->AddActor2D(sliceTextActor);
> imageViewer->GetRenderer()->AddActor2D(usageTextActor);
>
> // initialize rendering and interaction
> //imageViewer->GetRenderWindow()->SetSize(400, 300);
> //imageViewer->GetRenderer()->SetBackground(0.2, 0.3, 0.4);
> imageViewer->Render();
> imageViewer->GetRenderer()->ResetCamera();
> imageViewer->Render();
> renderWindowInteractor->Start();
> return EXIT_SUCCESS;
>}
>
>---------------------------------main.cpp------------------
>#include <QtGui/QApplication>
>#include "mainwindow.h"
>
>int main(int argc, char *argv[])
>{
> QApplication a(argc, argv);
> MainWindow w;
> w.show();
>
> return a.exec();
>}
>
>--------------------------CMakeLists.txt------------------------
>
>cmake_minimum_required(VERSION 2.8)
>
>PROJECT(Test)
>
>FIND_PACKAGE( Qt4 REQUIRED )
>INCLUDE(${QT_USE_FILE})
>
>QT4_WRAP_UI(UISrcs mainwindow.ui)
>QT4_WRAP_CPP(MOCSrcs mainwindow.h)
>
>find_package(ITK REQUIRED)
>include(${ITK_USE_FILE})
>if (ITKVtkGlue_LOADED)
> find_package(VTK REQUIRED)
> include(${VTK_USE_FILE})
>else()
> find_package(ItkVtkGlue REQUIRED)
> include(${ItkVtkGlue_USE_FILE})
> set(Glue ItkVtkGlue)
>endif()
>add_executable(Test main.cpp mainwindow.cpp ${MOCSrcs} ${UISrcs})
>
>if(VTK_LIBRARIES)
> target_link_libraries(Test
> ${Glue} ${VTK_LIBRARIES} ${ITK_LIBRARIES} ${QT_LIBRARIES})
>else()
> target_link_libraries(Test vtkHybrid)
>endif()
>-------------------------------------------------------------------------------------------------
>
>
>I hope thats enough informations and somebody can help me.
>
>Thanks a lot.
>
>
>
>Tue, 17 Jun 2014 12:18:06 -0400 (EDT) от David Cole <dlrdave at aol.com>:
>>> Can nobody tell me, why i get a black screen?
>>
>>Maybe you could send some code that shows us what you're doing. It's
>>kind of hard to tell you what's wrong without looking at some code...
>>There are any number of things that could cause a "black screen".
>>
>>Do you get any errors reported? What version of VTK are you using?
>>
>
>_______________________________________________
>Powered by www.kitware.com
>
>Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
>Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
>
>Follow this link to subscribe/unsubscribe:
>http://public.kitware.com/mailman/listinfo/vtkusers
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20140623/27986025/attachment.html>
More information about the vtkusers
mailing list