【003】ITK 之 HelloWorld

ITK 之 HelloWorld



0.前言

前面一篇文章已經編譯並配置好了ITK ,我們如何使用 ITK 庫 呢?
這裏,我選擇了 Qt Creator 作爲 IDE ,使用 CMake 管理項目的方式使用 ITK 庫

1.新建 HelloWorld 項目

  • 打開Qt Creator 選則【新建文件或項目】–> 【Non-Qt Project】–>【Plain C++ Application】
  • 這裏選擇了 非 Qt 項目,純 C++ 應用程序
    在這裏插入圖片描述
  • 指定項目的名稱
  • 項目路徑不可以有中文
    在這裏插入圖片描述
  • 此處選擇 CMake 作爲 構建工具
    在這裏插入圖片描述
  • 選擇本項目使用的構建套件,因爲我們的 ITK 庫編譯使用的是VS 2017 64位,因此,此處我們選擇了對應的MSVC2017 64 bit`。
    在這裏插入圖片描述
  • 此處可以指定版本控制,我們這裏就不指定了,直接點擊完成即可。
    在這裏插入圖片描述

2.實現 HelloWorld 代碼

2.1 CMakeLists.txt

cmake_minimum_required(VERSION 3.5)

project(HelloWorld LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(ITK REQUIRED)
#loads the UseITK.cmake file which contains the configuration information about the specified ITK build.
include(${ITK_USE_FILE})
# first argument the name of the executable that will be produced as result of this project.
# The remaining argument(s) of add executable are the names of the source files to be compiled.
add_executable(HelloWorld main.cpp)
# call specifies which ITK libraries will be linked against this project
target_link_libraries(HelloWorld ${ITK_LIBRARIES})

2.2 main.cpp

#include "itkImage.h"
#include <iostream>
using namespace std;

int main()
{
    using ImageType = itk::Image< unsigned short, 3>;
    ImageType::Pointer image = ImageType::New();

    cout << "Hello World!" << endl;
    return 0;
}

3.編譯運行

  • 一定要注意一個細節,如果我們使用的是 Debug 版本的 ITK 庫 此時我們只能選擇 Debug,構建 Release會出現庫不相符的情況而報錯。
  • 如果我們使用的是 Release 版本的 ITK 庫 此時我們只能選擇 Release
    在這裏插入圖片描述

4.運行結果

  • 如果應用程序輸出了 HelloWorld ,則我們的 ITK 庫已經可以正常使用了。
    在這裏插入圖片描述
  • 如果,您這裏報錯,找不到 ITK_DIR 建議看一下上一篇文章最後,配置 ITK 環境變量。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章