VS2017+ITK-5.1.1+VTK-9.0.1

一、编译安装ITK-5.1.1

下载对应版本库,创建文件夹:创建ITKlib:Bin,Build,Source
Bin:后续VS2017编译的文件路径;
Build:cmake编译后的文件路径;
Source:将ITK-5.0.1解压到该目录下。

1.Cmake选 x64

2.更改选项:

(1)将CMAKE_INSTALL_PREFIX选项设置到创建的 \ITKlib\Bin文件夹

(2)勾选BUILD_SHARED_LIBS选项,勾选该选项会生成动态链接库dll文件

(3)勾选Module_ITKVtkGlue项

3.编译项目ALL_BULID: debug、x64 都编译一次,INSTALL同样编译一次;

4.配置环境:在path中添加 \ITKlib\Bin文件夹路径,将bin文件夹下的所有dll文件复制到C:\Windows\System32和C:\Windows\SysWOW64中。

 

二、编译安装VTK-9.0.1

VTK安装跟ITK类似。
1.首先创建VTK-9.0.1lib文件夹,该文件夹下创建Bin,Build,Source文件夹。将VTK-9.0.1解压到Source中。

cmake修改选项
勾选BUILD_SHARED_LIBS;
修改CMAKE_INSTALL_PREFIX路径为x:\lib\VTK-9.0.1lib\Bin,即刚刚创建的Bin目录。
选择Debug、x64,右键ALL_BUILD,点击重新生成。

配置环境变量
path添加x:\lib\VTK-9.0.1lib\Bin

将bin文件夹下的所有dll文件复制到C:\Windows\System32和C:\Windows\SysWOW64中

点击VC++目录,编辑包含目录,添加include路径
x:\lib\VTK-9.0.1lib\Bin\include\vtk-8.2和x:\lib\VTK-9.0.1lib\Bin\include

编辑库目录,添加lib路径
x:\lib\VTK-9.0.1lib\Bin\lib

打开链接器->输入,添加附加依赖项。

由于库比较多,先在lib文件夹下创建一个lib.txt文件,输入
DIR *.lib /B >lib.TXT
然后把lib.txt修改为.bat格式,运行,会生成一个lib.TXT文件,发现已将所有.lib文件写入,复制到附加依赖项。

在创建到vtktest中输入测试代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
VTK_MODULE_INIT(vtkRenderingOpenGL2);//和官方的例子有些不同,下面解释
VTK_MODULE_INIT(vtkInteractionStyle);
int main(int, char *[]) {
  vtkSmartPointer colors =
      vtkSmartPointer::New();

  // Set the background color.
  std::array bkg{
    {26, 51, 102, 255}};
  colors->SetColor("BkgColor", bkg.data());

  // This creates a polygonal cylinder model with eight circumferential facets
  // (i.e, in practice an octagonal prism).
  vtkSmartPointer cylinder =
      vtkSmartPointer::New();
  cylinder->SetResolution(8);

  // The mapper is responsible for pushing the geometry into the graphics
  // library. It may also do color mapping, if scalars or other attributes are
  // defined.
  vtkSmartPointer cylinderMapper =
      vtkSmartPointer::New();
  cylinderMapper->SetInputConnection(cylinder->GetOutputPort());

  // The actor is a grouping mechanism: besides the geometry (mapper), it
  // also has a property, transformation matrix, and/or texture map.
  // Here we set its color and rotate it around the X and Y axes.
  vtkSmartPointer cylinderActor = vtkSmartPointer::New();
  cylinderActor->SetMapper(cylinderMapper);
  cylinderActor->GetProperty()->SetColor(
      colors->GetColor4d("Tomato").GetData());
  cylinderActor->RotateX(30.0);
  cylinderActor->RotateY(-45.0);

  // The renderer generates the image
  // which is then displayed on the render window.
  // It can be thought of as a scene to which the actor is added
  vtkSmartPointer renderer = vtkSmartPointer::New();
  renderer->AddActor(cylinderActor);
  renderer->SetBackground(colors->GetColor3d("BkgColor").GetData());
  // Zoom in a little by accessing the camera and invoking its "Zoom" method.
  renderer->ResetCamera();
  renderer->GetActiveCamera()->Zoom(1.5);

  // The render window is the actual GUI window
  // that appears on the computer screen
  vtkSmartPointer renderWindow =
      vtkSmartPointer::New();
  renderWindow->SetSize(300, 300);
  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("Cylinder");

  // The render window interactor captures mouse events
  // and will perform appropriate camera or actor manipulation
  // depending on the nature of the events.
  vtkSmartPointer renderWindowInteractor =
      vtkSmartPointer::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);

  // This starts the event loop and as a side effect causes an initial render.
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

三、ITK和VTK联合测试,用ITK读取图像,VTK显示

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageToVTKImageFilter.h"
#include "itkJPEGImageIOFactory.h"

#include "vtkSmartPointer.h"
#include "vtkImageViewer.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2); // VTK was built with vtkRenderingOpenGL2
VTK_MODULE_INIT(vtkInteractionStyle);
#include "vtkImageFlip.h"
//ITK and VTK to show picture test
int main(int argc, char **argv)
{
	typedef itk::Image ImageType;//the type of pic
	typedef itk::ImageFileReader ReaderType;   //the class of read pic
	typedef itk::ImageToVTKImageFilter ConnectorType;  //connenct itk and vtk

	ReaderType::Pointer reader = ReaderType::New();
	ConnectorType::Pointer connector = ConnectorType::New();
	reader->SetFileName("C:/input/1.jpg");

	itk::JPEGImageIOFactory::RegisterOneFactory();

	connector->SetInput(reader->GetOutput());
	connector->Update();

	vtkSmartPointer imageflip = vtkSmartPointer::New();
	imageflip->SetInputData(connector->GetOutput());
	imageflip->SetFilteredAxes(1);//Y轴
	imageflip->Update();

	vtkSmartPointer viewer = vtkSmartPointer::New();
	vtkSmartPointer interactor = vtkSmartPointer::New();
	viewer->SetInputData(imageflip->GetOutput());
	viewer->SetupInteractor(interactor);
	viewer->GetRenderWindow()->SetSize(484, 300);//set window size
	viewer->SetColorWindow(255); //set window color
	viewer->SetColorLevel(128);   //set the level of window
	viewer->Render();

	interactor->Initialize();
	interactor->Start();
	return EXIT_SUCCESS;
}

 

你可能感兴趣的:(C/C++,ITK,VTK,医疗影像)