ITK&&VTK 联合编程

  1. CMakeLists.txt

    find_package 里面要有 VTK REQUIRED 和 ITK REQUIRED

    include包含${VTK_USE_FILE} 和${ITK_USE_FILE}

    联合编程主要要包含itkImageToVTKImageFilter.h该文件,在下面中贴出。

    cmake_minimum_required(VERSION 2.8)
    
    project(RayCast)
    
    find_package(VTK REQUIRED)
    find_package( ITK REQUIRED )
    
    include(${VTK_USE_FILE} )
    INCLUDE( ${ITK_USE_FILE} )
    
    add_executable(RayCastInstall RayCast.cxx itkImageToVTKImageFilter.h)
    
    target_link_libraries(RayCastInstall  ${VTK_LIBRARIES} ${ITK_LIBRARIES})
    

     

  2. 除了自己的 .cxx文件以外还需要itkImageToVTKImageFilter.h,该文件在InsightToolkit-4.7.2里面。在此贴出该文件。

    /*=========================================================================
     *
     *  Copyright Insight Software Consortium
     *
     *  Licensed under the Apache License, Version 2.0 (the "License");
     *  you may not use this file except in compliance with the License.
     *  You may obtain a copy of the License at
     *
     *         http://www.apache.org/licenses/LICENSE-2.0.txt
     *
     *  Unless required by applicable law or agreed to in writing, software
     *  distributed under the License is distributed on an "AS IS" BASIS,
     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     *  See the License for the specific language governing permissions and
     *  limitations under the License.
     *
     *=========================================================================*/
    #ifndef __itkImageToVTKImageFilter_h
    #define __itkImageToVTKImageFilter_h
    
    #include "itkVTKImageExport.h"
    #include "vtkImageImport.h"
    #include "vtkImageData.h"
    
    namespace itk
    {
    
    /** \class ImageToVTKImageFilter
     * \brief Converts an ITK image into a VTK image and plugs a
     *  itk data pipeline to a VTK datapipeline.
     *
     *  This class puts together an itkVTKImageExporter and a vtkImageImporter.
     *  It takes care of the details related to the connection of ITK and VTK
     *  pipelines. The User will perceive this filter as an adaptor to which
     *  an itk::Image can be plugged as input and a vtkImage is produced as
     *  output.
     *
     * \ingroup   ITKVtkGlue
     *
     * \wiki
     * \wikiexample{IO/ImageToVTKImageFilter,Display an ITK image}
     * \wikiexample{IO/itkVtkImageConvertDICOM,Uses a custom user matrix to align the image with DICOM physical space}
     * \endwiki
     */
    template <typename TInputImage >
    class ImageToVTKImageFilter : public ProcessObject
    {
    public:
      /** Standard class typedefs. */
      typedef ImageToVTKImageFilter     Self;
      typedef ProcessObject             Superclass;
      typedef SmartPointer<Self>        Pointer;
      typedef SmartPointer<const Self>  ConstPointer;
    
      /** Method for creation through the object factory. */
      itkNewMacro(Self);
    
      /** Run-time type information (and related methods). */
      itkTypeMacro(ImageToVTKImageFilter, ProcessObject);
    
      /** Some typedefs. */
      typedef TInputImage                            InputImageType;
      typedef typename InputImageType::ConstPointer  InputImagePointer;
    
      typedef VTKImageExport< InputImageType>        ExporterFilterType;
      typedef typename ExporterFilterType::Pointer   ExporterFilterPointer;
    
      /** Get the output in the form of a vtkImage.
          This call is delegated to the internal vtkImageImporter filter  */
      vtkImageData *  GetOutput() const;
    
      /** Set the input in the form of an itk::Image */
      using Superclass::SetInput;
      void SetInput( const InputImageType * );
      InputImageType * GetInput();
    
      /** Return the internal VTK image importer filter.
          This is intended to facilitate users the access
          to methods in the importer */
      vtkImageImport * GetImporter() const;
    
      /** Return the internal ITK image exporter filter.
          This is intended to facilitate users the access
          to methods in the exporter */
      ExporterFilterType * GetExporter() const;
    
      /** This call delegates the update to the importer */
      void Update();
    
    protected:
      ImageToVTKImageFilter();
      virtual ~ImageToVTKImageFilter();
    
    private:
      ImageToVTKImageFilter(const Self&); //purposely not implemented
      void operator=(const Self&);        //purposely not implemented
    
      ExporterFilterPointer       m_Exporter;
      vtkImageImport *            m_Importer;
    };
    
    } // end namespace itk
    
    #ifndef ITK_MANUAL_INSTANTIATION
    #include "itkImageToVTKImageFilter.hxx"
    #endif
    
    #endif
    

      

  3. 写入自己的文件,在此该文件是ITK读取序列,VTK显示。

    #if defined(_MSC_VER)
    #pragma warning ( disable : 4786 )
    #endif
    
    #ifdef __BORLANDC__
    #define ITK_LEAN_AND_MEAN
    #endif
    
    #include "itkImage.h"
    #include "itkImageFileReader.h"
    #include "itkImageToVTKImageFilter.h"
    #include "itkGDCMImageIO.h"
    #include "itkGDCMSeriesFileNames.h"
    #include "itkImageSeriesReader.h"
    #include "vtkImageAnisotropicDiffusion3D.h"
    #include "vtkImageCast.h"
    #include <itkShiftScaleImageFilter.h>
    #include "vtkRenderer.h"
    #include "vtkRenderWindow.h"
    #include "vtkRenderWindowInteractor.h"
    #include "vtkPiecewiseFunction.h"
    #include "vtkColorTransferFunction.h"
    #include "vtkVolumeProperty.h"
    #include "vtkVolumeRayCastIsosurfaceFunction.h"
    #include "vtkVolumeRayCastCompositeFunction.h"
    #include "vtkVolumeRayCastMapper.h"
    #include "vtkVolume.h"
    #include "vtkImageCast.h"
    
    int main( int argc, char* argv[] )
    {
      if( argc < 2 )
        {
        std::cerr << "Usage: " << argv[0] << " DicomDirectory " << std::endl;
        return EXIT_FAILURE;
        }
      typedef  signed short       PixelType;
      const unsigned int         Dimension = 3;
      typedef itk::Image< PixelType, Dimension >      ImageType;
      typedef itk::ImageSeriesReader< ImageType >     ReaderType;
      ReaderType::Pointer reader = ReaderType::New();
      typedef itk::GDCMImageIO       ImageIOType;
      ImageIOType::Pointer dicomIO = ImageIOType::New();
      reader->SetImageIO( dicomIO );
      typedef itk::GDCMSeriesFileNames     NamesGeneratorType;
      NamesGeneratorType::Pointer nameGenerator = NamesGeneratorType::New();
      nameGenerator->SetInputDirectory( argv[1] );  //读取序列,在参数中输入位置
    
      //"e://mi/pic/111"
      /*const ReaderType::FileNamesContainer & filenames=nameGenerator->GetInputFileNames();
      reader->SetFileNames( filenames );*/
      typedef std::vector<std::string>    FileNamesContainer;
      FileNamesContainer fileNames = nameGenerator->GetInputFileNames();
      reader->SetFileNames( fileNames );
     try
        {
        reader->Update();
        }
      catch (itk::ExceptionObject &ex)
        {
        std::cout << ex << std::endl;
        return EXIT_FAILURE;
        }
            //ITK TO VTK
            typedef itk::ImageToVTKImageFilter< ImageType> itkTovtkFilterType;
            itkTovtkFilterType::Pointer itkTovtkImageFilter = itkTovtkFilterType::New();
            itkTovtkImageFilter->SetInput(reader->GetOutput());
    
            vtkImageCast* readerImageCast=vtkImageCast::New();
            readerImageCast->SetInput(itkTovtkImageFilter->GetOutput());
            readerImageCast->SetOutputScalarTypeToUnsignedShort();
            readerImageCast->ClampOverflowOn();
    
    	vtkRenderer *aRender = vtkRenderer::New();
    	vtkRenderWindow *renWin = vtkRenderWindow::New();
    		renWin->AddRenderer(aRender);
    	vtkRenderWindowInteractor *iRen = vtkRenderWindowInteractor::New();
    		iRen->SetRenderWindow(renWin);	
    
    		//不透明度映射函数是设置光线方向上的灰度值及其不透明度映射。
    	vtkPiecewiseFunction *opacityTransferFunction = vtkPiecewiseFunction::New();
    
    	opacityTransferFunction->AddPoint(30.0, 0.2);
    	opacityTransferFunction->AddPoint(40.0, 0.3);
    	opacityTransferFunction->AddPoint(50.0, 0.5);
    	opacityTransferFunction->AddPoint(60.0, 0.7);
    	opacityTransferFunction->AddPoint(70.0, 0.9);
    
    		//颜色映射函数是设置灰度值与RGB颜色的映射。//灰度值及RGB颜色值
    	vtkColorTransferFunction *colorTransferFunction = vtkColorTransferFunction::New();
    	   colorTransferFunction->AddRGBPoint(0.0, 0.5, 0.0, 0.0);
           colorTransferFunction->AddRGBPoint(30, 0.75, 0.75, 0.75);//皮肤
           colorTransferFunction->AddRGBPoint(50, 1, 0.6, 0.07);//骨骼
           colorTransferFunction->AddRGBPoint(100.0, 1, 1, 1);
    
    	vtkVolumeProperty *volumeProperty = vtkVolumeProperty::New();
    	  volumeProperty->SetColor(colorTransferFunction);
    	  volumeProperty->SetScalarOpacity(opacityTransferFunction);
    	  volumeProperty->ShadeOn();
      	  volumeProperty->SetInterpolationTypeToLinear();
    	  volumeProperty->SetAmbient(0.2);
          volumeProperty->SetDiffuse(0.9);
          volumeProperty->SetSpecular(0.2);
          volumeProperty->SetSpecularPower(10);	
    
    	vtkVolumeRayCastCompositeFunction *compositeFunction = vtkVolumeRayCastCompositeFunction::New();
        vtkVolumeRayCastIsosurfaceFunction *RayIso1=vtkVolumeRayCastIsosurfaceFunction::New();
          RayIso1->SetIsoValue (50) ;
    
        vtkVolumeRayCastIsosurfaceFunction *RayIso2=vtkVolumeRayCastIsosurfaceFunction::New();
          RayIso2->SetIsoValue (30) ;
    
    	vtkVolumeRayCastMapper *volumeMapper1 = vtkVolumeRayCastMapper::New();
    	  volumeMapper1->SetVolumeRayCastFunction(RayIso1);
    	  volumeMapper1->SetInputConnection( readerImageCast->GetOutputPort());
    
    	vtkVolumeRayCastMapper *volumeMapper2 = vtkVolumeRayCastMapper::New();
    	  volumeMapper2->SetVolumeRayCastFunction(RayIso2);
    	  volumeMapper2->SetInputConnection( readerImageCast->GetOutputPort());
    
    	 vtkVolume *volume1 = vtkVolume::New();
    		  volume1->SetMapper(volumeMapper1);
    		  volume1->SetProperty(volumeProperty);
    	 vtkVolume *volume2 = vtkVolume::New();
    		  volume2->SetMapper(volumeMapper2);
    		  volume2->SetProperty(volumeProperty);
    	aRender->AddVolume(volume1);
    	aRender->SetBackground(1,1,1);
    	aRender->AddVolume(volume2);
    	aRender->SetBackground(1,1,1);
    	renWin->SetSize(600, 600);
    	renWin->Render();
    
    	iRen->Initialize();
    	iRen->Start();
    
    	aRender->Delete();
    	renWin->Delete();
    	iRen->Delete();
    	reader->Delete();
    	opacityTransferFunction->Delete();
    	colorTransferFunction->Delete();
    	readerImageCast->Delete();
    	//duff->Delete();
    
    	volumeMapper1->Delete();
        volumeMapper2->Delete();
    	volumeProperty->Delete();
    	compositeFunction->Delete();
    	volume1->Delete();
        volume2->Delete();
    
    	return 0;
    
    }
    

      

  4. CMAKE之后即可完成ITK &&VTK 联合编程。

    

  

