48
This work by John Galeotti and Damion Shelton, © 2004-2017, was made possible in part by NIH NLM contract# HHSN276201000580P, and is licensed under a Creative Commons Attribution 3.0 Unported License . To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative Commons, 171 2nd Street, Suite 300, San Francisco, California, 94105, USA. Permissions beyond the scope of this license may be available by emailing [email protected]. Methods in Medical Image Analysis - Spring 2017 16-725 (CMU RI) : BioE 2630 (Pitt) Dr. John Galeotti Based in part on Damion Shelton’s slides from 2006 Lecture 4 Getting Started with ITK! 1

Lecture 4 Getting Started with ITK!

  • Upload
    abia

  • View
    46

  • Download
    0

Embed Size (px)

DESCRIPTION

Lecture 4 Getting Started with ITK!. Methods in Medical Image Analysis - Spring 2014 BioE 2630 (Pitt) : 16-725 (CMU RI) 18-791 (CMU ECE) : 42-735 (CMU BME) Dr. John Galeotti Based in part on Shelton ’ s slides from 2006. Goals for this lecture. Compile, compile, compile - PowerPoint PPT Presentation

Citation preview

Page 1: Lecture  4 Getting Started with ITK!

This work by John Galeotti and Damion Shelton, © 2004-2017, was made possible in part by NIH NLM contract# HHSN276201000580P, and is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative Commons, 171 2nd Street, Suite 300, San Francisco, California, 94105, USA. Permissions beyond the scope of this license may be available by emailing [email protected] most recent version of these slides may be accessed online via http://itk.galeotti.net/

Methods in Medical Image Analysis - Spring 201716-725 (CMU RI) : BioE 2630 (Pitt)

Dr. John Galeotti

Based in part on Damion Shelton’s slides from 2006

Lecture 4Getting Started with ITK!

1

Page 2: Lecture  4 Getting Started with ITK!

Goals for this lecture

Compile, compile, compile Learn how to use SVN & CMakeBuild ITKCompile several programs that use ITK

Find documentation onlineLearn the quirks (if any) of the system you

choose to use

2

Page 3: Lecture  4 Getting Started with ITK!

Getting help

Email your instructorJoin the insight-users mailing list; instructions

are at http://www.itk.org

3

Page 4: Lecture  4 Getting Started with ITK!

Assignments

Collaboration is encouraged; unless told otherwise, feel free to discuss assignments with other students

But… please submit your own code - don’t copy and paste stuff from friends

More so than other classes, you will be learning techniques that translate directly to the real world - don’t cheat yourself

4

Page 5: Lecture  4 Getting Started with ITK!

Grading of assignments

Grading criteria:Does it accomplish the specified task? Is it well commented? Follow the “6 month rule” - if

you leave for 6 months, you should be able to pick up where you left off.

Many/most assignments will be divided into sections, with each section pass-fail. We may give opportunities to fix “stupid” problems before

final judgment is passed

5

Page 6: Lecture  4 Getting Started with ITK!

Assignments, cont.

Please interpret due dates as absolute, unless told otherwise

ReallyWe’re happy to spend time helping you debug

code, but not at 11 pm the day before the assignment is due

6

Page 7: Lecture  4 Getting Started with ITK!

Computer requirements: recommended

Your own computer is preferableCluster machines should also workPlease be aware that ITK can consume a lot of disk

space during the build processWindows 7 through 10; with Visual Studio

2010, 2012, or 2015; Python 2.7 (32)OS X Mavericks through Sierra, X-Code, Python

2.7 (64)

7

Page 8: Lecture  4 Getting Started with ITK!

Alternative usable computer configurations

Any platform supported by ITK (Mac, Linux, etc.) If there are problems, you will have to work with us to get

your code working on one of our machines. Try having us check your code before it is due.

If the grader’s computer can’t run your code, you will have a short (but reasonable) period of time to fix it after he emails you that your code appears broken (along with what errors he got). If you are trying to make things work, but have many things to “fix,”

then more time may be granted. For final projects, we may decide to let you show us your code running

