没想到困扰昨天晚上一晚上的问题,早上就突然就解决了。开心!继续上一篇的内容。
在参考之前的几篇文章,大家已经完成了基本ITK,VTK的独立安装了,现在我们开始联合编译一下。
本项目Github地址: Alxemade/VTK_ITK_SimpleTest/test_itk_vtk/ 欢迎Star和Fork。
这里我们参考的是如下文章: ITK的安装与测试。
但是由于版本的问题,我们需要对一些局部代码就行修改,才能得到我们期望的结果。
按照作者的文章,我们需要安装insightApplication, 但是我惊喜的发现我们安装ITK之后已经有了相关的模块了,所以就不需要安装了。主要是#include
头文件,表示ITK和VTK的图形的转换,但是我们已经有了。就不需要继续安装了。
这里这个pro文件编写的思路还是很简单的,就是把itk和vtk相关的头文件和lib文件包含进去就行了。
其中需要注意一点就是, 我们需要额外添加几句代码:
win32: LIBS += "shell32.lib"
win32: LIBS += "advapi32.lib"
win32: LIBS += "rpcrt4.lib"
win32: LIBS += "wsock32.lib "
不然后续会出现很多:出现异常:error LNK2019: 无法解析的外部符号 _SnmpUtilVarBindFree@4
的错误。
我们总体.pro文件代码如下:
QT -= gui
CONFIG += c++11 console
CONFIG -= app_bundle
QT += widgets
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
main.cpp
SysStatus = $$system(if exist %windir%/SysWOW64 echo x64) ## if contains SysWOW64 prove the windows is 64 bit
win32 {
## Windows common build here
!contains(SysStatus, x64) {
message("x86 build ")
## Windows x86 (32bit) specific build here
} else {
message("x86_64 build")
## Windows x64 (64bit) specific build here
LABMR_PREFIX = E:/XC/vtk/vtk-8.1.1/build/lib
## TOOLS_PREFIX = quote(C:/Program Files)
}
}
##VTK INCLUDEPATH Starts
INCLUDEPATH += $$quote(C:/Program Files/VTK/include/vtk-8.1)
##VTK Ends
##ITK INCLUDEPATH Starts
INCLUDEPATH += $$quote(E:/XC/itk/Bin/include/ITK-4.13)
##win32: LIBS += "snmpapi.lib"
win32: LIBS += "shell32.lib"
win32: LIBS += "advapi32.lib"
win32: LIBS += "rpcrt4.lib"
win32: LIBS += "wsock32.lib "
##ITK Ends
CONFIG(debug, debug|release) {
## VTK Debug LIB Starts
LIBS += $${LABMR_PREFIX}/Debug/vtk*.lib
## VTK Debug LIB Ends
## ITK Debug LIB Starts
LIBS += $$quote(E:/XC/itk/Bin/lib/itk*.lib)
LIBS += $$quote(E:/XC/itk/Bin/lib/ITK*.lib)
## ITK Debug LIB Ends
} else {
## VTK Release LIB Starts
LIBS += $${LABMR_PREFIX}/Release/vtk*.lib
## VTK Release LIB Ends
## ITK Release LIB Starts
## ITK Release LIB Starts
}
如果直接拷贝之前的文章可能不能直接运行:我们需要进行修改。
这是我们在进行转换的时候出现的出现的问题,解决方法:C/C++ const char如何转换成char
参考:VTK学习(一)SetInputData和SetInputConnection替换SetInput
我们对相关的进行修改。作者的思路还是因为版本的问题。
参考: VTKError:no override found for ‘vtkImageMapper’ 和Link to vtkInteractionStyle
解决加上两句代码:
#include
VTK_MODULE_INIT(vtkRenderingOpenGL2)
注意因为我们的版本比较高,这里是vtkRenderingOpenGL2
而不是vtkRenderingOpenGL
一般来说,主要的问题就是这些,如果有其他问题,可以自行百度。 接下来我们贴一下我们的cpp代码:
#include
#include
VTK_MODULE_INIT(vtkRenderingOpenGL2)
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int main()
{
typedef itk::Image<unsigned char,2> ImageType;
typedef itk::ImageFileReader<ImageType> ReaderType;
ReaderType::Pointer reader = ReaderType::New();
//获得DICOM文件读取对象
typedef itk::GDCMImageIO ImageIOType;
ImageIOType::Pointer gdcmImageIO = ImageIOType::New();
//Filterofitkimagedatatovtkimagedata;
typedef itk::ImageToVTKImageFilter<ImageType>FilterType;
FilterType::Pointer connector = FilterType::New();
//获得DICOM文件名并读取DICOM文件;
const char *temp = "G://qt_problem//test_itk_vtk//dcm//brain_001.dcm";
char *DICOMName =new char[strlen(temp)+1];
strcpy(DICOMName, temp);
//const char* DICOMName = "G://qt_problem//test_itk_vtk//dcm//brain_001.dcm";
reader->SetFileName(DICOMName);
reader->SetImageIO(gdcmImageIO);
//ITK到VTK转化
connector->SetInput(reader->GetOutput());
connector->Update();
vtkSmartPointer<vtkImageFlip> flip = vtkSmartPointer<vtkImageFlip>::New();
//vtkSmartPointer
flip->SetInputData(connector->GetOutput());
flip->SetFilteredAxis(1);
//需要进行上下翻转才能正确显示;
flip->Update();
vtkSmartPointer<vtkImageActor>actor = vtkSmartPointer<vtkImageActor>::New();
actor->SetInputData(flip->GetOutput());
actor->InterpolateOff();
vtkSmartPointer<vtkRenderer>renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(actor);
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renderWindow);
iren->Initialize();
iren->Start();
return 0;
}
这里有一点就是我们显示的是一个dcm文件,百度了好多都是需要csdn金币的,但是没有真的无力吐槽开源多么重要 推荐一个一个博客,上面写了好多dicom图像网站,真心不错。收集了一些国外 DICOM 文件下载网站。
另外在我的项目里面我提供了20张dcm图像,感兴趣的同学可以自己下载:
Alxemade/VTK_ITK_SimpleTest/test_itk_vtk/dcm/
最后显示的图像大概是这样。
Over!
因为ITK,VTK,QT这些也是第一次接触,其中好多原理性的东西也不是太明白。所以有错误的地方还是需要大家指出来。 接下来就是学习这些库的过程了…