ubuntu14.04下ITK4.8,VTK6.2和Qt4整合筆記

整合這三個,對於剛接觸的新手我來講是一件很複雜的玩意,根本無從下手,網絡整合的資源也很少,並且大部分是用cmake來管理項目,直接用過Qt的.pro文件來管理項目仍是不多的,所以經過此次整合經歷,記錄出現問題,以便翻閱。
html

一 整合sql

經過Qtcreator建立Qt項目,會出現帶有.pro文件的項目。json

編輯.pro文件網絡

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = vtkQtTest
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui


INCLUDEPATH += /usr/local/include/vtk-6.2
INCLUDEPATH += /usr/local/include/ITK-4.8

LIBS += -lvtkalglib-6.2\
 -lvtkChartsCore-6.2\
 -lvtkCommonColor-6.2\
 -lvtkCommonComputationalGeometry-6.2\
 -lvtkCommonCore-6.2\
 -lvtkCommonDataModel-6.2\
 -lvtkCommonExecutionModel-6.2\
 -lvtkCommonMath-6.2\
 -lvtkCommonMisc-6.2\
 -lvtkCommonSystem-6.2\
 -lvtkCommonTransforms-6.2\
 -lvtkDICOMParser-6.2\
 -lvtkDomainsChemistry-6.2\
 -lvtkexoIIc-6.2\
 -lvtkexpat-6.2\
 -lvtkFiltersAMR-6.2\
 -lvtkFiltersCore-6.2\
 -lvtkFiltersExtraction-6.2\
 -lvtkFiltersFlowPaths-6.2\
 -lvtkFiltersGeneral-6.2\
 -lvtkFiltersGeneric-6.2\
 -lvtkFiltersGeometry-6.2\
 -lvtkFiltersHybrid-6.2\
 -lvtkFiltersHyperTree-6.2\
 -lvtkFiltersImaging-6.2\
 -lvtkFiltersModeling-6.2\
 -lvtkFiltersParallel-6.2\
 -lvtkFiltersParallelImaging-6.2\
 -lvtkFiltersProgrammable-6.2\
 -lvtkFiltersSelection-6.2\
 -lvtkFiltersSMP-6.2\
 -lvtkFiltersSources-6.2\
 -lvtkFiltersStatistics-6.2\
 -lvtkFiltersTexture-6.2\
 -lvtkFiltersVerdict-6.2\
 -lvtkfreetype-6.2\
 -lvtkftgl-6.2\
 -lvtkGeovisCore-6.2\
 -lvtkgl2ps-6.2\
 -lvtkGUISupportQt-6.2\
 -lvtkGUISupportQtOpenGL-6.2\
 -lvtkGUISupportQtSQL-6.2\
 -lvtkGUISupportQtWebkit-6.2\
 -lvtkhdf5-6.2\
 -lvtkhdf5_hl-6.2\
 -lvtkImagingColor-6.2\
 -lvtkImagingCore-6.2\
 -lvtkImagingFourier-6.2\
 -lvtkImagingGeneral-6.2\
 -lvtkImagingHybrid-6.2\
 -lvtkImagingMath-6.2\
 -lvtkImagingMorphological-6.2\
 -lvtkImagingSources-6.2\
 -lvtkImagingStatistics-6.2\
 -lvtkImagingStencil-6.2\
 -lvtkInfovisCore-6.2\
 -lvtkInfovisLayout-6.2\
 -lvtkInteractionImage-6.2\
 -lvtkInteractionStyle-6.2\
 -lvtkInteractionWidgets-6.2\
 -lvtkIOAMR-6.2\
 -lvtkIOCore-6.2\
 -lvtkIOEnSight-6.2\
 -lvtkIOExodus-6.2\
 -lvtkIOExport-6.2\
 -lvtkIOGeometry-6.2\
 -lvtkIOImage-6.2\
 -lvtkIOImport-6.2\
 -lvtkIOInfovis-6.2\
 -lvtkIOLegacy-6.2\
 -lvtkIOLSDyna-6.2\
 -lvtkIOMINC-6.2\
 -lvtkIOMovie-6.2\
 -lvtkIONetCDF-6.2\
 -lvtkIOParallel-6.2\
 -lvtkIOParallelXML-6.2\
 -lvtkIOPLY-6.2\
 -lvtkIOSQL-6.2\
 -lvtkIOVideo-6.2\
 -lvtkIOXML-6.2\
 -lvtkIOXMLParser-6.2\
 -lvtkjpeg-6.2\
 -lvtkjsoncpp-6.2\
 -lvtklibxml2-6.2\
 -lvtkmetaio-6.2\
 -lvtkNetCDF-6.2\
 -lvtkNetCDF_cxx-6.2\
 -lvtkoggtheora-6.2\
 -lvtkParallelCore-6.2\
 -lvtkpng-6.2\
 -lvtkproj4-6.2\
 -lvtkRenderingAnnotation-6.2\
 -lvtkRenderingContext2D-6.2\
 -lvtkRenderingContextOpenGL-6.2\
 -lvtkRenderingCore-6.2\
 -lvtkRenderingFreeType-6.2\
 -lvtkRenderingFreeTypeOpenGL-6.2\
 -lvtkRenderingGL2PS-6.2\
 -lvtkRenderingImage-6.2\
 -lvtkRenderingLabel-6.2\
 -lvtkRenderingLIC-6.2\
 -lvtkRenderingLOD-6.2\
 -lvtkRenderingOpenGL-6.2\
 -lvtkRenderingQt-6.2\
 -lvtkRenderingVolume-6.2\
 -lvtkRenderingVolumeOpenGL-6.2\
 -lvtksqlite-6.2\
 -lvtksys-6.2\
 -lvtktiff-6.2\
 -lvtkverdict-6.2\
 -lvtkViewsContext2D-6.2\
 -lvtkViewsCore-6.2\
 -lvtkViewsInfovis-6.2\
 -lvtkViewsQt-6.2\
 -lvtkzlib-6.2\




