整合这三个,对于刚接触的新手我来说是一件很复杂的玩意,根本无从下手,网络整合的资源也不多,而且大部分是用cmake来管理项目,直接用过Qt的.pro文件来管理项目还是很少的,因此通过这次整合经历,记录出现问题,以便翻阅。
一 整合
通过Qtcreator创建Qt项目,会出现带有.pro文件的项目。
编辑.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要删除,连接用"\"符号。
test.cxx文件,代码如下:
#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。
如果还有疑问可以上一下两个网站参考:
http://www.itk.org/Wiki/ITK/Examples
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里都定义好了,不会有问题。