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 d’Informatique Gaspard-Monge (LIGM) first . lastname @lrde.epita.fr first . lastname @esiee.fr November 2014, 3 rd
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
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
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
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] out[i,j] = sup; return out; 3/ 16
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; It works but. . . 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] out[i,j] = sup; return out; 3/ 16
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 Possible uses of the di- lation with a square SE. Square We want genericity to cover Ball image2d image3d graph the space of possibilities! Images uint8 double rgb8 Values 4/ 16
y Why do we need genericity? On the (re)conciliation of Genericity and Efficiency t i c y i r . t e l i p x n e e m l G p i m 1 l y a o c r = C n u t e c . e i g c u d ffi l r a o t C E S 1 Paradigms Code duplication ✗ ✓ ✗ ✗ Code duplication. Copy & paste and adapt the code → redundancy and maintainability issues. . . 1D dilation for 8-bits unsigned 2D dilation for float image image dilation(image f, int r) dilation(image f, int r) image out(f.size()); image out(f.nrows(), f.ncols()); for i = 0 to f.size(); do for i = 0 to f.nrows(); do unsigned char sup = 0; for j = 0 to f.ncols(); do for k = -r to r; do float sup = FLT_MIN; sup = max(f[i+k], sup); for k = -r to r; do out[i] = sup; for l = -r to r; do return out; if sup < f[i+k, j+l] sup = f[i+k, j+l] out[i,j] = sup; return out;
y Why do we need genericity? On the (re)conciliation of Genericity and Efficiency t i c y i r . t e l i p x n e e m l G p i m 1 l y a o c r = C n u t e c . e i g c u d ffi l r a o t C E S 1 Paradigms Code duplication ✗ ✓ ✗ ✗ Code duplication. Copy & paste and adapt the code → redundancy and maintainability issues. . . 1D dilation for 8-bits unsigned 2D dilation for float image image dilation(image f, int r) dilation(image f, int r) image out(f.size()); image out(f.nrows(), f.ncols()); for i = 0 to f.size(); do for i = 0 to f.nrows(); do unsigned char sup = 0; for j = 0 to f.ncols(); do for k = -r to r; do Bad ! float sup = FLT_MIN; sup = max(f[i+k], sup); for k = -r to r; do out[i] = sup; for l = -r to r; do return out; if sup < f[i+k, j+l] sup = f[i+k, j+l] out[i,j] = sup; return out; 5/ 16
y Why do we need genericity? On the (re)conciliation of Genericity and Efficiency t i c y i r . t e l i p x n e e m l G p i m 1 l y a o c r = C n u t e c . e i g c u d ffi l r a o t C E S 1 Paradigms 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
y Why do we need genericity? On the (re)conciliation of Genericity and Efficiency t i c y i r . t e l i p x n e e m l G p i m 1 l y a o c r = C n u t e c . e i g c u d ffi l r a o t C E S 1 Paradigms 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
y Why do we need genericity? On the (re)conciliation of Genericity and Efficiency t i c y i r . t e l i p x n e e m l G p i m 1 l y a o c r = C n u t e c . e i g c u d ffi l r a o t C E S 1 Paradigms Code duplication ✗ ✓ ✗ ✗ Generalization ✓ ✗ ✗ ✓ Object-Oriented Programming ✓ ✗ ✓ ✓ Generic Programming ✓ ✓ ✓ ✓ Generic programming is the way to go. . . 7/ 16
y Why do we need genericity? On the (re)conciliation of Genericity and Efficiency t i c y i r . t e l i p x n e e m l G p i m 1 l y a o c r = C n u t e c . e i g c u d ffi l r a o t C E S 1 Paradigms 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 out ( p ) ← inf ( V ) foreach Site n in win ( p ) do out ( p ) ← sup ( out ( p ) , f ( n )) return out 7/ 16
y Why do we need genericity? On the (re)conciliation of Genericity and Efficiency t i c y i r . t e l i p x n e e m l G p i m 1 l y a o c r = C n u t e c . e i g c u d ffi l r a o t C E S 1 Paradigms 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 Real implementation should foreach Site p in f’s domain do look like this! (see full code) out ( p ) ← inf ( V ) foreach Site n in win ( p ) do out ( p ) ← sup ( out ( p ) , f ( n )) return out 7/ 16
Why do we need genericity? On the (re)conciliation of Genericity and Efficiency Specific algorithms. Input Type 1 out image2d int Input Type 2 out 1 Input Type image2d float = 1 Implementation Input Type 3 out image3d int
Why do we need genericity? On the (re)conciliation of Genericity and Efficiency Specific algorithms. Input Type 1 out image2d int Input Type 2 out 1 Input Type image2d float = 1 Implementation Input Type 3 out image3d int Generic algorithm. Input Type 1 image2d int 1 Algorithm Input Type 2 out = 1 Implementation image2d float = Many Input Types Input Type 3 image3d int 8/ 16
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
Why do we need genericity? On the (re)conciliation of Genericity and Efficiency Genericity vs Efficiency Trade-Off Before. Graph Image 2D out Generic Image 3D
Why do we need genericity? On the (re)conciliation of Genericity and Efficiency Genericity vs Efficiency Trade-Off Before. Graph OK but Image 2D out no so efficient Generic Image 3D slower than impl. with pointers
Why do we need genericity? On the (re)conciliation of Genericity and Efficiency Genericity vs Efficiency Trade-Off Before. Graph OK but Image 2D out no so efficient Generic Image 3D slower than impl. with pointers After. Generic out Dilation (dispatcher) Partially Specialized for regular images 10/ 16
Why do we need genericity? On the (re)conciliation of Genericity and Efficiency Genericity vs Efficiency Trade-Off Before. Graph OK but Image 2D out no so efficient Generic Image 3D slower than impl. with pointers After. Graph Generic out Dilation (dispatcher) Partially Specialized for regular images 10/ 16
Recommend
More recommend