LIBS+=-lITKBiasCorrection-4.8\
 -lITKBioCell-4.8\
 -lITKCommon-4.8\
 -lITKDICOMParser-4.8\
 -lITKEXPAT-4.8\
 -lITKFEM-4.8\
 -lITKgiftiio-4.8\
 -lITKIOBioRad-4.8\
 -lITKIOBMP-4.8\
 -lITKIOCSV-4.8\
 -lITKIOGDCM-4.8\
 -lITKIOGE-4.8\
 -lITKIOGIPL-4.8\
 -lITKIOHDF5-4.8\
 -lITKIOImageBase-4.8\
 -lITKIOIPL-4.8\
 -lITKIOJPEG-4.8\
 -lITKIOLSM-4.8\
 -lITKIOMesh-4.8\
 -lITKIOMeta-4.8\
 -lITKIOMRC-4.8\
 -lITKIONIFTI-4.8\
 -lITKIONRRD-4.8\
 -lITKIOPNG-4.8\
 -lITKIOSiemens-4.8\
 -lITKIOSpatialObjects-4.8\
 -lITKIOStimulate-4.8\
 -lITKIOTIFF-4.8\
 -lITKIOTransformBase-4.8\
 -lITKIOTransformHDF5-4.8\
 -lITKIOTransformInsightLegacy-4.8\
 -lITKIOTransformMatlab-4.8\
 -lITKIOVTK-4.8\
 -lITKIOXML-4.8\
 -lITKKLMRegionGrowing-4.8\
 -lITKLabelMap-4.8\
 -lITKMesh-4.8\
 -lITKMetaIO-4.8\
 -lITKniftiio-4.8\
 -lITKNrrdIO-4.8\
 -lITKOptimizers-4.8\
 -lITKOptimizersv4-4.8\
 -lITKPath-4.8\
 -lITKPolynomials-4.8\
 -lITKQuadEdgeMesh-4.8\
 -lITKSpatialObjects-4.8\
 -lITKStatistics-4.8\
 -lITKVideoCore-4.8\
 -lITKVideoIO-4.8\
 -lITKVNLInstantiation-4.8\
 -lITKVTK-4.8\
 -lITKVtkGlue-4.8\
 -lITKWatersheds-4.8\
 -lITKznz-4.8\
 -litksys-4.8\
 -litkvnl-4.8 \
 -litkvnl_algo-4.8

對於INCLUDE代碼後面要加上本身的ITK和VTK的include文件夾,注意本身的在哪裏,寫正確位置,後面的LIBS通常在/usr/local/lib/目錄下,注意把原來前面的lib換成-l,最前面要有空格,最後的.so要刪除,鏈接用"\"符號。
app

test.cxx文件,代碼以下:ide

#include "itkImageFileReader.h"
#include "itkImageToVTKImageFilter.h"

