首页 > 学院 > 开发设计 > 正文

ITK、VTK、Qt读取与显示DICOM文件------代码部分

2019-11-06 06:08:00
字体:
来源:转载
供稿:网友
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageToVTKImageFilter.h"
#include <itkGDCMImageIO.h>    //此处使用尖括号更好
#include "itkMetaDataDictionary.h"
#include "itkMetaDataObject.h"
#include "vtkImageViewer2.h"
#include "vtkImageFlip.h"
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
using namespace std;
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);    
}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::on_OpenFile_triggered()
{
    typedef signed short InputPixelType;
    const unsigned int InputDimension=2;
    typedef itk::Image<InputPixelType,InputDimension> ImageType;
    typedef itk::ImageFileReader<ImageType> ReaderType;
    ReaderType::Pointer reader = ReaderType::New();
    reader->SetFileName("F://1-248.dcm");
    typedef itk::GDCMImageIO ImageIOType;
    ImageIOType::Pointer gdcmImageIO = ImageIOType::New();
    reader->SetImageIO(gdcmImageIO);
    reader->Update();
    std::cout<<"read dicom sucess"<<std::endl;
    //头文件数据字典
    typedef itk::MetaDataDictionary DictionaryType;
    DictionaryType &dictionary= gdcmImageIO->GetMetaDataDictionary();
    typedef itk::MetaDataObject<std::string> MetaDataStringType;//管理字符串类型
    //从数据字典中可以获得几乎所有dicom标签对应的数值
    DictionaryType::ConstIterator itr=dictionary.Begin();
    DictionaryType::ConstIterator end=dictionary.End();
    while(itr!=end)
    {
        itk::MetaDataObjectBase::Pointer entry=itr->second;
        MetaDataStringType::Pointer entryValue=dynamic_cast<MetaDataStringType *>(entry.GetPointer());
        //dynamic_cast将一个基类对象指针(或引用)cast到继承类指针 MetaDataStringType::Pointer
        if(entryValue)
        {
            std::string tagKey=itr->first;
            std::string labelId;
            bool found=itk::GDCMImageIO::GetLabelFromTag(tagKey,labelId);
            std::string tagValue = entryValue->GetMetaDataObjectValue();
            if(found)
            {
                std::cout<<"("<<tagKey<<")"<<labelId;
                std::cout<<"="<<tagValue.c_str()<<std::endl;
            }
            else
            {
               std::cout<<"("<<tagKey<<")"<<"unknown";
               std::cout<<"="<<tagValue.c_str()<<std::endl;
            }
            ++itr;
        }
    }
    //获取医院的名称并在qt上显示。
     std::string tagKey="0008|103e";
     std::string value;
     gdcmImageIO->GetValueFromTag(tagKey,value);
     std::cout<<"value="<<value.c_str()<<std::endl;
     ui->hospital->setText(value.c_str());
    typedef itk::ImageToVTKImageFilter<ImageType> FilterType;
    FilterType::Pointer connector = FilterType::New();
    connector->SetInput(reader->GetOutput());
    connector->Update();        //vtk版本大于5必须加上这句话,否则显示不出图片
    vtkImageFlip *flip=vtkImageFlip::New();//由于vtk、itk显示图像时y轴相反,此处做一次翻转
    flip->SetInputData(connector->GetOutput());
    flip->SetFilteredAxis(1);
    flip->Update();
    vtkImageViewer2  *viewer =  vtkImageViewer2::New();
   // vtkRenderWindowInteractor *interactor=vtkRenderWindowInteractor::New();
    viewer->SetInputData(flip->GetOutput());
    viewer->SetColorWindow(1224);
    viewer->SetColorLevel(587);
    viewer->SetRenderWindow(ui->qvtkwidget->GetRenderWindow());
    viewer->Render();
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表