Practical Genericity: Writing Image Processing Algorithms Both - - PowerPoint PPT Presentation

practical genericity writing image processing algorithms
SMART_READER_LITE
LIVE PREVIEW

Practical Genericity: Writing Image Processing Algorithms Both - - PowerPoint PPT Presentation

Practical Genericity: Writing Image Processing Algorithms Both Reusable and Efficient Roland Levillain 1 , Thierry G eraud 1 , Laurent Najman 2 , Edwin Carlinet 1,2 1 EPITA Research and Development Laboratory (LRDE) 2 Laboratoire dInformatique


slide-1
SLIDE 1

Practical Genericity: Writing Image Processing Algorithms Both Reusable and Efficient

Roland Levillain1, Thierry G´ eraud1, Laurent Najman2, Edwin Carlinet1,2

1EPITA Research and Development Laboratory (LRDE) 2Laboratoire d’Informatique Gaspard-Monge (LIGM)

first.lastname@lrde.epita.fr first.lastname@esiee.fr

November 2014, 3rd

slide-2
SLIDE 2

Why do we need genericity? On the (re)conciliation of Genericity and Efficiency

Objective

Be able to process easily and efficiently many kind of images. A generic watershed transform

On a regular grid On an edge-valued graph On a 3D surface mesh

2/ 16

slide-3
SLIDE 3

Why do we need genericity? On the (re)conciliation of Genericity and Efficiency

Objective

Be able to process easily and efficiently many kind of images. A generic watershed transform

On a regular grid On an edge-valued graph On a 3D surface mesh A single algorithm processes these “images” !

2/ 16

slide-4
SLIDE 4

Why do we need genericity? On the (re)conciliation of Genericity and Efficiency

What about image processing algorithms?

Case study. Dilation by a structuring element (SE).

3/ 16

slide-5
SLIDE 5

Why do we need genericity? On the (re)conciliation of Genericity and Efficiency

What about image processing algorithms?

Case study. Dilation by a structuring element (SE). 2D dilation of float images with a square SE

image dilation(image f, int r) image out(f.nrows(), f.ncols()); for i = 0 to f.nrows(); do for j = 0 to f.ncols(); do float sup = FLT_MIN; for k = -r to r; do for l = -r to r; do if sup < f[i+k, j+l] sup = f[i+k, j+l]

  • ut[i,j] = sup;

return out;

3/ 16

slide-6
SLIDE 6

Why do we need genericity? On the (re)conciliation of Genericity and Efficiency

What about image processing algorithms?

Case study. Dilation by a structuring element (SE). 2D dilation of float images with a square SE

image dilation(image f, int r) image out(f.nrows(), f.ncols()); for i = 0 to f.nrows(); do for j = 0 to f.ncols(); do float sup = FLT_MIN; for k = -r to r; do for l = -r to r; do if sup < f[i+k, j+l] sup = f[i+k, j+l]

  • ut[i,j] = sup;

return out;

It works but. . .

3/ 16

slide-7
SLIDE 7

Why do we need genericity? On the (re)conciliation of Genericity and Efficiency

What about image processing algorithms?

  • Problem. It works but. . .
  • what if the image is in color? (genericity in the value space)
  • what if the image is 3D? (genericity in the domain space)
  • what if the image is a graph? (structural genericity)
  • what if the structuring element is a ball?

SE Ball Square Images image2d image3d graph Values uint8 double rgb8 Possible uses of the di- lation with a square SE.

We want genericity to cover the space of possibilities!

4/ 16

slide-8
SLIDE 8

Why do we need genericity? On the (re)conciliation of Genericity and Efficiency

Paradigms C

  • d

e C

  • m

p l e x i t y E ffi c i e n c y S t r u c t u r a l G e n e r i c i t y 1 a l g . = 1 i m p l . Code duplication ✗ ✓ ✗ ✗ Code duplication. Copy & paste and adapt the code → redundancy and maintainability issues. . . 1D dilation for 8-bits unsigned

image dilation(image f, int r) image out(f.size()); for i = 0 to f.size(); do unsigned char sup = 0; for k = -r to r; do sup = max(f[i+k], sup);

  • ut[i] = sup;

return out;

2D dilation for float

