Advanced Image Processing Techniques for Maximum Efficiency
Learn about traditional vs. generic image processing, benefits of standard template library, and implementing versatile algorithms with multidimensional locators. Discover the power of generic programming in C++ for high-performance, portable image processing applications.
Advanced Image Processing Techniques for Maximum Efficiency
E N D
Presentation Transcript
Traditional Image Processing • Data Structures • Images, Palettes, Histograms, Profiles, etc. • Formanydatatypes (8bit, 16 bit, float, etc.) • Algorithms • Dependent on Data Structures • Dependent on datatypes • Combinatorial Explosion -> unmanageable • Toimplementonenewalgorithm, it must bewrittenmanytimes – foreach type www.ngi-central.org
Generic Image Processing • GenericProgrammingcomestotherescue • Threekey design techniques • Iteratorsdecoupledatastructures and algorithms • Functorsallowtoexchangepartsofcomputation • Genericalgorithms in termsofabstractiterators and functors • Enablingtechnologyofcompilersisthetemplate<> mechanism www.ngi-central.org
Standard Template Library • STL is an implementationofgenericprogrammingconcepts • STL isavailablewith all compilerssinceitispartofthe C++ standard • STL isaround 10 yearsold and consideredmature • Educational material tolearngenericprogramming in generaland STL in particularisavailable www.ngi-central.org
Example: RGB to Gray • Using STL style programming structRGBValue { unsignedcharred, green, blue; }; structRGBToGray { unsignedcharoperator()(constRGBValue& rgb) const { return 0.3*rgb.red + 0.59*rgb.green + 0.11*rgb.blue; } }; vector<RGBValue> rgb; vector<unsignedchar> gray; ... transform(rgb.begin(), rgb.end(), gray.begin(), RGBToGray()); www.ngi-central.org
New:worksfor all types! Betterexample • Templated on thedatatype • <template class T> • struct RGBValue { • T red, green, blue; • }; • <template class T> • struct RGBToGray { • T operator()(const RGBValue<T>& rgb) const { • return 0.3*rgb.red + 0.59*rgb.green + 0.11*rgb.blue; • } • }; • vector<RGBValue<float> > rgb; • vector<float> gray; • ... • transform(rgb.begin(), rgb.end(), gray.begin(), RGBToGray<float>()); www.ngi-central.org
STL leavesthingstodesire • STL isdesignedtoworkwith 1D data • Images areinherentlytwo(multi)dimensional • Somealgorithms do not needthedimensionality • Use STL in thiscase • Somealgorithmsneedthedimensionality • Cannotuse STL, needsomething different www.ngi-central.org
Multidimensional Locators • A locatoris a multidimensional iterator • Logical extensionto an iterator Move theiterator ++it; --it; it+=100; it-=50; Move thelocator ++it.x; --it.y; it.x+=100; it.y-=50; www.ngi-central.org
Algorithms change as well • // STL implementation • template<class IT, class F> • IT transform(IT First, IT Last, IT Dest, F Func) • { • for (; First != Last; ++First, ++Dest) • *Dest = Func(*First); • return (Dest); • } • // 2D implementation • template<class IT, class F> • IT transform(IT First, IT Last, IT Dest, F Func) • { • for (; First.y != Last.y; ++First.y, ++Dest.y) • for (; First.x != Last.x; ++First.x, ++Dest.x) • *Dest = Func(*First); • return (Dest); • } www.ngi-central.org
What do wegain? • An algorithmliketransformreplacesmanyfunctions in traditional programming style • Itcan do this, becauseitistemplated on thefunction • We still needtoprogramthefunctionality, but decoupledfromnavigation • Itsavesustorewritetheloopsmanytimes • Bonus: thefunctionobjectscanbereused in completely different algorithmsas well • Itcan do this, becauseitistemplated on thedatatype (byiteratorindirection) • Itsavesustorewritethecompletethingforeachdatatype • The gainistremendous (time, functionality, flexibility) www.ngi-central.org
NGI - Goals • Image Processing • Image -> Image • Image Analysis • Image -> Numbers • Generic Library • C++ • Templates • Source code • Independence of type • High Performance • nopenaltyforgenericcode • Portability • clean sourcecodefor easy portability www.ngi-central.org
Directory Structure • Code (include/) • Third partycode (toolkits/) • Documentation (book/, reference/, presentations/) • Sample code (samples/) and sample images (images/) • Automated tests (tests/) www.ngi-central.org
NGI - Tests • Unit Tests • Features of a class/functionaretested in isolation • Regression Tests • Outcomeof a functionistestedforregressions • Image/Text comparisons (now == previous) • Benchmarks • Measured in clocks per pixel www.ngi-central.org
NGI - Documentation • User Documentation • NGI Book • Reference Documentation • BuiltwithDoxygenfromsourcecode (HTML) • Alwaysup-to-date www.ngi-central.org
Third Party Code • Mandatory • boost (www.boost.org) • Optional • FreeImage (freeimage.sourceforge.net) • Cairo (www.cairographics.org) www.ngi-central.org
NGI – Build System • CMakeisusedasbuildengine • modular • selectcompiler(VS2005, VS2008, VS2010, Intel) • runtests • builddocumentation • Currentlysettingup a continuousbuildmachinewithvariousvirtualbuildenvironments www.ngi-central.org
Sample Code • Manycodesamples • Consolesamples • MFC samples • .NET samples • Tests are also samples • UsingBoost.Test • Unit tests, benchmarks, regressiontests www.ngi-central.org
NGI – Guidelines • Header onlylibrary • Source code • Heavy useoftemplates • Concentration on coreissues • usingother Open Source librarieswhereapplicable • Boost, FreeImage, Cairo, Qt www.ngi-central.org
NGI – Concepts • Buffer • storesdata • handlesallocation and deallocation • canuseexternalbuffers • View • levelofindirection • light, just a pointer and a fewpitches • all accessesthroughviews • can handle foreigndatabuffers • viewmanipulations • sub-views (moving_view.exe) • rotatedviews (dimension_swap_view.exe) • reversescanningdirections (reverse_view.exe) • subsampling (subsampling_view.exe) www.ngi-central.org
NGI – FunctionalityOverview (1) • Basic functionsusedeverywhereelse • Interpolation • linear, spline • Geometry • point, line_segment, ray, line, triangle, quadrilateral, rectangle, polygon, circle, ellipse • widgetsfor all geometricelements • Other examples • Base64, ZIP/GZIP Compression/Decompression, Fixed point, Matrix solver, solvingquadratic and cubicequations, etc. www.ngi-central.org
NGI – FunctionalityOverview (2) • STL likefunctions • copy, count, equal, find, ... • Image Processing functions • Point, Filters, Morphology, ... • Image Analysis functions • Blob, Pattern match, OCR/OCV, ... • Display Engine • Images, Palettes, Curves, Widgets, Geometry, ... • Framegrabber/Camerainterface www.ngi-central.org
NGI – STL LikeFunctions • accumulate • copy • count • equal • fill • find • generate • inner_product • max_element • min_element • replace • transform www.ngi-central.org
NGI – Image Processing Functions (1) • Point functions (point_operations.exe, profile.exe) • Filter and morphologicalfunctions (neighborhood_operations.exe) • 1D, 2D, 3D • Separable • Fast box filters – independentofkernelsize • Manypredefinedkernels, usercaneasilyadd • Framing (framing.exe) • Erode, Dilate, Open, Close, Gradient • Gray and binarybehavior www.ngi-central.org
NGI – Image Processing Functions (2) • Binning (binning.exe) • binningmethodselectable/userwritable • Geometric Transformation (resample.exe) • affine / polar • choiceofresampling filter (nearestneighbor, box, triangle, quadratic, cubic, bspline, sinc, kaiser, lanczos) • Autofocus • based on variance, gradient www.ngi-central.org
NGI – Image Processing Functions (3) • Color Processing • Color models (color_transform.exe) • RGB, CMY(K), HLS, HSI, Lab, Lchab, Luv, Lchuv • Color twist (color_twist.exe) • Bayer demosaic • StatisticFunctions • min, max, average, variance, percentiles • CameraCalibration www.ngi-central.org
NGI – Image Analysis Functions (1) • Blob analysis • Thresholding via Otsu‘smethod • 2D, 3D Labelling • Fläche/Volumen • Chain Code (inner, outer), perimeter • Moments • binary, gray • ordinary, central, Hu, Flusser www.ngi-central.org
NGI – Image Analysis Functions (2) • Pattern matching and searching (search.exe) • grayorcolor • usingpyramidforacceleration • OCR/OCV • train a characterset • read/verifytext www.ngi-central.org
NGI - Acceleration • Writtenfor Multi-Core • UsesOpenMP • Most functionsscalenicely, canbeseenwiththebenchmarks • Working on SIMD acceleration • Vector Processing (128 bytechunks) • 16 Pixels at a time • First forpointfunctions • These twomethodsare orthogonal www.ngi-central.org
NGI – Display Engine (1) • Display Engine • Direct2D, GDI Plus, OpenGL, Cairo • Alpha channel support (blit.exe) • Images (file_access.exe) • Palettes (palette.exe, false_color.exe) • Curves (histogram.exe) www.ngi-central.org
NGI – Display Engine (2) • Widgets (scale.exe) • Hierarchicalwidgetcomposition(widget_box.exe) • Callbacksfor smart interaction(horizontal_cursor.exe) www.ngi-central.org
NGI – Framegrabber/Camera • Grabbingintoringbuffer • asynchronous on different thread • live/snapshot • pre-trigger/post-trigger • Gen<i>cam support planned www.ngi-central.org
More Information www.ngi-central.org ngi-central.blogspot.com peter@ngi-central.org www.ngi-central.org