on your own machine, on a case-by-case basis.

8

Page 9: Lecture  4 Getting Started with ITK!

What is ITK?

To clarify, ITK is a toolkit It doesn’t “do” anythingYou can’t “run” itThere isn’t an itk.exe file

Typically, you use ITK in conjunction with other toolkits to handle visualization and GUI interaction

9

Page 10: Lecture  4 Getting Started with ITK!

So, what’s it good for?

ITK code is easy to add to existing C++ codeAlso Python, C#, Java, …

It provides a variety of flexible data containers, and ways of processing / analyzing them

You can do a lot in only a few lines of codeOnce you get used to it, it’s easy to use (gasp!)

10

Page 11: Lecture  4 Getting Started with ITK!

What we assume you can do

Understand C++ and/or Python syntax Standard flow control such as for, do, calling

functions, etc.Classes Inheritance For C++: Pointers, dereferencing, passing by

referenceWork comfortably in the operating system of

your choice, using the compiler or Python environment of your choice

11

Page 12: Lecture  4 Getting Started with ITK!

You may have not…

Used revision control using SVN (or CVS)Engaged in collaborative programmingWritten C++ code that builds on multiple

platformsUsed cross-platform make software

(CMake or Jam, for example)Designed software using a data-flow

architecture, worried about smart pointers, etc.

12

Page 13: Lecture  4 Getting Started with ITK!

Revision control with SVN

Revision control software allows you to store incremental changes to software

You will be expected to use SVN to manage your homework assignments SVN is like CVS, but better

I encourage you to use revision control on your code outside of this class as well - it’s a good habit to develop

13

Page 14: Lecture  4 Getting Started with ITK!

SVN terms

Server - what it sounds likeModule - a group of files that can be accessed

on the serverUser - each module has associated users, with

varying levels of access (read only, read/write, etc.).

14

Page 15: Lecture  4 Getting Started with ITK!

SVN terms, cont.

Checkout - Download a fresh copy of a module from the server to your computer

Update - Sync your copy of a module with the server copy; much faster than a checkout

Commit - Merge changes made to your local copy with the server

15

Page 16: Lecture  4 Getting Started with ITK!

SVN setup

The SVN server for this course will be:https://svn.vialab.org/svn/miia17/{your user name}

You will each have a module, based on your email; you will get email about this in a week or two.

Only you and the instructors will have access to this module

16

Page 17: Lecture  4 Getting Started with ITK!

SVN setup, cont.

GUI wrappers for SVNWindows: Tortoise SVN http://tortoisesvn.net/

Mac: svnX http://code.google.com/p/svnx/

Windows, Mac, and Linux (cross-platform): RapidSVN: http://rapidsvn.org/download/release/0.12.1/ SmartSVN: http://www.wandisco.com/subversion/download

Command line works fine too, but may be more awkward if you’re used to GUI’s

17

Page 18: Lecture  4 Getting Started with ITK!

Cross platform (C++) development

ITK builds on a large combination of operating systems and platforms

For C++, each compiler has it’s own input format: Makefiles, workspaces, etc.

Q: How can you possibly coordinate builds on different platforms?

18

Page 19: Lecture  4 Getting Started with ITK!

The answer: CMake

Cross platform tool to manage the build process

Simplifies the build processAuto-configurationEasy access to external librariesUsed by several other open

source projects

19

www.cmake.org

Page 20: Lecture  4 Getting Started with ITK!

CMake is:

Required to build native (C++) ITKCross-platform project generatorOften simpler than particular environmentsText as inputProject file as output:

20

Windows Visual Studio Solution

UNIX MakefileMac OS X Xcode project or Makefile

Page 21: Lecture  4 Getting Started with ITK!

How CMake runs

Write a CMakeLists.txt file describing your project in CMake’s language

Run CMake to generate an appropriate makefile/project/workspace for your compiler

Compile as you normally would

21

Page 22: Lecture  4 Getting Started with ITK!

How CMake runs, cont.

This is not unlike the configure-make process you may be familiar with from various Unix systems