image dilation(image f, int r) image out(f.nrows(), f.ncols()); for i = 0 to f.nrows(); do for j = 0 to f.ncols(); do float sup = FLT_MIN; for k = -r to r; do for l = -r to r; do if sup < f[i+k, j+l] sup = f[i+k, j+l]

  • ut[i,j] = sup;

return out;

slide-9
SLIDE 9

Why do we need genericity? On the (re)conciliation of Genericity and Efficiency

Paradigms C

  • d

e C

  • m

p l e x i t y E ffi c i e n c y S t r u c t u r a l G e n e r i c i t y 1 a l g . = 1 i m p l . Code duplication ✗ ✓ ✗ ✗ Code duplication. Copy & paste and adapt the code → redundancy and maintainability issues. . . 1D dilation for 8-bits unsigned

image dilation(image f, int r) image out(f.size()); for i = 0 to f.size(); do unsigned char sup = 0; for k = -r to r; do sup = max(f[i+k], sup);

  • ut[i] = sup;

return out;

2D dilation for float

image dilation(image f, int r) image out(f.nrows(), f.ncols()); for i = 0 to f.nrows(); do for j = 0 to f.ncols(); do float sup = FLT_MIN; for k = -r to r; do for l = -r to r; do if sup < f[i+k, j+l] sup = f[i+k, j+l]

  • ut[i,j] = sup;

return out;

Bad !

5/ 16

slide-10
SLIDE 10

Why do we need genericity? On the (re)conciliation of Genericity and Efficiency

Paradigms C

  • d

e C

  • m

p l e x i t y E ffi c i e n c y S t r u c t u r a l G e n e r i c i t y 1 a l g . = 1 i m p l . Code duplication ✗ ✓ ✗ ✗ Generalization ✓ ✗ ✗ ✓

  • Generalization. e.g. consider 3D image of double for every images (the

wider type). → efficiency issues and still not structurally generic

6/ 16

slide-11
SLIDE 11

Why do we need genericity? On the (re)conciliation of Genericity and Efficiency

Paradigms C

  • d

e C

  • m

p l e x i t y E ffi c i e n c y S t r u c t u r a l G e n e r i c i t y 1 a l g . = 1 i m p l . Code duplication ✗ ✓ ✗ ✗ Generalization ✓ ✗ ✗ ✓ Object-Oriented Programming ✓ ✗ ✓ ✓

  • Generalization. e.g. consider 3D image of double for every images (the

wider type). → efficiency issues and still not structurally generic Object-Oriented Programming. Generalization through type hierarchies. → efficiency issues (virtual methods)

6/ 16

slide-12
SLIDE 12

Why do we need genericity? On the (re)conciliation of Genericity and Efficiency

Paradigms C

  • d

e C

  • m

p l e x i t y E ffi c i e n c y S t r u c t u r a l G e n e r i c i t y 1 a l g . = 1 i m p l . Code duplication ✗ ✓ ✗ ✗ Generalization ✓ ✗ ✗ ✓ Object-Oriented Programming ✓ ✗ ✓ ✓ Generic Programming ✓ ✓ ✓ ✓ Generic programming is the way to go. . .

7/ 16

slide-13
SLIDE 13

Why do we need genericity? On the (re)conciliation of Genericity and Efficiency

Paradigms C

  • d

e C

  • m

p l e x i t y E ffi c i e n c y S t r u c t u r a l G e n e r i c i t y 1 a l g . = 1 i m p l . Code duplication ✗ ✓ ✗ ✗ Generalization ✓ ✗ ✗ ✓ Object-Oriented Programming ✓ ✗ ✓ ✓ Generic Programming ✓ ✓ ✓ ✓ Generic programming is the way to go. . . Because the algorithm is intrasically generic and so should be the code V is the image value type dilation(Image f, SE win) initialize out from f foreach Site p in f’s domain do

  • ut(p) ← inf (V )

foreach Site n in win(p) do

  • ut(p) ← sup(out(p), f (n))

return out

7/ 16

slide-14
SLIDE 14

Why do we need genericity? On the (re)conciliation of Genericity and Efficiency

Paradigms C

  • d

e C

  • m

p l e x i t y E ffi c i e n c y S t r u c t u r a l G e n e r i c i t y 1 a l g . = 1 i m p l . Code duplication ✗ ✓ ✗ ✗ Generalization ✓ ✗ ✗ ✓ Object-Oriented Programming ✓ ✗ ✓ ✓ Generic Programming ✓ ✓ ✓ ✓ Generic programming is the way to go. . . Because the algorithm is intrasically generic and so should be the code V is the image value type dilation(Image f, SE win) initialize out from f foreach Site p in f’s domain do

  • ut(p) ← inf (V )

