#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle)
#include <vtkSmartPointer.h>
class vtkRenderer;
class vtkImageViewer2;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void openFileSlot();
private:
vtkSmartPointer<vtkRenderer> pRenderer;
vtkSmartPointer<vtkImageViewer2> pImageViewer;
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QVTKWidget.h>
#include <vtkImageViewer2.h>
#include <vtkJPEGReader.h>
#include <vtkPNGReader.h>
#include <vtkMedicalImageReader2.h>
#include <vtkMetaImageReader.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkImageActor.h>
#include <QFileDialog>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
pImageViewer = vtkSmartPointer<vtkImageViewer2>::New();
pRenderer = vtkSmartPointer<vtkRenderer>::New();
ui->qvtkWidget->GetRenderWindow()->AddRenderer(pRenderer);
connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(openFileSlot()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::openFileSlot()
{
//#if 1
// QString selectFilePath = QFileDialog::getOpenFileName(this, QString("选择图像文件"), QString(""), QString("图像(*.jpeg)"));
//#else
// QString selectFilePath = QFileDialog::getOpenFileName(this, QString("选择图像文件"), QString(""), QString("图像(*.png)"));
//#endif
QString selectFilePath = QFileDialog::getOpenFileName(this, QString("选择图像文件"), QString(""), QString("图像(*.mhd)"));
if(selectFilePath.isEmpty())
{
ui->textBrowser->append("选择图像路径为空!");
return ;
}
//#if 1
// vtkSmartPointer<vtkJPEGReader> jpegReader = vtkSmartPointer<vtkJPEGReader>::New();
//#else
// vtkSmartPointer<vtkPNGReader> jpegReader = vtkSmartPointer<vtkPNGReader>::New();
//#endif
vtkSmartPointer<vtkMetaImageReader> medicalReader = vtkSmartPointer<vtkMetaImageReader>::New();
medicalReader->SetFileName(selectFilePath.toStdString().c_str());
//
#if 0
// 不调用update时,调用GetOutput()函数没有数据
medicalReader->Update();
pImageViewer->SetInputData(medicalReader->GetOutput());
#else
pImageViewer->SetInputConnection(medicalReader->GetOutputPort());
#endif
pImageViewer->UpdateDisplayExtent();
pImageViewer->SetRenderWindow(ui->qvtkWidget->GetRenderWindow());
pImageViewer->SetRenderer(pRenderer);
pImageViewer->SetupInteractor(ui->qvtkWidget->GetInteractor());
pImageViewer->SetSliceOrientationToXY();
pImageViewer->GetImageActor()->InterpolateOn();
pImageViewer->SetColorLevel(500);
pImageViewer->SetColorWindow(2000);
pImageViewer->SetSlice(40);
pRenderer->ResetCamera();
pRenderer->DrawOn();
ui->qvtkWidget->GetRenderWindow()->Render();
ui->textBrowser->append(QString("载入图像:") + selectFilePath + QString(" 成功 !"));
}
QT UI 文件如下(使用时,将部分内容粘贴到文本文件中,并将格式改为.ui即可):
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>649</width>
<height>465</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QHBoxLayout" name="horizontalLayout" stretch="2,5">
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>选择图像</string>
</property>
</widget>
</item>
<item>
<widget class="QTextBrowser" name="textBrowser"/>
</item>
</layout>
</item>
<item>
<widget class="QVTKWidget" name="qvtkWidget"/>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>649</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
<customwidget>
<class>QVTKWidget</class>
<extends>QWidget</extends>
<header>QVTKWidget.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
来源:oschina
链接:https://my.oschina.net/u/4394252/blog/4906820