But… it works with many compilersCMakeLists.txt files are easy to perform revision

control on

22

Page 23: Lecture  4 Getting Started with ITK!

CMakeLists.txt syntax

Comment lines indicated with # Look at examples in ITK Simple example:

cmake_minimum_required(VERSION 2.8)#Make sure the user’s CMake is recent enough

#Give this project a name:PROJECT(cool_demo)

#The command-line executable “demo”#is built from “demo_code.cxx” and#must be linked with the ITK librariesADD_EXECUTABLE(demo demo_code.cxx)TARGET_LINK_LIBRARIES(demo ${ITK_LIBRARIES})

23

Page 24: Lecture  4 Getting Started with ITK!

Full Example of CMakeLists.txt

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)# CMake poicies sometimes change, so it is# recommended that new projects require the# latest CMake version

PROJECT(cool_demo)

# Find ITK:FIND_PACKAGE(ITK REQUIRED)INCLUDE(${ITK_USE_FILE})

ADD_EXECUTABLE(demo demo_code.cxx)TARGET_LINK_LIBRARIES(demo ${ITK_LIBRARIES})

24

Page 25: Lecture  4 Getting Started with ITK!

Steps to get started with ITK

Pay AttentionA similar process is used in HW2, but for C++

SimpleITKHW2 does not directly install ITK Instead, HW2 uses this process to compile SimpleITKCompiling SimpleITK automatically downloads and

compiles ITK for us

25

Page 26: Lecture  4 Getting Started with ITK!

Step 0 – Don’t panic!

There is substantial documentation on everything I’m going to present here, and vastly more about things that we will never cover in this course

http://www.itk.org/ITK/help/documentation.html

Download a copy of the ITK Software Guide

26

Page 27: Lecture  4 Getting Started with ITK!

Step 1 - Install CMake

Check if a recent version of CMake is already installed on your computer.

If not, …Download and install a binary distribution of CMake

3.4.1 from:http://www.cmake.org/

27

Page 28: Lecture  4 Getting Started with ITK!

Step 2 - Install ITK

Note: If you are going to compile SimpleITK, then do not install ITK directly SimpleITK’s “super build” will compile & install ITK for you …Following a procedure similar to that for ITK

Check if ITK 4.8.2 is already installed on your computer. If not, …Download the latest version of InsightToolkit:

http://www.itk.org/ITK/resources/software.htmlExtract, e.g., InsightToolkit-4.8.2.zip to your working source

directory for this class

28

Page 29: Lecture  4 Getting Started with ITK!

In source vs. out source builds

29

Source Tree

ITK

Common

Algorithms

BasicFilter

Numerics

IO

ITKb

Common

Algorithms

BasicFilter

Numerics

IO

Binary Tree

Recommended !

Out Source Build

InSourceBuild

Page 30: Lecture  4 Getting Started with ITK!

Why use two trees?

Keeps your C++ source and binary code separate

Minimizes the amount of damage you can do to your SVN tree

ITK is found in the InsightToolkit-4.8.2 folderWe suggest that you build it in a new folder you

create named InsightBin

30

Page 31: Lecture  4 Getting Started with ITK!

Configure - Easy Start

Run CMake

Select the SOURCE directory

Select the BINARY directory

Pres the “Configure” button

31

Page 32: Lecture  4 Getting Started with ITK!

CMake may ask to choose which compiler (“generator”) you want to use for compiling:

On my OS X system, here are the choices:

32

CMake: Choosing a Compiler

• On Windows, choose your version of Visual Studio• On OS X, choose Xcode (recommended) or ”Unix Makefiles”

Page 33: Lecture  4 Getting Started with ITK!

Configure - Easy Start, cont.

33

Page 34: Lecture  4 Getting Started with ITK!

Configure - Easy Start, cont.

Disable BUILD_EXAMPLES

Disable BUILD_TESTS

Disable BUILD_SHARED_LIBS

34

Page 35: Lecture  4 Getting Started with ITK!

Configuring and Generating

Each time you change an option or options you may need to “configure” CMake again

