#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFileDialog>
#include <QDebug>
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle)
#include <vtkSmartPointer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkJPEGReader.h>
#include <vtkImageData.h>
#include <vtkImageLuminance.h>
#include <vtkImageActor.h>
#include <vtkImageThreshold.h>
#include <vtkInteractorStyleImage.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void openFileSlot();
private:
vtkSmartPointer<vtkRenderer> pSrcRenderer;
vtkSmartPointer<vtkRenderer> pFFTRenderer;
vtkSmartPointer<vtkRenderer> pRFFTRenderer;
vtkSmartPointer<vtkImageActor> pSrcImageActor;
vtkSmartPointer<vtkImageActor> pFFTImageActor;
vtkSmartPointer<vtkImageActor> pRFFTImageActor;
vtkSmartPointer<vtkInteractorStyleImage> pImageStyle;
vtkSmartPointer<vtkJPEGReader> pJpegReader;
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <vtkImageGradient.h>
#include <vtkImageMagnitude.h>
#include <vtkImageShiftScale.h>
#include <vtkImageSobel2D.h>
#include <vtkImageExtractComponents.h>
#include <vtkImageMathematics.h>
#include <vtkImageLuminance.h>
#include <vtkImageCast.h>
#include <vtkImageFFT.h>
#include <vtkImageRFFT.h>
#include <vtkImageExtractComponents.h>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
pSrcRenderer = vtkSmartPointer<vtkRenderer>::New();
pSrcRenderer->SetBackground(1.0, 0, 0);
pSrcRenderer->SetViewport(0, 0, 0.33, 1.0);
pFFTRenderer = vtkSmartPointer<vtkRenderer>::New();
pFFTRenderer->SetBackground(1.0, 0, 0);
pFFTRenderer->SetViewport(0.33, 0, 0.66, 1.0);
pRFFTRenderer = vtkSmartPointer<vtkRenderer>::New();
pRFFTRenderer->SetBackground(1.0, 0, 0);
pRFFTRenderer->SetViewport(0.66, 0, 1.0, 1.0);
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 ;
}
else
{
ui->textBrowser->append(selectFilePath);
}
pJpegReader = vtkSmartPointer<vtkJPEGReader>::New();
pJpegReader->SetFileName(selectFilePath.toStdString().c_str());
pJpegReader->Update();
int dims[3];
pJpegReader->GetOutput()->GetDimensions(dims);
qDebug() << "dims = " << dims[0] << ", " << dims[1] << ", " << dims[2];
vtkSmartPointer<vtkImageLuminance> pImageLuminance = vtkSmartPointer<vtkImageLuminance>::New();
pImageLuminance->SetInputData(pJpegReader->GetOutput());
pImageLuminance->Update();
pSrcImageActor = vtkSmartPointer<vtkImageActor>::New();
pSrcImageActor->SetInputData(pImageLuminance->GetOutput());
pSrcRenderer->AddActor(pSrcImageActor);
ui->qvtkWidget->GetRenderWindow()->AddRenderer(pSrcRenderer);
// 数据类型 unsigned char => float
vtkSmartPointer<vtkImageCast> pImageCast1 = vtkSmartPointer<vtkImageCast>::New();
pImageCast1->SetInputData(pImageLuminance->GetOutput());
pImageCast1->SetOutputScalarTypeToFloat();
pImageCast1->Update();
// 进行FFT变换
vtkSmartPointer<vtkImageFFT> pImageFFT = vtkSmartPointer<vtkImageFFT>::New();
pImageFFT->SetInputData(pImageCast1->GetOutput());
pImageFFT->Update();
// 抽取部分
vtkSmartPointer<vtkImageExtractComponents> pImageExtract1 = vtkSmartPointer<vtkImageExtractComponents>::New();
pImageExtract1->SetComponents(0);
pImageExtract1->SetInputData(pImageFFT->GetOutput());
pImageExtract1->Update();
// float => unsigned char
vtkSmartPointer<vtkImageCast> pImageCast2 = vtkSmartPointer<vtkImageCast>::New();
pImageCast2->SetOutputScalarTypeToUnsignedChar();
pImageCast2->SetInputData(pImageExtract1->GetOutput());
pImageCast2->Update();
pFFTImageActor = vtkSmartPointer<vtkImageActor>::New();
pFFTImageActor->SetInputData(pImageCast2->GetOutput());
pFFTRenderer->AddActor(pFFTImageActor);
ui->qvtkWidget->GetRenderWindow()->AddRenderer(pFFTRenderer);
// 逆FFT变换
vtkSmartPointer<vtkImageRFFT> pImageRFFT = vtkSmartPointer<vtkImageRFFT>::New();
pImageRFFT->SetInputData(pImageFFT->GetOutput());
pImageRFFT->Update();
vtkSmartPointer<vtkImageExtractComponents> pImageExtract2 = vtkSmartPointer<vtkImageExtractComponents>::New();
pImageExtract2->SetComponents(0);
pImageExtract2->SetInputData(pImageRFFT->GetOutput());
pImageExtract2->Update();
vtkSmartPointer<vtkImageCast> pImageCast3 = vtkSmartPointer<vtkImageCast>::New();
pImageCast3->SetOutputScalarTypeToUnsignedChar();
pImageCast3->SetInputData(pImageExtract2->GetOutput());
pImageCast3->Update();
pRFFTImageActor = vtkSmartPointer<vtkImageActor>::New();
pRFFTImageActor->SetInputData(pImageCast3->GetOutput());
pRFFTRenderer->AddActor(pRFFTImageActor);
ui->qvtkWidget->GetRenderWindow()->AddRenderer(pRFFTRenderer);
pImageStyle = vtkSmartPointer<vtkInteractorStyleImage>::New();
ui->qvtkWidget->GetInteractor()->SetInteractorStyle(pImageStyle);
ui->qvtkWidget->GetRenderWindow()->Render();
}
<?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>400</width>
<height>300</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>400</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>