Qt VTK显示单张DICOM图像并调节窗宽窗位

开发环境:

  • Qt 5.12.7( & Visual Studio 2017)
  • VTK 8.2.0

源代码:

#include <QtWidgets/QApplication>

#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle)
VTK_MODULE_INIT(vtkRenderingFreeType)
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2)

#include <QVTKOpenGLNativeWidget.h>
#include <vtkDICOMImageReader.h>
#include <vtkGenericOpenGLRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkImageSlice.h>
#include <vtkImageResliceMapper.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkNew.h>

const char* dcmFileName = "G:/DICOM/DCM/009/I1270.dcm";

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QVTKOpenGLNativeWidget w;

    vtkNew<vtkDICOMImageReader> reader;
    vtkNew<vtkGenericOpenGLRenderWindow> renderWindow;
    vtkNew<vtkRenderer> renderer;
    vtkNew<vtkImageSlice> imageSlice;
    vtkNew<vtkImageResliceMapper> mapper;
    vtkNew<vtkInteractorStyleImage> interactorStyle;    // 用于响应鼠标事件以调节窗宽窗位

    reader->SetFileName(dcmFileName);
    reader->Update();
    vtkImageData* imageData = reader->GetOutput();

    mapper->SetInputData(imageData);
    imageSlice->SetMapper(mapper);
    renderer->AddViewProp(imageSlice);
    renderWindow->AddRenderer(renderer);
    w.SetRenderWindow(renderWindow);
    w.GetInteractor()->SetInteractorStyle(interactorStyle);

    renderer->ResetCamera();
    w.show();

    return a.exec();
}

运行效果:

鼠标左键按住不放,上下移动调整窗位,左右移动调整窗宽

鼠标右键按住不放,上下移动放大缩小,或者使用鼠标滚轮

 
 
扩展:如果不想使用上述VTK默认方式调节调节窗位,同时也不想让图像显示成三维模式(可以旋转),那么需要继承vtkInteractorStyleImage类,将虚函数virtual void WindowLevel()重写,如下代码所示:
//
// QVtkInteractorStyleImage.h
//
#pragma once

#include <vtkInteractorStyleImage.h>

class QVtkInteractorStyleImage : public vtkInteractorStyleImage
{
public:
    static QVtkInteractorStyleImage *New();
    vtkTypeMacro(QVtkInteractorStyleImage, QVtkInteractorStyleImage);

    virtual void WindowLevel() override;

protected:
    QVtkInteractorStyleImage() = default;
    ~QVtkInteractorStyleImage() = default;

};
//
// QVtkInteractorStyleImage.cpp
//
#include "QVtkInteractorStyleImage.h"
#include <vtkObjectFactory.h>

vtkStandardNewMacro(QVtkInteractorStyleImage);

void QVtkInteractorStyleImage::WindowLevel()
{

}

设置窗宽窗位接口:

imageSlice->GetProperty()->SetColorLevel(/*double*/val);   // 窗位
imageSlice->GetProperty()->SetColorWindow(/*double*/val);  // 窗宽

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页