#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle)
#include
#include
#include
#include
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void openFileSlot();
private:
vtkSmartPointer pRenderer0;
vtkSmartPointer pRenderer1;
vtkSmartPointer pRenderer2;
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
pRenderer0 = vtkSmartPointer::New();
pRenderer0->SetBackground(1, 0, 0);
pRenderer0->SetViewport(0, 0, 0.33, 1);
pRenderer1 = vtkSmartPointer::New();
pRenderer1->SetBackground(0, 1, 0);
pRenderer1->SetViewport(0.33, 0, 0.66, 1);
pRenderer2 = vtkSmartPointer::New();
pRenderer2->SetBackground(0, 0, 1);
pRenderer2->SetViewport(0.66, 0, 1, 1);
ui->qvtkWidget->GetRenderWindow()->AddRenderer(pRenderer0);
ui->qvtkWidget->GetRenderWindow()->AddRenderer(pRenderer1);
ui->qvtkWidget->GetRenderWindow()->AddRenderer(pRenderer2);
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("图像(*.jpg)"));
#else
QString selectFilePath = QFileDialog::getOpenFileName(this, QString("选择图像文件"), QString(""), QString("图像(*.png)"));
#endif
if(selectFilePath.isEmpty())
{
ui->textBrowser->append("选择图像路径为空!");
return ;
}
// 原始图像
#if 1
vtkSmartPointer pJpegReader = vtkSmartPointer::New();
#else
vtkSmartPointer pJpegReader = vtkSmartPointer::New();
#endif
pJpegReader->SetFileName(selectFilePath.toStdString().c_str());
pJpegReader->Update();
ui->textBrowser->append(QString("载入图像:") + selectFilePath + QString(" 成功 !"));
// vtkImageData
{
// 获取图像信息
int dims[3];
pJpegReader->GetOutput()->GetDimensions(dims);
ui->textBrowser->append(QString("图像维数:") + QString::number(dims[0]) + ", " + QString::number(dims[1]) + ", " + QString::number(dims[2]));
// 获取图像原点
double origin[3];
pJpegReader->GetOutput()->GetOrigin(origin);
ui->textBrowser->append(QString("图像原点:") + QString::number(origin[0]) + ", " + QString::number(origin[1]) + ", " + QString::number(origin[2]));
// 获取图像的间隔
double spaceing[3];
pJpegReader->GetOutput()->GetSpacing(spaceing);
ui->textBrowser->append(QString("图像像素点间隔:") + QString::number(spaceing[0]) + ", " + QString::number(spaceing[1]) + ", " + QString::number(spaceing[2]));
vtkSmartPointer pJpegActor = vtkSmartPointer::New();
pJpegActor->SetInputData(pJpegReader->GetOutput());
pRenderer0->AddActor(pJpegActor);
}
// vtkChangeImageInformation
{
// 图像信息进行修改
vtkSmartPointer pJpegChange = vtkSmartPointer::New();
pJpegChange->SetInputData(pJpegReader->GetOutput());
pJpegChange->SetOutputOrigin(100, 100, 0);
pJpegChange->SetOutputSpacing(5, 5, 1);
pJpegChange->SetCenterImage(1);
pJpegChange->Update();
// 获取图像信息
int dims[3];
pJpegChange->GetOutput()->GetDimensions(dims);
ui->textBrowser->append(QString("修改后图像维数:") + QString::number(dims[0]) + ", " + QString::number(dims[1]) + ", " + QString::number(dims[2]));
// 获取图像原点
double origin[3];
pJpegChange->GetOutput()->GetOrigin(origin);
ui->textBrowser->append(QString("修改后图像原点:") + QString::number(origin[0]) + ", " + QString::number(origin[1]) + ", " + QString::number(origin[2]));
// 获取图像的间隔
double spaceing[3];
pJpegChange->GetOutput()->GetSpacing(spaceing);
ui->textBrowser->append(QString("修改后图像像素点间隔:") + QString::number(spaceing[0]) + ", " + QString::number(spaceing[1]) + ", " + QString::number(spaceing[2]));
}
// 访问图像点
{
int dims[3];
pJpegReader->GetOutput()->GetDimensions(dims);
// 深拷贝图像
vtkSmartPointer pCopyImage = vtkSmartPointer::New();
pCopyImage->DeepCopy(pJpegReader->GetOutput());
// 非管线,不需要update
for(int k = 0; k < dims[2]; ++k)
{
for(int i = 0; i < dims[0]; ++i)
{
for(int j = 0; j < dims[1]; ++j)
{
if(i < 100 && j < 100)
{
unsigned char* pPixel = (unsigned char*)(pCopyImage->GetScalarPointer(i, j, k));
*(pPixel + 0) = 0;
*(pPixel + 1) = 0;
*(pPixel + 2) = 0;
}
}
}
}
vtkSmartPointer pCopyActor = vtkSmartPointer::New();
pCopyActor->SetInputData(pCopyImage);
pRenderer1->AddActor(pCopyActor);
}
// 使用迭代器遍历图像点
{
int dims[3];
pJpegReader->GetOutput()->GetDimensions(dims);
// 深拷贝图像
vtkSmartPointer pCopyImage = vtkSmartPointer::New();
pCopyImage->DeepCopy(pJpegReader->GetOutput());
// 非管线,不需要update
int region[] = {dims[0] / 4, dims[0] * 3 / 4, dims[1] / 4, dims[1] * 3 / 4};
vtkImageIterator pImageIterator(pCopyImage, region);
while(!pImageIterator.IsAtEnd())
{
auto iterBegin = pImageIterator.BeginSpan();
auto iterEnd = pImageIterator.EndSpan();
for(auto iter = iterBegin; iter != iterEnd; ++iter)
{
*iter = 255 - *iter;
}
pImageIterator.NextSpan();
}
vtkSmartPointer pCopyActor = vtkSmartPointer::New();
pCopyActor->SetInputData(pCopyImage);
pRenderer2->AddActor(pCopyActor);
}
// 不能在MainWidow的构造函数中
vtkSmartPointer style = vtkSmartPointer::New();
ui->qvtkWidget->GetRenderWindow()->GetInteractor()->SetInteractorStyle(style);
pRenderer0->ResetCamera();
pRenderer1->ResetCamera();
pRenderer2->ResetCamera();
ui->qvtkWidget->GetRenderWindow()->Render();
}
QT UI 文件
MainWindow
0
0
400
300
MainWindow
-
-
选择图像
-
-
0
0
400
22
TopToolBarArea
false
QVTKWidget
QWidget
QVTKWidget.h