时间: 2024-10-12 16:11:18

ITK&&VTK 联合编程的相关文章

c++与matlab联合编程,调用Deploytool 生成exe文件和dll文件(转)

转自:http://www.cnblogs.com/xlw1219/archive/2012/12/25/2832222.html 首先必须知道联合编程需要知道的一些命令解释: mcc 的作用是将 .m文件编译为 c/c++动态链接库文件,使你可以在 c/c++程序中使用 matlab的一些函数功能.mcc 也可以将.m文件编译为exe可执行文件. mex 的作用是将 c/cpp文件编译为 .m  文件可以调用的库文件,在Windows操作系统里通常是以mexw32或mexw64为扩展名,让你可

OpenGL【3 MFC和OpenGL联合编程框架简述】

[需要将view的显示区域黑色背景所需的步骤] 1. 简历普通单文档MFC工程(自动关联了DOC VIEW 和Frame三个类) 2. 拷贝Test 工程中的几个函数到目标工程 一.PreCreateWindow[改变窗口类型] 二.OnCreate[调用初始化函数myInitOpenGL] 三.myInitOpenGL[建立DC 和RC并关联二者,其中调用mySetupPixelFormat初始化RC绘图环境] 四.mySetupPixelFormat[初始化RC绘图环境] 五.OnDraw里