#define vtkRenderingCore_AUTOINIT 4(vtkInteractionStyle,vtkRenderingFreeType,vtkRenderingFreeTypeOpenGL,vtkRenderingOpenGL)
#define vtkRenderingVolume_AUTOINIT 1(vtkRenderingVolumeOpenGL)
//注意以上兩句 cmake管理能夠不用
#include <vtkSmartPointer.h>
#include "vtkImageViewer2.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkInteractorStyleImage.h"
#include "itkJPEGImageIOFactory.h"
#include "vtkImageMapper3D.h"
#include "vtkImageActor.h"

//#include "vtkJPEGReader.h"
#define JPEG_IMAGE_FILE_PATH "/home/jason/123.jpg"
//#include "itkRescaleIntensityImageFilter.h"


using namespace itk;

int main(int argc, char *argv[])
{
    typedef itk::Image<itk::RGBPixel<unsigned char>, 2> ImageType;
    typedef itk::ImageFileReader<ImageType>             ReaderType;
    typedef itk::ImageToVTKImageFilter<ImageType>       ConnectorType;
    itk::JPEGImageIOFactory::RegisterOneFactory();    //注意這裏的註冊,用cmake管理能夠不用
    ReaderType::Pointer reader = ReaderType::New();
    ConnectorType::Pointer connector = ConnectorType::New();

    reader->SetFileName(JPEG_IMAGE_FILE_PATH);
    connector->SetInput(reader->GetOutput());

    vtkSmartPointer<vtkImageActor> actor =
      vtkSmartPointer<vtkImageActor>::New();
//必定要注意下面的代碼,否則多是一片黑
  #if VTK_MAJOR_VERSION <= 5
    actor->SetInput(connector->GetOutput());
  #else
    connector->Update();
    actor->GetMapper()->SetInputData(connector->GetOutput());
  #endif
    vtkSmartPointer<vtkRenderer> renderer =
      vtkSmartPointer<vtkRenderer>::New();
    renderer->AddActor(actor);
    renderer->ResetCamera();

    vtkSmartPointer<vtkRenderWindow> renderWindow =
      vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);

    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
      vtkSmartPointer<vtkRenderWindowInteractor>::New();
    vtkSmartPointer<vtkInteractorStyleImage> style =
      vtkSmartPointer<vtkInteractorStyleImage>::New();

    renderWindowInteractor->SetInteractorStyle(style);

    renderWindowInteractor->SetRenderWindow(renderWindow);
    renderWindowInteractor->Initialize();

    renderWindowInteractor->Start();

    return EXIT_SUCCESS;
}


若是使用cmake管理項目,只要在CMakeLists.txt寫上網站

cmake_minimum_required(VERSION 2.4)
project(test)
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(test MACOSX_BUNDLE test.cxx)
target_link_libraries(test  ${Glue}  ${VTK_LIBRARIES} ${ITK_LIBRARIES})

就能夠了,記得改文件的名字,個人是test.cpp。ui

若是還有疑問能夠上一下兩個網站參考:spa

http://www.itk.org/Wiki/ITK/Examples
code

http://www.vtk.org/Wiki/VTK/Examples


二 錯誤

1.以前除了兩個與vnl有關的錯誤,具體報錯忘記了,能夠在.pro中添加

 -litkvnl-4.8 \
 -litkvnl_algo-4.8

以前的文件已經添加了能夠在最後面找到。


2.用通常的cmake管理項目時,ITK讀取文件能夠直接用

const char * ImageFileName = "";
const unsigned int Dimension = 2;
    
typedef unsigned char PixelType;
typedef itk::Image< PixelType, Dimension > ImageType;
    
typedef itk::ImageFileReader< ImageType >  ReaderType;
ReaderType::Pointer reader = ReaderType::New();

reader->SetFileName(ImageFileName );
try
{
    reader->Update();
    return EXIT_FAILURE;
}
catch( itk::ImageFileReaderException & )
{
    std::cout << "fail.\n" << std::endl;
}

直接用採用Qt項目,用ITK來讀取圖片的時候,直接這樣寫會報錯誤

terminate called after throwing an instance of 'itk::ImageFileReaderException'
  what():  /usr/local/include/ITK-4.8/itkImageFileReader.hxx:143:
 Could not create IO object for reading file /home/jason/program/ITK-4/Examples/Data/BrainT1Slice.png
  There are no registered IO factories.
  Please visit http://www.itk.org/Wiki/ITK/FAQ#NoFactoryException to diagnose the problem.

http://www.itk.org/Wiki/ITK/FAQ#NoFactoryException這裏給出的解釋是:

