安装 VS2015 和 QT-5.7.1
- 下载并安装 VS2015 和 qt-opensource-windows-x86-msvc2015-5.7.1.exe
- 配置QT环境变量
- 安装
QT-VS-TOOLS
并选择Qt版本C:\Qt\Qt5.7.1\5.7\msvc2015_64
安装 VTK
- 文件目录如下,
VTK-src
放置源代码,VTK-bin
放置CMake编译后的文件,VTK
放置最终的文件,对应的下载网址 VTK
- CMake 编译
- Configure 一次
BUILD_EXAMPLES
:指明是否编译VTK中的例子。未激活可以减少编译时间,之后也可以再进行单独编译。此处我们置为OFF。
BUILD_TESTING
——指明是否编译VTK中的测试代码。未激活可以减少编译时间,之后也可以再进行单独编译。此处我们置为OFF。
BUILD_SHARED_LIBS
——指明是否创建共享库。如果激活,那么DLL或共享库将会被创建,这样之后生成的VTK应用程序将会比较小,可以节省不少空间;如果未激活,那么静态库将被创建.此处我们置为ON。
CMAKE_INSTALL_PREFIX
——VTK的生成路径,修改如下
接下来的设置是有关QT的,搜索qt并勾选全部
再次Configure后VTK_QT_VERSION
改为5
再次Configure,后QT的位置会自动检测出来(改变QT环境变量后需要关机一下)
这里Qt5WebKitWidgets_DIR
找不到,我们去掉Moudule_vtkGUISupportQtWebkit
Configure
后没红点击Generate
,然后点击Open Project
,还可以找到VTK.sln打开
- VS2015编译 :
ALL_BUILD
-生成
.没有问题后然后INSTALL
-仅用于项目
-仅生成INSTALL
换成重新编译
将QVTKWidgetPlugin.dll
复制到Qt 的designer中,对应目录如下
这样VS2015
中打开Qt designer
会多出QVTKWidget
*VTK测试
:采用cmake建立工程,目录如下
CMakeLists.txt内容
cmake_minimum_required(VERSION 2.8)
cmake_policy(SET CMP0020 NEW)
project(Cylinder)
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
add_executable(Cylinder Cylinder.cxx)
if(VTK_LIBRARIES)
target_link_libraries(Cylinder ${VTK_LIBRARIES})
else()
target_link_libraries(Cylinder vtkHybrid vtkWigets)
endif(VTK_LIBRARIES)
这里需要输入VTK_DIR
(如果系统环境变量配置了D:\Kitware\VTK\VTK\debug\bin
,这里会自动搜索出),他是包含UseVTK.cmake
的文件夹
然后Configure
没红后Generate
没问题后 Open Project
打开VS2015在Cylinder.cxx
中输入下面代码
#include "vtkCylinderSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkCamera.h"
#include "vtkProperty.h"
#include "vtkSmartPointer.h"
int main() {
vtkSmartPointer<vtkCylinderSource> cylinder = vtkSmartPointer<vtkCylinderSource>::New();
cylinder->SetResolution(8);
vtkSmartPointer<vtkPolyDataMapper> cylinderMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
cylinderMapper->SetInputConnection(cylinder->GetOutputPort());
vtkSmartPointer<vtkActor> cylinderActor = vtkSmartPointer<vtkActor>::New();
cylinderActor->SetMapper(cylinderMapper);
cylinderActor->GetProperty()->SetColor(1.0000, 0.3882, 0.2784);
cylinderActor->RotateX(30.0);
cylinderActor->RotateY(-45.0);
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(cylinderActor);
renderer->SetBackground(0, 0, 1);//RGB
renderer->ResetCamera();
renderer->GetActiveCamera()->Zoom(0.3);
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
renderWindow->SetSize(200, 200);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindowInteractor->Start();
return 0;
}
ALL-BUILD
-生成
打开Cylinder.exe
安装ITK
从ITK 下载 到ITK_src
,这里下的InsightToolkit-4.11.0
版本,这里是 ITK examples,安装和上面一致,注意的是要勾上Moudule_ITKVtkGlue
- ITK测试
CMakeLists.cxx
cmake_minimum_required(VERSION 2.8)
cmake_policy(VERSION 2.8)
project(HelloWorld)
find_package(ITK REQUIRED)
include(${ITK_USE_FILE})
add_executable(HelloWorld HelloWorld.cxx)
target_link_libraries(HelloWorld ${ITK_LIBRARIES})
HelloWorld.cxx
#include"itkImage.h"
#include<iostream>
int main(){
typedef itk::Image<unsigned short,3> ImageType;
ImageType::Pointer image=ImageType::New();
std::cout<<"hello world"<<std::endl;
system("pause");
return 0;
}
结果
VTK,ITK混合测试
我们使用ITK读取图片然后VTK展示
CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
cmake_policy(SET CMP0020 NEW)
project(ImageToVTKImageFilter)
find_package(ITK REQUIRED)
include(${ITK_USE_FILE})
if (ITKVtkGlue_LOADED)
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
else()
find_package(ItkVtkGlue REQUIRED)
include(${ItkVtkGlue_USE_FILE})
set(Glue ItkVtkGlue)
endif()
add_executable(ImageToVTKImageFilter ImageToVTKImageFilter.cxx)
target_link_libraries(ImageToVTKImageFilter
${Glue} ${VTK_LIBRARIES} ${ITK_LIBRARIES})
ImageToVTKImageFilter.cxx
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkGDCMImageIO.h"
#include "itkImageToVTKImageFilter.h"
#include "vtkSmartPointer.h"
#include "vtkImageFlip.h"
#include "vtkImageViewer.h"
#include "vtkRenderWindowInteractor.h"
int main()
{
typedef signed short PixelType;
const unsigned int Dimension = 2;
typedef itk::Image<PixelType, Dimension> ImageType;
typedef itk::GDCMImageIO ImageIOType;
ImageIOType::Pointer imageIO = ImageIOType::New();
typedef itk::ImageFileReader<ImageType> ReaderType;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName("E:/22");
reader->SetImageIO(imageIO);
reader->Update();
typedef itk::ImageToVTKImageFilter<ImageType> ImageToVTKImageFilterType;
ImageToVTKImageFilterType::Pointer itkToVtk = ImageToVTKImageFilterType::New();
itkToVtk->SetInput(reader->GetOutput());
itkToVtk->Update();
vtkSmartPointer<vtkImageFlip> imageFlip = vtkSmartPointer<vtkImageFlip>::New();
imageFlip->SetInputData(itkToVtk->GetOutput());
imageFlip->SetFilteredAxes(1);
imageFlip->Update();
vtkSmartPointer<vtkRenderWindowInteractor> interactor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkImageViewer> imageViewer = vtkSmartPointer<vtkImageViewer>::New();
imageViewer->SetInputData(imageFlip->GetOutput());
imageViewer->SetColorLevel(300);
imageViewer->SetColorWindow(2000);
imageViewer->SetupInteractor(interactor);
imageViewer->Render();
system("pause");
return 0;
}