This work by John Galeotti and Damion Shelton, © 2004-2015, was made possible in part by NIH NLM contract# HHSN276201000580P, and is licensed under a Creative.

Slides:



Advertisements
Similar presentations
The content of these slides by John Galeotti, © 2013 Carnegie Mellon University (CMU), was made possible in part by NIH NLM contract# HHSN P,
Advertisements

This work by John Galeotti and Damion Shelton, © , was made possible in part by NIH NLM contract# HHSN P, and is licensed under a Creative.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Chapter 17 vector and Free Store John Keyser’s Modifications of Slides By Bjarne Stroustrup
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Kernighan/Ritchie: Kelley/Pohl:
Methods in Image Analysis 2004, Damion Shelton 1 ITK Lecture 4 Images in ITK Methods in Image Analysis CMU Robotics Institute U. Pitt Bioengineering.
Images in ITK JG, DS, HJ. Data storage in ITK 2 ITK separates storage of data from the actions you can perform on data The DataObject class is the base.
This work by John Galeotti and Damion Shelton, © , was made possible in part by NIH NLM contract# HHSN P, and is licensed under a Creative.
This work by John Galeotti and Damion Shelton, © , was made possible in part by NIH NLM contract# HHSN P, and is licensed under a Creative.
1 ITK Lecture 7 - Writing Filters, Part 1 Methods in Image Analysis CMU Robotics Institute U. Pitt Bioengineering 2630 Spring Term, 2006 Methods.
This work by John Galeotti and Damion Shelton, © , was made possible in part by NIH NLM contract# HHSN P, and is licensed under a Creative.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
CS 61C L03 C Arrays (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #3: C Pointers & Arrays
C++ for Engineers and Scientists Third Edition
The content of these slides by John Galeotti, © 2008 to 2015 Carnegie Mellon University (CMU), was made possible in part by NIH NLM contract# HHSN P,
The content of these slides by John Galeotti, © Carnegie Mellon University (CMU), was made possible in part by NIH NLM contract# HHSN P,
1 ITK Lecture 4 Images in ITK Methods in Image Analysis CMU Robotics Institute U. Pitt Bioengineering 2630 Spring Term, 2006 Methods in Image Analysis.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
Array.
ICS220 – Data Structures and Algorithms Lecture 10 Dr. Ken Cosh.
The content of these slides by John Galeotti, © Carnegie Mellon University (CMU), was made possible in part by NIH NLM contract# HHSN P,
Lists in Python.
CS352-Week 2. Topics Heap allocation References Pointers.
The content of these slides by John Galeotti, © Carnegie Mellon University (CMU), was made possible in part by NIH NLM contract# HHSN P,
The content of these slides by John Galeotti, © Carnegie Mellon University (CMU), was made possible in part by NIH NLM contract# HHSN P,
The content of these slides by John Galeotti, © Carnegie Mellon University (CMU), was made possible in part by NIH NLM contract# HHSN P,
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Game Programming in Java Dr. Jeyakesavan Veerasamy CS faculty, The University of Texas at Dallas Website:
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts.
1 ITK Lecture 8 - Neighborhoods Methods in Image Analysis CMU Robotics Institute U. Pitt Bioengineering 2630 Spring Term, 2006 Methods in Image.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Methods in Image Analysis 2004, Damion Shelton 1 ITK Lecture 9 - Toolbox Part I Methods in Image Analysis CMU Robotics Institute U. Pitt Bioengineering.
Linked List. Background Arrays has certain disadvantages as data storage structures. ▫In an unordered array, searching is slow ▫In an ordered array, insertion.
CSC 211 Data Structures Lecture 13
Lecture 6 : Intro. to Generic Programming Acknowledgement : courtesy of Prof. Dekai Wu lecture slides.
C++ Memory Overview 4 major memory segments Key differences from Java
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Object-Oriented Programming in C++
1 ITK Lecture 9 - Toolbox Part I Methods in Image Analysis CMU Robotics Institute U. Pitt Bioengineering 2630 Spring Term, 2006 Methods in Image.
The content of these slides by John Galeotti, © Carnegie Mellon University (CMU), was made possible in part by NIH NLM contract# HHSN P,
CS 31 Discussion, Week 8 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:00-1:00pm.
CSC 107 – Programming For Science. Today’s Goal  Become familiar with simple arrays  Declaring an array variable  Assigning data to array entries 
Homework due Test the random number generator Create a 1D array of n ints Fill the array with random numbers between 0 and 100 Compute and report the average.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
1 ITK Lecture 5 Intro to Iterators Methods in Image Analysis CMU Robotics Institute U. Pitt Bioengineering 2630 Spring Term, 2006 Methods in Image.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Announcements You will receive your scores back for Assignment 2 this week. You will have an opportunity to correct your code and resubmit it for partial.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
CISC/CMPE320 - Prof. McLeod
Lecture 16 Images in ITK Methods in Medical Image Analysis - Spring (CMU ECE) : (CMU BME) : BioE 2630 (Pitt) Dr. John Galeotti Based.
SPL – PS2 C++ Memory Handling.
Presentation transcript:

This work by John Galeotti and Damion Shelton, © , was made possible in part by NIH NLM contract# HHSN P, and is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit 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 ing Commons Attribution 3.0 Unported License The most recent version of these slides may be accessed online via Lecture 16 Images in ITK Methods in Medical Image Analysis - Spring 2015 BioE 2630 (Pitt) : (CMU RI) (CMU ECE) : (CMU BME) Dr. John Galeotti Based on Shelton’s slides from

Data storage in ITK 2

Data containers in ITK  Images: N-d rectilinear grids of regularly sampled data  Meshes: N-d collections of points linked together into cells (e.g. triangles)  Meshes are outside the scope of this course  (Meshes are covered in section 4.3 of the ITK Software Guide, Book 1.)  ITK Spatial Objects  May discuss in a future lecture  ITK Path Objects  Final Lecture 3

What is an image?  For our purposes, an image is an N-d rectilinear grid of data  Images can contain any type of data, although scalars (e.g. grayscale) or vectors (e.g. RGB color) are most common  We will deal mostly with scalars, but keep in mind that unusual images (e.g. linked-lists as pixels) are perfectly legal in ITK 4

Images are templated 5 Pixel typeDimensionality (value)

An aside: smart pointers 6

Danger!  Suppose you allocate memory in a function and forget to call delete prior to returning… the memory is still allocated, but you can’t get to it  This is a memory leak  Leaking doubles or chars can slowly consume memory, leaking 200 MB images will bring your computer to its knees 7

Smart pointers to the rescue  Smart pointers get around this problem by allocating and deallocating memory for you  You do not explicitly delete objects in ITK, this occurs automatically when they go out of scope  Since you can’t forget to delete objects, you can’t leak memory 8 (ahem, well, you have to try harder at least)

Smart pointers, cont.  This is often referred to as garbage collection - languages like Java have had it for a while, but it’s fairly new to C++  Keep in mind that this only applies to ITK objects - you can still leak arrays of floats/chars/widgets until your heart’s content…or your program crashes. 9

Why are smart pointers smart?  Smart pointers maintain a “reference count” of how many copies of the pointer exist  If N ref drops to 0, nobody is interested in the memory location and it’s safe to delete  If N ref > 0 the memory is not deleted, because someone still needs it 10

Scope  Refers to whether or not a variable exists within a certain segment of the code.  When does a variable cease to exist?  Local vs. global  Example: variables created within member functions typically have local scope, and “go away” when the function returns 11

Scope, cont.  Observation: smart pointers are only deleted when they go out of scope (makes sense, right?)  Problem: what if we want to “delete” a SP that has not gone out of scope; there are good reasons to do this, e.g. loops 12

Scope, cont. 13

A final caveat about scope  Don’t obsess about it  99% of the time, smart pointers are smarter than you!  1% of the time you may need to haul out the previous trick 14

Images and regions  ITK was designed to allow analysis of very large images, even images that far exceed the available RAM of a computer  For this reason, ITK distinguishes between an entire image and the part which is actually resident in memory or requested by an algorithm 15

Image regions 16 LargestPossibleRegion::Index BufferedRegion::Index RequestedRegion::Index RequestedRegion::Size BufferedRegion::Size LargestPossibleRegion::Size

Image regions, cont. 17

Data space vs. “physical” space  Data space is what you probably think of as “image coordinates”  Data space is an N-d array with integer indices, each indexed from 0 to (L i - 1)  e.g. pixel (3,0,5) in 3D space  Physical space relates to data space by defining the origin and spacing of the image 18 Length of side i

19 Figure 4.1 from the ITK Software Guide v 2.4, by Luis Ibáñez, et al.

Creating an image: step-by-step  Note: this example follows from the ITK Software Guide’s Book 1, but differs in content - please be sure to read the guide as well  This example is provided more as a demonstration than as a practical example - in the real world images are often/usually provided to you from an external source rather than being explicitly created 20

Declaring an image type 21

A syntax note 22 (well, not if you were paying attention during the ITK background lecture)

Syntax note, cont.  This illustrates one criticism of templates and typedefs—it’s easy to invent something that looks like a new programming language!  Remember that names ending in “Type” are types, not variables or class names  Doxygen is your friend - you can find developer- defined types under “Public Types” 23

Creating an image pointer 24 Pointer is typedef’d in itk::ImageNote the use of “big New”

A note about “ big New ” 25

When not to use ::New() 26

Setting up data space 27 SizeType is another typedef

Setting up data space, cont. 28 Note that the index object start was not created with ::New()

Setting up data space, cont. 29 region was also not created with ::New()

Allocating the image 30

Dealing with physical space  At this point we have an image of “pure” data; there is no relation to the real world  Nearly all useful medical images are associated with physical coordinates of some form or another  As mentioned before, ITK uses the concepts of origin and spacing to translate between physical and data space 31

Image spacing 32

Image origin 33

Origin/spacing units  There are no inherent units in the physical coordinate system of an image—i.e. referring to them as mm’s is arbitrary (but very common)  Unless a specific algorithm states otherwise, ITK does not understand the difference between mm/inches/miles/etc. 34

Direct pixel access in ITK  There are many ways to access pixels in ITK  The simplest is to directly address a pixel by knowing either its:  Index in data space  Physical position, in physical space 35

Why not to directly access pixels  Direct pixels access is simple conceptually, but involves a lot of extra computation (converting pixel indices into a memory pointer)  There are much faster ways of performing sequential pixel access, through iterators 36

Accessing pixels in data space 37

Pixel access in data space 38 (the type of pixel stored in the image)

Why the runaround with PixelType? 39

PixelType, cont.  Well… nothing is wrong in this example  But, when writing general-purpose code we don’t always know or control the type of pixel stored in an image  Referring to ImageType will allow the code to compile for any type that defines the = operator (float, int, char, etc.) 40

PixelType, cont. 41

Accessing pixels in physical space 42

Defining a point 43

Why do we need a Point?  The image class contains a number of convenience methods to convert between pixel indices and physical positions (as stored in the Point class)  These methods take into account the origin and spacing of the image, and do bounds-checking as well (i.e., is the point even inside the image?) 44

TransformPhysicalPointToIndex 45

The transform in action 46

Point and index transforms 47

Ways of accessing pixels 48

Walking through an image 49

Image traversal, cont.  The loop technique is easy to understand but:  Is slow  Does not scale to N-d  Is unnecessarily messy from a syntax point of view  Iterators are a way around this 50

Why direct access is bad 1… It’s slow: a)Build the index b)Pass the index to the image c)Build a memory address from the index d)Access the pixel 51

