Title: Generic Programming and Boost
1Generic Programming and Boost
2Contents
- Templates
- Generic Programming
- Boost C Library
- Boost CVPR
- Coding Advices
3Templates
4Templates
- Template class
- Template function
- Template class instantiation
- Template class specialization
- Template function overloading
5Templates in C
- Great utility to programmers in C, especially
when combined with multiple inheritance and
operator overloading. - The C Standard Template Library (STL) provides
many useful functions within a framework of
connected templates. - Very expressive they may be used for things other
than generic programming. - Template metaprogramming, a way of pre-evaluating
some of the code at compile-time rather than
run-time
6Template class - Specialization
convert_floatltTgttype convert_floatltchargttype c
onvert_floatltdoublegttype
template lttypename Tgt struct convert_float
typedef float type template ltgt struct
convert_floatltdoublegt typedef double
type template ltgt struct convert_floatltlonggt
typedef double type template ltgt struct
convert_floatltlong doublegt typedef long
double type
Specialization for double
Specialization for long
Specialization for long double
7Template class Partial Specialization
CubicArrayltfloat, 3gt a CubicArrayltint, 2gt
b CubicArrayltstring, 2gt s
template lttypename T, size_t NumDimsgt struct
CubicArray CubicArrayltT, NumDims-1gt
elem16 CubicArrayltT, NumDims-1gt
operator(int i) return elemi
template lttypename Tgt struct CubicArrayltT,
1gt T elem16 T operator(int i)
return elemi
template ltgt struct CubicArrayltstring, 1gt
//...
template ltsize_t NumDimsgt struct
CubicArrayltstring, NumDimsgt CubicArrayltT,
NumDims-1gt elem16 CubicArrayltT, NumDims-1gt
operator(int i) return elemi void
clear_all()
8Template function
templateltclass T, int Ngt T max(T t, N n) int
intArray -1,0,3,2,1 char charArray
'a', 'Z', 'n', 'c' int m max(intArray, 5)
//call template int max(int a, int b)
//Overloading int w max(5, 4)
//call max(a, b) char c
max(charArray, 4) //call
template templatelt templateltclassgt class T,
int Ngt Tltintgt max(Tltintgt t, N n)
9Generic Programming
10Generic Programming
- Introduction
- Concept
- Traits / Type Generators
- Policy Classes / Tag Dispatching
- Adaptors
11Introduction
- Generic programming is a datatype-independent way
of computer programming. - The same source code will be used regardless of
the datatype the code will be instantiated with
or passed as parameters.
12Generic programming
- Generic programming is about generalizing
software components so that they can be easily
reused in a wide variety of situations. - In C, class and function templates are
particularly effective mechanisms for generic
programming because they make the generalization
possible without sacrificing efficiency.
13Concept
- Generic programming is "programming with
concepts," - Concept is defined as a family of abstractions
that are all related by a common set of
requirements.
concept ForwardIteratorlttypename Itergt
typename value_type Iter operator(Iter
x) value_type operator(Iter) bool
operator(Iter, Iter) bool operator!(Iter,
Iter) //Not a valid syntax in C now
14Policy / Tag
- Policy classes are small self-contained
implementations for specific behaviors. - Many of them are interchangeable which allows
users to choose exactly which policies they wish.
- Tag is empty classes used as options for class or
functions
15Policy / Tag
- Policy example Allocator
- templateltclass _Ty, class _Ax allocatorlt_Tygt gt
- class list
- Tag example, fast compact, property tag
- struct fast_t //Tag
- struct compact_t //Tag
- void nearest_neighbor(..., fast_t opt)
- void nearest_neighbor(..., compact_t opt)
16Traits
- Related types wrapper
- listltgtiterator,
- listltgtreference,
- listltgtvalue_type,
- unrefltgttype
- convert_floatltgttype
- graph_traitsltgtvertex_descriptor
- graph_traitsltgtedge_descriptor
- Hold relations between templated types
17Adaptor
- Wrapper existed data structure to satisfy given
concept - For example, we can wrap CImage to satisfy
boostMultiArray concept such as we can use
boostMultiArray algorithms on CImage directly.
18A real example
Template template parameter, the template type of
the input image
Template of the kernel
template lt templatelttypename, size_tgt class
MultiArray1, templatelttypename, size_tgt class
MultiArrayKer, templatelttypename, size_tgt class
MultiArray2, typename T1, typename TK, typename
T2 gt void filt( const MultiArray1ltT1, 2gt in,
const MultiArrayKerltTK, 2gt f,
MultiArray2ltT2, 2gt out, T1 (pad_pixel)(
const MultiArray1ltT1, 2gt, int,int) )
Template of the output image
Input image
kernel
Output image
Policy for how to padding pixels out of boundaries
19Example contd
Type Traits
typedef typename convert_floatltT2gttype
TF2 int cy (f.shape()0-1)/2 int cx
(f.shape()1-1)/2 for(size_t yy0 yylt
in.shape()0 yy) for(size_t xx0
xxltin.shape()1 xx) TF2 t0
for(size_t dy0 dylt f.shape()0 dy)
for(size_t dx0 dxlt f.shape()1 dx)
t pad_pixel(in, yydy-cy,
xxdx-cx)fdydx
outyyxx static_castltT2gt(t)
MultiArray Concept has requirements
20Boost
21Boost
- Boost (http//www.boost.org) is a collection of
free peer-reviewed portable C source libraries,
which includes many useful algorithms and data
structure. - String and text processing/Input/Output
- Generic Programming/Template Metaprogramming
- Containers/Iterators/Algorithms
- Function Objects and higher-order programming
- Concurrent Programming
- Math and numerics
- Correctness and testing
- Data structures/Memory
- Parsing
22Boost
- The aim of boost is to become part of C
standard in the future. Ten Boost libraries are
already included in the C Standards Committee's
Library Technical Report (TR1) as a step toward
becoming part of a future C Standard. More
Boost libraries are proposed for the upcoming
TR2. - By taking advantages of the templates and generic
programming, they are all very flexible and
configurable. My favorite libraries include
boostmulti_array, boostgraph , boostlambda,
boostrandom. - A good choice is to represent images and matrices
in boostmulti_array, which will provide many
useful features such as views and subarrays. So,
if we want to reconstruct our codes, the first
step is to modify the matrix algorithms to using
boostmulti_array, and wrapper the image loader
to produce a boostmulti_arrayltT,3gt image.
23Boost
- Boost Multidimensional Array Library
- Boost Graph Library
- Boost Lamda Library
-
24Boost multi_array Library
- Introduction
- Short Example
- Creating Views
- Facilities for CVPR
25Boost multi_array Library
- The Boost MultiArray library provides a class
template specifying an interface for
N-dimensional containers. - It includes a general array class template and
array adaptors. - The MultiArray class supports idiomatic array
operations and interoperate with C Standard
Library containers and algorithms.
26Short Example
Instantiate the template
include ltboost/multi_array.hppgt using namespace
boost int main() typedef multi_arrayltfloat,3gt
MultiArray typedef MultiArrayindex
index MultiArray A(extents343) int v
alues 0 for(index i 0 i lt 3 i )
for(index j 0 j lt 4 j ) for(index
k 0 k lt 3 k ) Aijk
values return 0
A signed integral type used for indexing into A
Create a 3D array that is 3 x 4 x 3
Access elements
27Creating Views
- The sub-view supports the similar interface as
the original array. - It can retain the same number of dimensions as
the original. - It can have less dimensions than the original.
28typedef MultiArrayarray_viewlt3gt type
view3D typedef MultiArrayarray_viewlt2gt
type view2D typedef MultiArrayindex_range
r view3D aView3 A indicesr(0,2)r(0,4,
2)r() view2D aView2 A indices0r()r()
The view type with Dims dimension template
array_viewltDimsgt type
This type specifies a range of indices over some
dimension of a MultiArray.
from beginning to end
base, stride, bound) 0,2,4) 0, 2
base,bound) 0,2) 0, 1
29Facilities for CV
- The MultiArray container perfectly suits
3D-graphics and digital image. - For 3D-grahpics, slices are gained effortlessly
with the tool of creating views. - For digital images, we can easily choose one
color channel similarly.
30Facilities for CV
- By creating a view type of the same dimensions of
the original, we can easily sample on a multigrid
lattice.
e.g. use MultiArray to contain a color image
(256256) MultiArray A(extents2562563
) create such a view view3D sample A
indicesr(0,256,4)r(0,256,4)r( )
31Facilities for CV
- MultiArray provides a flexible process on the
numbers of dimension.
e.g. (reshape in matlab) If we have a group of
images for training, Xi, with the size of m x
n. The MultiArray of 3 dimensions can be
created to contain this group. It is
convenient to train all these images (2D
(mn)xK), as well as to show the result of just
one image (3D mxnxK).
32Boost Graph Library
- Generic Data Structures
- Generic Graph Algorithms
33Generic Data Structures
- adjacency_list
- adjacency_matrix
- edge_list
- Suit directed, undirected, or bidirected graph
when specially parameterized - Provide containers for both node and edge
34Generic Graph Algorithms
- Breadth First Search
- Depth First Search
- Uniform Cost Search
- Build blocks for constructing graph algorithms,
currently including 13 in the BGL, such as
connected components.
35Boost Lambda Library
- Whats lambda
- Unnamed inplace function
- Lisp Programming Language
- If C has lambda
sort(v.begin(), v.end(), _1.timegt_2.time) transf
orm(v.begin(), v.end(), v2.begin(), sin(_1))
36Boost Lambda Library
- Unnamed functions can be created easily with an
intuitive syntax. - e.g.
- for_each( a.begin(), a.end(), cout ltlt (1
_1)) - Most of the restrictions in argument binding are
removed, arbitrary arguments of practically any
C function can be bound. - Separate function composition operations are not
needed, as function composition is supported
implicitly.
_1 Placeholder for parameters
37Lambda Examples
- For_each transform
- Sort by class member
- Pointers of each element
listltfloatgt v for_each(v.begin(), v.end(), _1
bind((float()(float))sin, _1))
struct Event //... int time vectorltEvent
gt v //... sort(v.begin(), v.end(),
bind(Eventtime, _1) gt bind(Eventtime, _2))
listltintgt v(10) vectorltintgt vp(10)
transform(v.begin(), v.end(), vp.begin(), _1)
38Boost String
- Case-insensitive compare
- Lexical cast between string and number
equal(s1.begin(), s1.end(), s2.begin(),
is_iequal)
boostlexical_castltstdstringgt(2006) 2006
boostlexical_castltshortgt(2006) 2006
39Boost CVPR
40 Boost CVPR
- Boostcvpr uses boostmulti_arrayltgt to contain
images and matrices, boostgraph ltgt to represent
graph. - The key feature that distinguished Boost CVPR
from other related libraries like OpenCV is that
it is a pure C library and takes full
advantages of C templates and Boost generic
programming.
41multi_arrayltfloat,2gt gauKernel(extents55)
float sigma 1.5 gaussian(gauKernel,sigma)
coutltlt"gauKernel "ltltendlltltgauKernelltlt""ltltendl
multi_arrayltfloat, 3gt in multi_arrayltfloat,
3gt out load_image(in, "in.bmp")
out.resize(extentsin.shape()0
in.shape()1in.shape()2)
for(index cc 0 cc lt in.shape()2 cc)
view2D filt_in in indicesr()r()cc
view2D filt_out out indicesr()r()cc
filt(filt_in,gauKernel,filt_out)
save_image(out,"out.bmp")
Stream operator overloading
Load image
View a color channel
Filter a color channel
Save image
42Color Space Transformation
- Function templates to transform RGB to LUV color
space and in the reverse direction.
rgb2luv(rgbImageArray, luvImageArray) luv2rgb(luv
ImageArray, rgbImageArray)
43Color Transformation
- This head file provides several components for
color amendments.
nbin is the number of the color scales, and maxv
is the maximal value after process.
- Five useful functions
- saturate (in, lb, ub)
- histogram (in, hist, bin)
- histeq (in, out, nbin, maxv)
- level (in, out, bin, curv)
- autolevel (in, out, lb, ub)
p lt lb, let p lb p gt ub, let p ub.
curv bin are 1D multiarray of the same size,
guiding the leveling process.
hist bin are 1D multiarray of the same size.
histi store the statics data of the pixals
being the color of bini.
out can be omitted.
44Array Visitor
- Wrapper of multi-folded loops for multiarray
- multi_for_each(_at)
- multi_copy(_at)
- multi_arith(_at)
eg.multi_arrayltfloat, 3gt a(extents1001003)
multi_arrayltfloat, 3gt b(extents1001003
) multi_arrayltfloat, 3gt c(extents100100
3) multi_for_each(a, _12) //a 2
multi_copy(a, b, _22_1) //b 2a
multi_arith(a, b, c, _32_1_2)//c 2ba
boostmulti_arrayboostlambdaMatlabC
45IO Stream
- By overloading the operator ltlt, the objects of
multi_array, array_view, and sub_array types can
be output at one time.
- Using the sentences just like (both file and std.
console) - coutltlt A cingtgt A
- foutltlt A fingtgt A
46Advices on Coding
47Code Separation
- There are 5 parts of algorithms and data
structures in our program, - (1) interface libraries such as MFC, dotNet, Qt,
VCL, Xtreme, wxWindows - (2) interface code of our own code
- (3) core algorithms and data structure oriented
to our specified task - (4) special CV and math algorithm libraries such
as OpenCV, VisSDK, ImageMagick, gsl - (5) the standard library
- We should make our core part as independent as
possible. So there are 3 categories of calls or
reference s between these parts, allowed,
forbidden, and limitedly permitted.
48Call Diagram
49Code Separation
- The only directed call from the core is call the
standard library. - We often need to use some other CV algorithms,
these algorithms should wrapper under our data
structure before using, and we call the wrapper
layer instead of the original library. - Our core algorithms and data structures must be
designed without the assumption that the
interface be MFC, image loader be FreeImage,
Canny algorithm be from OpenCV.
50Forbidden Calls and References
- To make the code more separable, the following
calls and references are forbidden - (1) the core calls the interface or interface
library algorithms directly, such as updating the
progress bar directly during the iterations in
the core - (2) core refers the interface or interface
library data structure, such as accessing
MainFrame from the core directly, or using CRect
and CString in the core data structure
definition, or letting a core class inherited
from MFC class.
51Forbidden Calls and References
52Callback Example
In MainFrame.h (VC/MFC)
- An example on iteration and progress
Or in MainFormUnit.h (CBuilder/VCL)
53More Separation
- To make our work more reusable, another
separation in the core is encouraged. We often
implement generic algorithms in our projects,
such as connected components, histogram
equalization these codes should be independent
from other task specified algorithms.
54End
- Thanks
- Welcome to visit
- Video/Motion Group Reference Documents section _at_
our SharePoint - Some Materials are from Yiyi Weis seminar report
55Research Platform
- Separation of Annotation Software and Research
Code
56Issues in ImageParser
- class static namespace
- Its better to define an object of class, just
like App and MainFrame in MFC, though they just
have one object, they do not use namespace - Keep header files in order, using forward
declaration - If too many similar codes, abstraction is needed
to make it neat and revisable
57Interfacing
- 4 level of interfacing
- File exchange text, image, xml
- Executable file call
- Dll call
- Source code in one Executable file