foreach Site n in win(p) do

  • ut(p) ← sup(out(p), f (n))

return out Real implementation should look like this! (see full code)

7/ 16

slide-15
SLIDE 15

Why do we need genericity? On the (re)conciliation of Genericity and Efficiency

Specific algorithms.

Input Type 1 image2d int Input Type 2 image2d float Input Type 3 image3d int

  • ut
  • ut
  • ut

1 Input Type = 1 Implementation

slide-16
SLIDE 16

Why do we need genericity? On the (re)conciliation of Genericity and Efficiency

Specific algorithms.

Input Type 1 image2d int Input Type 2 image2d float Input Type 3 image3d int

  • ut
  • ut
  • ut

1 Input Type = 1 Implementation

Generic algorithm.

Input Type 1 image2d int Input Type 2 image2d float Input Type 3 image3d int

  • ut

1 Algorithm = 1 Implementation = Many Input Types

8/ 16

slide-17
SLIDE 17

Why do we need genericity? On the (re)conciliation of Genericity and Efficiency

Outline

Why do we need genericity? On the (re)conciliation of Genericity and Efficiency

9/ 16

slide-18
SLIDE 18

Why do we need genericity? On the (re)conciliation of Genericity and Efficiency

Genericity vs Efficiency Trade-Off

Before.

Graph Image 2D Image 3D Generic

  • ut
slide-19
SLIDE 19

Why do we need genericity? On the (re)conciliation of Genericity and Efficiency

Genericity vs Efficiency Trade-Off

Before.

Graph Image 2D Image 3D Generic

  • ut

OK but no so efficient

slower than impl. with pointers

slide-20
SLIDE 20

Why do we need genericity? On the (re)conciliation of Genericity and Efficiency

Genericity vs Efficiency Trade-Off

Before.

Graph Image 2D Image 3D Generic

  • ut

OK but no so efficient

slower than impl. with pointers

After.

Dilation (dispatcher) Generic Partially Specialized for regular images

  • ut

10/ 16

slide-21
SLIDE 21

Why do we need genericity? On the (re)conciliation of Genericity and Efficiency

Genericity vs Efficiency Trade-Off

Before.

Graph Image 2D Image 3D Generic

  • ut

OK but no so efficient

slower than impl. with pointers

After.

Graph Dilation (dispatcher) Generic Partially Specialized for regular images

  • ut

10/ 16

slide-22
SLIDE 22

Why do we need genericity? On the (re)conciliation of Genericity and Efficiency

Genericity vs Efficiency Trade-Off

Before.

Graph Image 2D Image 3D Generic

  • ut

OK but no so efficient

slower than impl. with pointers

After.

Image 2D Dilation (dispatcher) Generic Partially Specialized for regular images

  • ut

10/ 16

slide-23
SLIDE 23

Why do we need genericity? On the (re)conciliation of Genericity and Efficiency

Genericity vs Efficiency Trade-Off

Before.

Graph Image 2D Image 3D Generic

  • ut

OK but no so efficient

slower than impl. with pointers

After.

Image 3D Dilation (dispatcher) Generic Partially Specialized for regular images

  • ut

10/ 16

slide-24
SLIDE 24

Why do we need genericity? On the (re)conciliation of Genericity and Efficiency

Hidden layer Interface layer Dilation (Facade) Generic Partially Specialized

using pointers

Partially Specialized

using pointers and separability

Graph Image 2D + Ball Image 2D + Square

  • therwise

f is regular only? f is regular and se is separable?

11/ 16

slide-25
SLIDE 25

Why do we need genericity? On the (re)conciliation of Genericity and Efficiency

Hidden layer Interface layer Dilation (Facade) Generic Partially Specialized

using pointers

Partially Specialized

using pointers and separability

f = any image-like type se = any SE-like type f = {image2d<uint8>, image3d<rgb8>...} se = {ball, cross...} f = {image1d<float>, image2d<uint8>, image3d<rgb8>...} se = {square, losange, hexagon...}

  • therwise

f is regular only? f is regular and se is separable?