Direct access = bad, cont. 2… It’s hard to make it N-d: Let’s say I want to do something really simple, like access all of the pixels in an image (any data type, any dimensionality) How would you do this using indices? 52

N-d access troubles  You could probably come up with a fairly complicated way of building an index  But, nested for-loops will not work, because you don’t know ahead of time how many loops to nest 53

N-d access troubles, cont. 54

Iterators to the rescue  There’s a concept in generic programming called the iterator  It arises from the need to sequentially & efficiently access members of complex data objects  Iterators are not unique to ITK; the Standard Template Library (STL) uses them extensively 55

Iterators in ITK  ITK has many types of iterators. There are iterators to traverse:  image regions  neighborhoods  arbitrary functions (“inside” the function)  random pixels in an image  and more…  We’ll be covering several of these in class 56

See the software guide  All this and more can be found in Chapter 6 of the ITK Software Guide, Book 1 57

Good news about iterators Iterators are:  Simple to learn and use, and make your code easier to read (!)  Wrap extremely powerful data access methods in a uniform interface  N-d  Fast 58

An aside: “Concepts” in ITK  One of the ways the Doxygen documentation is organized is by concepts  This helps sort classes by similar functionality (concepts) even if they don’t share base classes   Iterators are one of the concepts you can look up 59

Image region iterators 60

Creating the iterator 61

Using the iterator 62

More compact notation 63

Image regions 64

Other iterator tricks  Access the pixel with Get()  Figure out the Index of the pixel with GetIndex()  Get a reference to a pixel with Value()  Value() is somewhat faster than Get() 65

Iterator tricks, cont. 66

Const vs. non-const iterators  You will notice that most iterators have both const and non-const versions  Const iterators do not allow you to set pixel values (much like const functions don’t allow you to change class member values)  In general, the non-const versions of each iterator derive from the const 67

Const vs. non-const, cont.  Good programming technique is to enforce const access when you don’t intend to change data  Moreover, input images in ITK filters are const, so you can’t traverse them using non-const iterators  Why? It’s very important to not accidentally modify the input to a filter! 68

Problems with iterating regions  It’s not easy to know “who” your neighbors are, which is often important  You don’t have much control over how the iteration proceeds (why?)  Fortunately, there are solutions to both of these problems… stay tuned 69