VTk与MFC单文档程序联合编程

兴趣需要,想做下VTK与MFC想结合的程序,MFC快要在桌面程序上面失去市场份额了,现在大多使用QT来做,但是本科的时候学的就是MFC,也相对来说比较熟悉,所以就想使用MFC来写一个简单的单文档程序.首先我们需要在编译的时候将USEGUISUPPORT->USEMFC勾选上,才能在MFC平台上使用VTK.网络上现在大多流行两种VTK和MFC的方法,其实两者结合的关键就是将VTK的绘制窗口vtkrenderwindow与MFC中的view窗口相一致,让VTK上的绘制图形能够在MFC上的VIEW类上

[转载] C# matlab联合编程简介

原作者  文月 主要操作说明: 1. 找到matlab安装目录下的MCRInstaller.exe安装 MCRInstaller.exe 在安装目录下的 ..\MATLAB7\toolbox\compiler\deploy\win32\中: 2. 将写好的matlab的.m文件转换为动态链接库 2.1 编辑M文件 比如写了.m文件 f.m.其中的function C=f(A,B)实现的是C=A+B function C=f(A,B) C=A+B; end 2.2 在matlab的命令窗口中输入d

VS2010 C# Halcon12 联合编程,读取图片

1.首先配置好VS2010: 添加目录"HALCON-12.0\bin\dotnet35\halcondotnet.dll" 添加后工具栏中多出HWindowControl工具: 2.写好Halcon程序并导出: 导出: 其中halcon_test.cs代码如下: // // File generated by HDevelop for HALCON/DOTNET (C#) Version 12.0 // // This file is intended to be used with

