vtkImageFFT 和 vtkImageRFFT

94 篇文章 6 订阅
72 篇文章 11 订阅
#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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值