码迷,mamicode.com
首页 > 其他好文 > 详细

ITK&&VTK 联合编程

时间:2016-08-01 11:58:14      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

 

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 联合编程。

    

  

 

ITK&&VTK 联合编程

标签:

原文地址:http://www.cnblogs.com/donglinjia/p/5725192.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!