(1)若是是用cmake,須要在add_executable 或者 add_library以前添加

        include( ${ITK_USE_FILE} )

(2)若是沒有使用cmake,必須對factories進行註冊,可使用

        XXXImageIOFactory::RegisterOneFactory(),XXX是類型

具體的代碼:

    const char * metaImageFileName = "";
    const char * pngFileName = "";
    
    const unsigned int Dimension = 2;
    
    typedef unsigned char PixelType;
    typedef itk::Image< PixelType, Dimension > ImageType;
    
    typedef itk::ImageFileReader< ImageType >  ReaderType;
    ReaderType::Pointer reader = ReaderType::New();
    
    typedef std::list< itk::LightObject::Pointer > RegisteredObjectsContainerType;
    
    
    RegisteredObjectsContainerType registeredIOs =
    itk::ObjectFactoryBase::CreateAllInstance( "itkImageIOBase" );
    std::cout << "When CMake is not used to register the IO classes, there are\n"
                << registeredIOs.size()
                << " IO objects available to the ImageFileReader.\n" << std::endl;
    
    
    std::cout << "When we try to read a MetaImage, we will ";
    reader->SetFileName( metaImageFileName );
    try
    {
        reader->Update();
        return EXIT_FAILURE;
    }
    catch( itk::ImageFileReaderException & )
    {
        std::cout << "fail.\n" << std::endl;
    }
    
    
    std::cout << "After registering the MetaImageIO object, ";
    itk::MetaImageIOFactory::RegisterOneFactory();
    
    std::cout << "there are\n";
    registeredIOs = itk::ObjectFactoryBase::CreateAllInstance( "itkImageIOBase" );
    std::cout << registeredIOs.size()
                << " IO objects available to the ImageFileReader.\n" << std::endl;
    
    std::cout << "Now, when we try to read a MetaImage, we will ";
    try
    {
        reader->Update();
        std::cout << "succeed.\n" << std::endl;
    }
    catch( itk::ImageFileReaderException & error )
    {
        std::cerr << "Error: " << error << std::endl;
        return EXIT_FAILURE;
    }
    
    
    std::cout << "Every format desired to be supported by the reader\n"
                << "must be registered." << std::endl;
    itk::PNGImageIOFactory::RegisterOneFactory();
    reader->SetFileName( pngFileName );
    try
    {
        reader->Update();
    }
    catch( itk::ExceptionObject & error )
    {
        std::cerr << "Error: " << error << std::endl;
        return EXIT_FAILURE;
    }

參考代碼:http://itk.org/ITKExamples/src/IO/ImageBase/RegisterIOFactories/Documentation.html


3.若是出現一下錯誤,找不到vtkImageSliceMapper,

Starting /home/jason/QtProjects/build-vtkQtTest-unknown-Debug/vtkQtTest...
Generic Warning: In /home/jason/program/VTK-6.2.0/Rendering/Core/vtkImageSliceMapper.cxx, line 35
Error: no override found for 'vtkImageSliceMapper'.

Generic Warning: In /home/jason/program/VTK-6.2.0/Rendering/Core/vtkRenderWindow.cxx, line 40
Error: no override found for 'vtkRenderWindow'.

Generic Warning: In /home/jason/program/VTK-6.2.0/Rendering/Core/vtkRenderer.cxx, line 46
Error: no override found for 'vtkRenderer'.

須要在#include任何有關vtk的頭文件以前添加

#define vtkRenderingCore_AUTOINIT 4(vtkInteractionStyle,vtkRenderingFreeType,vtkRenderingFreeTypeOpenGL,vtkRenderingOpenGL)
#define vtkRenderingVolume_AUTOINIT 1(vtkRenderingVolumeOpenGL)

這兩句,具體爲何尚未搞懂,若是有人知道但願能告知。

也可使用代碼

#include <vtkAutoInit.h>
 VTK_MODULE_INIT(vtkRenderingOpenGL);

可是會出現其它警告

Starting /home/jason/QtProjects/build-vtkQtTest-unknown-Debug/vtkQtTest...
Warning: In /home/jason/program/VTK-6.2.0/Rendering/Core/vtkInteractorStyleSwitchBase.cxx, line 43
vtkInteractorStyleSwitchBase (0x1924210): Warning: Link to vtkInteractionStyle for default style selection.

緣由還未分析。

若是是使用cmake管理,那麼就不會有這個錯誤,在CMakeLists.txt裏都定義好了,不會有問題。

相關文章
相關標籤/搜索