关于VS2017+Qt5.6.3(msvc2015_64)联合编程Qt project settings界面没有ok,cancel选项的问题

如题,我在项目开发的过程中,需要添加数据库模块SQL,然后发现VS上QT project settings选项中不能修改添加的模块,也就是对应QT creator中的在.pro文件中添加一句:QT += sql,的操作不能通过QT project settings界面实现. 图1 笔者VS上的Qt project settings界面                                                    图2 在网上看到的其他人VS上的Qt project set

Halcon 和 C# 联合编程 - 如何使用开源项目 ViewROI

声明 HWndCtrl _viewCtrl; ROIController _roiCtrl; 初始化 _viewCtrl = new HWndCtrl(hWindowControl); _roiCtrl = new ROIController(); _viewCtrl.useROIController(_roiCtrl); 显示图像 HImage hImage = new HImage(image); _viewCtrl.addIconicVar(hImage); // 注意不可以是HObjec

Halcon 和 C# 联合编程 - 图像变量的相互转换(HObject、HImage、Bitmap)

/// <summary> /// 灰度图像 HObject -> Bitmap /// </summary> public static Bitmap HObject2Bitmap(HObject ho) { try { HTuple type, width, height, pointer; //HOperatorSet.AccessChannel(ho, out ho, 1); HOperatorSet.GetImagePointer1(ho, out pointer,

[转] Matlab与C++混合编程,添加OpenCV库

原文地址 峰回璐转 最近在做运动医学软件优化工作,此款软件框架及算法语言全由matlab实现,虽然matlab矩阵运算.数值计算能力强大,但速度让人难以忍 受.软件立刻移植到C++上又不太实际,故采用联合编程的方式,速度难以容忍的算法交给C++实现,C++在实现代码的过程中某些数值计算及图像处理算法 调opencv库函数. 在网上有很多matlab编写mex函数调用opencv库的方法,但都不能直接拿来.经过一步步试验,修改,最终完成,现将过程及内容记录下来留给后来人参考. 第一步: (参考参考