Remark 1. Partially Specialized = Partially Generic = More efficient = Specific → Yet 1 implementation = Many input types

11/ 16

slide-26
SLIDE 26

Why do we need genericity? On the (re)conciliation of Genericity and Efficiency

Hidden layer Interface layer Dilation (Facade) Generic Partially Specialized

using pointers

Partially Specialized

using pointers and separability

f = any image-like type se = any SE-like type f = {image2d<uint8>, image3d<rgb8>...} se = {ball, cross...} f = {image1d<float>, image2d<uint8>, image3d<rgb8>...} se = {square, losange, hexagon...}

  • therwise

f is regular only? f is regular and se is separable?

Remark 2. The interface does not change. → 1 specialization = any dilation-based code gets optimized for free !

11/ 16

slide-27
SLIDE 27

Why do we need genericity? On the (re)conciliation of Genericity and Efficiency

Conclusion

Why Generic Programming?

  • No code duplication
  • One implementation to handle any kind of images
  • Somewhat efficient

12/ 16

slide-28
SLIDE 28

Why do we need genericity? On the (re)conciliation of Genericity and Efficiency

Conclusion

Why Generic Programming?

  • No code duplication
  • One implementation to handle any kind of images
  • Somewhat efficient

Reconciliation of genericity and efficiency.

  • Complexity hidden and transparent from the user POV
  • Partial specialization: loosing some genericity for efficiency. . .
  • . . . but we are still generic!

12/ 16

slide-29
SLIDE 29

Why do we need genericity? On the (re)conciliation of Genericity and Efficiency

Conclusion

Why Generic Programming?

  • No code duplication
  • One implementation to handle any kind of images
  • Somewhat efficient

Reconciliation of genericity and efficiency.

  • Complexity hidden and transparent from the user POV
  • Partial specialization: loosing some genericity for efficiency. . .
  • . . . but we are still generic!

→ generic implementations can run as fast as hand-written specific implementations

12/ 16

slide-30
SLIDE 30

Why do we need genericity? On the (re)conciliation of Genericity and Efficiency

Conclusion

Why Generic Programming?

  • No code duplication
  • One implementation to handle any kind of images
  • Somewhat efficient

Reconciliation of genericity and efficiency.

  • Complexity hidden and transparent from the user POV
  • Partial specialization: loosing some genericity for efficiency. . .
  • . . . but we are still generic!

→ generic implementations can run as fast as hand-written specific implementations Implemented in the Milena IP library of the Olena project http://olena.lrde.epita.fr

12/ 16

slide-31
SLIDE 31

Thank you for your attention

slide-32
SLIDE 32

Why do we need genericity? On the (re)conciliation of Genericity and Efficiency

Bibliography

Milena: Write generic morphological algorithms once, run on many kinds of images. Levillain, R., Levillain, R., G´ eraud, T., Najman, L. In: Proceedings of the ISMM. Lecture Notes in Computer Science,

  • vol. 5720, pp. 295–306. Springer Berlin / Heidelberg, Groningen,

The Netherlands (August 2009) Writing reusable digital topology algorithms in a generic image processing framework. Levillain, R., G´ eraud, T., Najman, L. In:

  • Proc. of WADGMM. Lecture Notes in Computer Science, vol. 7346,
  • pp. 140–153. Springer-Verlag (2012)

14/ 16

slide-33
SLIDE 33

Why do we need genericity? On the (re)conciliation of Genericity and Efficiency

Full C++ dilation code with Milena

template <class I, class W> I dilation(I input, W win) I output; initialize(output, input); mln_piter(I) p(input.domain()); mln_qiter(W) q(win, p); for_all(p) accu::supremum<mln_value(I)> sup; for_all(q) if (input.has(q)) sup.take(input(q));

  • utput(p) = sup.to_result();

return output;

15/ 16

slide-34
SLIDE 34

Why do we need genericity? On the (re)conciliation of Genericity and Efficiency

Full C++ pointer-based dilation with Milena

template <class I, class W> I dilation(I input, W win) { I output; initialize(output, input); mln_pixter(I) pi(input); mln_pixter(I) po(output); mln_qixter(I, W) q(pi, win); for_all_2(pi, po) { accu::supremum<mln_value(I)> sup; for_all(q) sup.take(q.val()); po.val() = sup.to_result(); } return output;

16/ 16