If the generate option (“OK” under Windows) is not presented, you definitely need to hit configure again

If any of the options are highlighted in red, you need to reconfigure

35

Page 36: Lecture  4 Getting Started with ITK!

36

Open the ITK Visual Studio Solution file in the Binary Directory

Select BuildBuild SolutionIt will probably take somewhere between 10 - 40

minutes, but your mileage may vary

Build ITK

Page 37: Lecture  4 Getting Started with ITK!

Verify the Build

Libraries will be found in:

ITK_BINARY / bin / { Debug, Release}

37

Page 38: Lecture  4 Getting Started with ITK!

Building with Makefiles and gcc

Order of operations is the sameDifferences

Can only configure for a debug OR a release buildRun the ccmake executable, with one of these: -DCMAKE_BUILD_TYPE=DEBUG -DCMAKE_BUILD_TYPE=RELEASE

ccmake uses a curses TUI, “identical” to the GUIRun make instead of Visual StudioThink of CMake as replacing the “./configure” step

you may be used to

38

Page 39: Lecture  4 Getting Started with ITK!

Building with Makefiles and gcc

Start in directory containing InsightToolkit-4.8.2mkdir InsightBinDebugcd InsightBinDebugccmake ../InsightToolkit-4.8.2 -DCMAKE_BUILD_TYPE=DEBUGEdit CMake optionsReconfigure if neededmake

39

Page 40: Lecture  4 Getting Started with ITK!

Now what?

At this point, you should have two things:A directory containing a bunch of source code E.g. …/MIIA/InsightToolkit-4.8.2/

A directory containing the built ITK libraries E.g. …/MIIA/InsightBinDebug

As mentioned earlier, you don’t have anything executable

40

Page 41: Lecture  4 Getting Started with ITK!

Building an application

ITK comes with a simple application you can build in order to test the ITK libraries “out of source” (i.e. not built inside ITK)

It can be found in:InsightToolkit-4.8.2/Examples/Installation

41

Page 42: Lecture  4 Getting Started with ITK!

How to build HelloWorld

Copy & rename the Installation directory somewhere outside of the Insight directory

Run CMake on HelloWorldRemember the source/binary distinction and use

HelloWorldBin as your build locationCMake should automatically find ITK

if not, edit the ITK_DIR option

42

Page 43: Lecture  4 Getting Started with ITK!

How to build HelloWorld, cont.

Once CMake is happy, generate the makefile/project for your compiler

Build HelloWorldGive it a try

43

Page 44: Lecture  4 Getting Started with ITK!

More examples

You can turn on ITK’s Examples option in CMake, which will build all of the examples for you

Or… you can copy the examples out-of-source and build them like you did HelloWorld

These examples link into ITK Software Guide; read the chapter, poke the code and see what happens…

44

Page 45: Lecture  4 Getting Started with ITK!

C++ Workflow thoughts

You should get used to the idea of:1. Writing some code2. Writing a CMakeLists.txt file3. Running CMake4. Building your code5. Rinse, repeat

45

Page 46: Lecture  4 Getting Started with ITK!

An aside: how to use ITK withexisting C/C++ applications

Your existing app may not use CMakeIn this case, you need to link to the ITK libraries

explicitly and include the appropriate source directories

This isn’t hard, but it may take some trial and error to discover everything you need

You don’t need to worry about this in the context of this class

46

Page 47: Lecture  4 Getting Started with ITK!

ITK Documentation

Most of the ITK documentation is generated automatically from source comments using Doxygen

Please familiarize yourself with the various means of navigating the Doxygen documentation online, e.g. selecting a Module and then selecting a Class

http://www.itk.org/Doxygen48/html/index.html

47

Page 48: Lecture  4 Getting Started with ITK!

Assignment 2…

Will be assigned next week.Please do NOT start until we have it ready

Follows this pattern of CMake & CompilationFollows a slightly different approach

Uses SimpleITK to automatically compile and install ITKDon’t procrastinate! If you’re going to have compiler problems, they

will show up early in the course, so don’t procrastinate this assignment.

48