+ All Categories
Home > Documents > Doc Imgp Roc Lib

Doc Imgp Roc Lib

Date post: 26-Oct-2014
Category:
Upload: sung-woong-ki
View: 68 times
Download: 0 times
Share this document with a friend
Popular Tags:
86
Image-processing Library in C++ Chen Lujie July 2, 2012 Contents 1 Introduction 5 1.1 Features .............................................. 5 1.2 Source files ............................................ 5 1.3 Explicit template instantiation ................................. 6 1.4 Coding style ............................................ 7 2 2D geometry classes and functions 8 2.1 CPt2D: 2D point class ...................................... 8 2.2 CSize2D: 2D size class ...................................... 9 2.3 CLine2D: 2D line segment class ................................. 9 2.4 CRect: rectangle class ...................................... 11 2.5 CRectRot: rotating rectangle class ............................... 12 2.6 CFan: fan shape class ...................................... 14 2.7 CCircle: circle class ........................................ 15 2.8 CEllipse: ellipse class ....................................... 16 2.9 CPoly2D: 2D polygon class ................................... 17 2.10 CLCSpl: a linear spline class .................................. 18 2.11 CNCSpl: a natural cubic spline class .............................. 19 2.12 Distance .............................................. 20 2.13 Area2 ............................................... 21 2.14 Angle ............................................... 21 2.15 MidPoint ............................................. 22 2.16 Centroid .............................................. 22 2.17 Intersect .............................................. 22 2.18 CutLineInRect .......................................... 23 2.19 BoundingRect ........................................... 23 2.20 PointInPolygon .......................................... 24 2.21 ConvexHull ............................................ 24 2.22 BoundingBox ........................................... 25 2.23 MinBoundingBox ......................................... 25 2.24 MinBoundingCircle ........................................ 25 2.25 DelaunayTriangulation ...................................... 26 2.26 FitLine .............................................. 26 2.27 FitCircle .............................................. 26 2.28 FitEllipse ............................................. 27 2.29 FitPolynomialCurve ....................................... 27 2.30 FitGauss .............................................. 27 2.31 HoughLine ............................................ 28 2.32 HoughCircle ............................................ 28 2.33 GetLinePoints ........................................... 29 2.34 GetCirclePoints .......................................... 30 1
Transcript
Page 1: Doc Imgp Roc Lib

Image-processing Library in C++

Chen Lujie

July 2, 2012

Contents

1 Introduction 5

1.1 Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51.2 Source files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51.3 Explicit template instantiation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61.4 Coding style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

2 2D geometry classes and functions 8

2.1 CPt2D: 2D point class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82.2 CSize2D: 2D size class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92.3 CLine2D: 2D line segment class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92.4 CRect: rectangle class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112.5 CRectRot: rotating rectangle class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122.6 CFan: fan shape class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142.7 CCircle: circle class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152.8 CEllipse: ellipse class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162.9 CPoly2D: 2D polygon class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172.10 CLCSpl: a linear spline class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182.11 CNCSpl: a natural cubic spline class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192.12 Distance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202.13 Area2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212.14 Angle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212.15 MidPoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222.16 Centroid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222.17 Intersect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222.18 CutLineInRect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232.19 BoundingRect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232.20 PointInPolygon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242.21 ConvexHull . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242.22 BoundingBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252.23 MinBoundingBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252.24 MinBoundingCircle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252.25 DelaunayTriangulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262.26 FitLine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262.27 FitCircle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262.28 FitEllipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272.29 FitPolynomialCurve . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272.30 FitGauss . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272.31 HoughLine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282.32 HoughCircle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282.33 GetLinePoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292.34 GetCirclePoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

1

Page 2: Doc Imgp Roc Lib

3 2D image processing functions 31

3.1 ImgAssign . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313.2 ImgAssignBorder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313.3 ImgCopy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323.4 ImgCopySubpixel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323.5 ImgBlend . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323.6 ImgGradientX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323.7 ImgGradientY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333.8 ImgClamp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333.9 ImgLinear . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333.10 ImgGamma . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343.11 ImgMin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343.12 ImgMax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353.13 ImgMinMax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353.14 ImgMean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353.15 ImgMedian . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353.16 ImgVariance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353.17 ImgStdDev . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353.18 ImgStatistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353.19 ImgFltMean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363.20 ImgFltMedian . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363.21 ImgFltVariance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373.22 ImgFltISEF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373.23 ImgConvX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373.24 ImgConvY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383.25 ImgConv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383.26 ImgResize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393.27 ImgRotate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393.28 ImgFlip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403.29 ImgFan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403.30 ImgRadial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413.31 ImgMosaic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423.32 ImgMatch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423.33 ImgMatchSubpixel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433.34 ImgMotion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433.35 ImgDIC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443.36 ImgHistogram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453.37 ImgHistoEqu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463.38 ImgClampExtreme . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463.39 ImgThre TwoPeak . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463.40 ImgThre IterSel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473.41 ImgDilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473.42 ImgErosion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473.43 ImgProjX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483.44 ImgProjY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483.45 ImgProjAny . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483.46 ImgRadon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493.47 ImgRadonVH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493.48 ImgBackprojVH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503.49 ImgIntegral . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503.50 ImgLabeling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513.51 ImgFilling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513.52 ImgThinning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523.53 ImgCentroid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523.54 ImgBoundaryOrdered . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533.55 ImgBoundaryUnordered . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533.56 ImgOuterCorners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533.57 ImgConvexHull . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543.58 ImgBoundingBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54

2

Page 3: Doc Imgp Roc Lib

3.59 ImgMinBoundingBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543.60 ImgMinBoundingCircle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553.61 ImgMinBoundingPolygon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553.62 ImgDistTrans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553.63 ImgWatershed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553.64 ImgEdgeX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563.65 ImgEdgeY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573.66 ImgEdgeXSubpixel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573.67 ImgEdgeYSubpixel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573.68 ImgEdgeAny . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573.69 ImgEdgeOnLine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583.70 ImgCannyEdge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593.71 ImgShenCastanEdge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593.72 ImgFitPlane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593.73 ImgFitPolynomialSurf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603.74 ImgFitGauss . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603.75 ImgFitImg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613.76 ImgLineData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613.77 ImgLineSum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613.78 ImgContour . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613.79 ImgMask . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623.80 ImgMaskPolygon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623.81 ImgMandelbrot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633.82 ImgPhaseShifting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633.83 ImgPhaseShifting Carre . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643.84 ImgPhaseShifting 5Frame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643.85 ImgPhaseUnw LToR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653.86 ImgPhaseUnw QualGui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653.87 ImgPhaseGradVar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653.88 ImgFaceDetection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663.89 ImgSquareDetection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663.90 ImgBlackSquareGrid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663.91 ImgReadASCII . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673.92 ImgSaveASCII . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683.93 ImgReadRaw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683.94 ImgSaveRaw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68

4 3D geometry classes and functions 69

4.1 CPt3D: 3D point class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 694.2 CSize3D: 3D size class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 694.3 CLine3D: 3D line segment class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 704.4 CCube: cube class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714.5 CTri3D: 3D triangle class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724.6 CPoly3D: 3D polygon class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 734.7 CCylinder: cylinder class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 734.8 Distance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744.9 Area . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744.10 Angle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744.11 MidPoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754.12 Centroid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754.13 Collinear . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754.14 NormalVector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754.15 Volume . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754.16 ConvexHull3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754.17 FitPlane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764.18 FitPolynomialSurf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764.19 FitGauss . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77

3

Page 4: Doc Imgp Roc Lib

5 3D image processing functions 78

5.1 ImgAssign3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 785.2 ImgCopy3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 785.3 ImgCopyXY 2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 785.4 ImgCopyXZ 2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 785.5 ImgCopyYZ 2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795.6 ImgCopy2D XY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795.7 ImgCopy2D XZ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795.8 ImgCopy2D YZ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795.9 ImgLinear3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795.10 ImgMin3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 805.11 ImgMax3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 805.12 ImgMinMax3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 805.13 ImgMean3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 805.14 ImgFltMean3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 805.15 ImgFltVariance3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 805.16 ImgConv3DZ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 815.17 ImgResize3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 815.18 ImgRotate3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 815.19 ImgFlip3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825.20 ImgMatch3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825.21 ImgMotion3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825.22 ImgLabeling3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 835.23 ImgFilling3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845.24 ImgDistTrans3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845.25 ImgWatershed3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845.26 ImgIsoSurface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855.27 ImgReadRaw3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855.28 ImgSaveRaw3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86

4

Page 5: Doc Imgp Roc Lib

1 Introduction

C++, standard template library (STL), computer graphics, and image processing: each of the areas isa big topic by itself, while they are indeed interrelated. This note aims to explore the interrelationshipand document an image processing library in C++.

The note focuses on the input and output parameters of the library functions with some concisebackground information. Readers are encouraged to exam the source code where brief comments canbe found explaining the implementation; however, code reading is not required for using the library. Acomplete package of the library files (*.h and *.cpp) may be requested from [email protected].

1.1 Features

There are a variety of image processing libraries available from the internet. The highlight of this librarylies in the following features.

Simplicity. The library does not rely on any particular image class so that you can keep your datastructure unchanged. (Many image processing libraries define their specific image class and data structureconversion is required, which may induce considerable processing overhead.)

Flexibility. Based on C++ template mechanism, the library accepts all C++ build-in types as theimage data, although certain functions are only valid to a subset of build-in types. If you have integerand float image data to process, for example, all you need to do is to generate two explicit templateinstantiations of a library function. It is a standard C++ technique and will be discussed in detail inSection 1.3.

Independency. Most of the image processing functions in the library are relatively independent.Combination of some functions may achieve sophisticated processing tasks.

Portability. The library is written in ANSI C++. It can be compiled by many compilers, such asVisual Studio C++ and g++.

Readability. Input and output of all functions are systemically organized. Following a few examples,you will be familiar with what to supply to a function and what can be expected from the output.Documentation is also attached in the source code, above each function’s definition.

1.2 Source files

The source files of the library are listed in Table 1. To use the library, all .cpp files (ImgProc2D.cpp,ImgProc3D.cpp, Shape2D.cpp, Shape3D.cpp, Util.cpp, lubksb.cpp, ludcmp.cpp, pythag.cpp and svd-cmp.cpp) should be compiled. Two header files ImgProc2D.h, and ImgProc3D.h should be includedin whichever file that uses the library. The rest of the header files are included automatically in Img-Proc2D.h, and ImgProc3D.h. It is important to know that the library classes and functions are definedin a CLJ namespace; and therefore after including ImgProc2D.h and / or ImgProc3D.h, you should write“using namespace CLJ;”. The following code shows an example.

#include "PreCompiled.h"

#if !USE_PRECOMPILED_HEADER

#include <iostream>

#include <vector>

using namespace std;

#endif

#include "ImgProc2D.h"

using namespace CLJ;

int main()

{

int w = 256, h = 512;

vector<float> vI2D1(w*h), vI2D2(w*h);

ImgAssign(&vI2D1[0], w, h, CRect<int>(0,0,w,h), 50.f);

ImgAssign(&vI2D2[0], w, h, CRect<int>(0,0,w,h), 100.f);

cout<<"ImgAssign done.\n";

ImgCopy(&vI2D1[0], w, h, CRect<int>(20,20,w-10,h-10),

&vI2D2[0], w, h, CRect<int>(10,10,w-20,h-20));

cout<<"ImgCopy done.\n";

5

Page 6: Doc Imgp Roc Lib

return 0;

}

Main files DescriptionImgPro2D.cpp and .h 2D image processing functionsImgPro3D.cpp and .h 3D image processing functionsShape2D.cpp and .h 2D geometry classes and functionsShape3D.cpp and .h 3D geometry classes and functionsSupport files DescriptionPreCompiled.h precompiled header fileUtil.cpp and .h utility functions source and header filenr.h header file of all ”Numerical Recipes” functionsnrtypes nr.h data types defined in ”Numerical Recipes”nrutil nr.h utilities of ”Numerical Recipes”lubksb.cpp a ”Numerical Recipes” functionludcmp.cpp a ”Numerical Recipes” functionpythag.cpp a ”Numerical Recipes” functionsvdcmp.cpp a ”Numerical Recipes” function

Table 1: Source files of the library.

There is a header file, PreCompiled.h, included at the first line of all .cpp files. It defines the AS-SERT(...) macro used by the library for error reporting. If DEBUG is defined, ASSERT(...) macro isthe same as the C++ library function assert(...); otherwise, it does nothing. In PreCompiled.h, someC++ library headers are conditionally included by

#define USE_PRECOMPILED_HEADER 1 (use precompilation) or#define USE_PRECOMPILED_HEADER 0 (not use precompilation).If you use the precompilation feature of your C++ compiler, then make PreCompiled.h as the pre-

compiled header of all your .cpp files. (Include PreCompiled.h at the first line of all your .cpp files.)Create a new file called PreCompiled.cpp, which contains just one line of code.

#include "PreCompiled.h"

Make PreCompiled.cpp as the precompiled source file of your project. This procedure is standard tomaking use of precompilation, which should be obvious to people who are familiar with compiler settings.However, if you are confused, you may simply

#define USE_PRECOMPILED_HEADER 0

1.3 Explicit template instantiation

An issue associated with using template functions and template classes is “explicit template instantia-tion”. Basically, if you want to use a template function or a template class in a particular data type (forexample: float), there should be an explicit template function or class instantiation of that type (float).The library’s “explicit template instantiation” is put at the end of each cpp file, where you can find somecode like:// Code in Shape2D.cpp

template class CPt2D<int>;

template class CPt2D<float>;

template class CRect<int>;

template class CRect<float>;

// Code in ImgProc2D.cpp

template void ImgAssign<unsigned char>

(unsigned char *pImg, int w, int h, const CRect<int> &rcROI,

unsigned char value);

template void ImgCopy<char, float>

(const char *pSrc, int w1, int h1, const CRect<int> &rcROI1,

float *pDst, int w2, int h2, const CRect<int> &rcROI2);

template void ImgCopy<unsigned char, float>

6

Page 7: Doc Imgp Roc Lib

(const unsigned char *pSrc, int w1, int h1, const CRect<int> &rcROI1,

float *pDst, int w2, int h2, const CRect<int> &rcROI2);

If you need a build-in data type of a specific class or function that has not been instantiated, you canadd an instantiation following the above coding style; otherwise you will get compiler errors.

1.4 Coding style

Clear and easy-to-follow coding styles and naming conventions are used in the library.1. A pointer variable is prefixed by ’p’.2. A one- or two-letter prefix, such as ’uc’, ’c’, ’n’, and ’d’, indicates the data type.3. General purpose integer variables are usually defined as ’x’, ’y’, ’z’, ’i’, ’j’ et al.

Example 1 (naming conventions)char *pcName = ”Mary”;unsigned char ucRed = 24;int nCount;float fRatio = 1.f;double dRadius = 30;

Example 2 (loop through an image)int x, y, w = 100, h = 50;float *pfImg = new float[w*h];for(y=0; y<h; ++y)

for(x=0; x<w; ++x)pfImg[y*w+x] = 3.f;

7

Page 8: Doc Imgp Roc Lib

2 2D geometry classes and functions

The coordinate system of the library (see Fig. 1) follows the most widely used convention in computergraphics. The origin (0,0) is located at the left-top corner of an image (or a window, or the computerscreen). The positive X axis extends horizontally to the right and the positive Y axis extends verticallydownward. An anticlockwise angle with respect to the X axis is defined as positive.

0 100 200 X0

100

200

Y

Figure 1: Coordinate system of the image processing library.

2.1 CPt2D: 2D point class

template <class T> class CPt2D

{

public:

T x, y;

CPt2D<T>() : x(0), y(0) { }

CPt2D<T>(const CPt2D<T> &pt) : x(pt.x), y(pt.y) { }

CPt2D<T>(T tx, T ty) : x(tx), y(ty) { }

CPt2D<T>& operator = (const CPt2D<T> &pt);

CPt2D<T>& operator += (const CPt2D<T> &pt);

CPt2D<T>& operator -= (const CPt2D<T> &pt);

CPt2D<T> operator + (const CPt2D<T> &pt) const;

CPt2D<T> operator - (const CPt2D<T> &pt) const;

bool operator == (const CPt2D<T> &pt) const;

bool operator != (const CPt2D<T> &pt) const;

void SetPoint(T tx, T ty) { x = tx; y = ty; }

void Rotate(const CPt2D<T> &ptRot, double dAngle);

};

Variable Descriptionx x coordinate of the pointy y coordinate of the point

Function Descriptionoperator = Assignment operatoroperator + = *this = *this + ptoperator − = *this = *this − ptoperator + Return *this + ptoperator − Return *this − ptoperator == Check if *this is equal to ptoperator != Check if *this is not equal to pt

Function DescriptionSetPoint Set the x and y coordinates of the pointRotate Rotate the point with respect to a point

8

Page 9: Doc Imgp Roc Lib

2.2 CSize2D: 2D size class

template <class T> class CSize2D

{

public:

T cx, cy;

CSize2D<T>() : cx(0), cy(0) { }

CSize2D<T>(const CSize2D<T> &size) : cx(size.cx), cy(size.cy) { }

CSize2D<T>(T tcx, T tcy) : cx(tcx), cy(tcy) { }

CSize2D<T>& operator = (const CSize2D<T> &size);

CSize2D<T>& operator += (const CSize2D<T> &size);

CSize2D<T>& operator -= (const CSize2D<T> &size);

CSize2D<T> operator + (const CSize2D<T> &size) const;

CSize2D<T> operator - (const CSize2D<T> &size) const;

bool operator == (const CSize2D<T> &size) const;

bool operator != (const CSize2D<T> &size) const;

void SetSize(T tcx, T tcy) { cx = tcx; cy = tcy; }

};

Variable Descriptioncx size in x directioncy size in y direction

Function Descriptionoperator = Assignment operatoroperator + = *this = *this + sizeoperator − = *this = *this − sizeoperator + Return *this + sizeoperator − Return *this − sizeoperator == Check if *this is equal to sizeoperator != Check if *this is not equal to size

2.3 CLine2D: 2D line segment class

template <class T> class CLine2D

{

public:

CLine2D<T>() : m_ptStart(0,0), m_ptEnd(0,0) { }

CLine2D<T>(const CLine2D<T> &line);

CLine2D<T>(const CPt2D<T> &ptStart, const CPt2D<T> &ptEnd);

CLine2D<T>(T x1, T y1, T x2, T y2);

CLine2D<T>& operator = (const CLine2D<T> &line);

bool operator == (const CLine2D<T> &line) const;

bool operator != (const CLine2D<T> &line) const;

CPt2D<T> Start() const;

CPt2D<T> End() const;

CPt2D<T> Center() const;

CRect<T> BoundingRect() const;

double Angle() const;

double Length() const;

void SetLine (T x1, T y1, T x2, T y2);

void SetLine (const CPt2D<T> &ptStart, const CPt2D<T> &ptEnd);

void SetStart (T x, T y);

void SetEnd (T x, T y);

void SetStart (const CPt2D<T> &pt);

void SetEnd (const CPt2D<T> &pt);

void SetCenter (const CPt2D<T> &pt);

9

Page 10: Doc Imgp Roc Lib

void SetAngle (const CPt2D<T> &ptRot, double dAngle);

void SetLength (double dLength, int nFix);

void Offset (T x, T y);

void Offset (const CPt2D<T> &ptOffset);

void Rotate (const CPt2D<T> &ptRot, double dAngle);

double DistToPt (double x, double y) const;

double DistToPt (const CPt2D<T> &pt) const;

bool GetY (T x, T *pY) const;

bool GetPointAt(double dDist, CPt2D<T> *ppt) const;

bool LineEqu (double *pdA, double *pdB, double *pdC) const;

private:

CPt2D<T> m_ptStart, m_ptEnd; // start and end point

};

Function Descriptionoperator = Assignment operatoroperator == Check if *this is equal to lineoperator != Check if *this is not equal to line

Function DescriptionStart Get the start point of the lineEnd Get the end point of the lineCenter Get the center of the lineBoundingRect Get the bounding box of the lineAngle Get the angle formed by the line and the positive X axisLength Get the length of the lineSetLine Set the start and the end points of the lineSetStart Set the start point of the lineSetEnd Set the end point of the lineSetCenter Set the center of the lineSetAngle Set the angle of the line

The line is rotated with respect to ptRot to the specified angle, formed bythe line and the positive X axis.

Offset Offset the position of the lineRotate Rotate the line with respect to a pointDistToPt Get the distance from a point to the line

If the line segment is a point (the start and end points are the same), returnthe distance from an input point to the line’s end point.If the line segment’s start and end points are different, return the distancefrom an input point to the line, which is derived from the line segment.

SetLength Set the length of the line.To modify the line length, one can fix the start point, the end point, or thecenter. nFix specifies which point to fix. The angle of the line is alwaysfixed. If the start point and the end point are the same, the line will beextended in the x direction only.

dLength the new length of the line, which should be >= 0.nFix 0: the start point of the line is fixed;

1: the center of the line is fixed;2: the end point of the line is fixed.

GetY Given a point’s x coordinate on the line, get its corresponding y coordinate.x input x coordinatepY output y coordinateReturn true: y coordinate found; false: cannot find y coordinate.

10

Page 11: Doc Imgp Roc Lib

GetPointAt Get a point on the line that has a specified distance from the star point.Take the start point as a reference, dDist > 0 indicates the point is at thesame side as the end point; while dDist < 0 indicates the point is at theopposite side of the end point.

dDist the distance of the point from the start point.ppt the point foundReturn true: point found; false: cannot find such a point.

LineEqu Based on the line segment, retrieve a line equation in the form: A*x + B*y= C. This line equation encompasses all possible situations. Another choiceis: cos(angle)*x + sin(angle)*y = R. However, it involves relatively slowtrigonometric computation, so it is not used.

pdA parameter A of the line equationpdB parameter B of the line equationpdC parameter C of the line equationReturn true: line function found; false: line function not exist.

0 100 200 X0

100

200

Y

start point

end point

angle

Figure 2: An example of a 2D line segment.

2.4 CRect: rectangle class

template <class T> class CRect

{

public:

T left, top, right, bottom;

CRect<T>() : left(0), top(0), right(0), bottom(0) { }

CRect<T>(const CRect<T> &rect);

CRect<T>(const CPt2D<T> &ptLeftTop, const CSize2D<T> &size);

CRect<T>(const CPt2D<T> &ptLeftTop, const CPt2D<T> &ptRightBottom);

CRect<T>(T l, T t, T r, T b);

CRect<T>& operator = (const CRect<T>& rect);

CRect<T>& operator &= (const CRect<T>& rect);

CRect<T>& operator |= (const CRect<T>& rect);

CRect<T> operator & (const CRect<T>& rect) const;

CRect<T> operator | (const CRect<T>& rect) const;

bool operator == (const CRect<T>& rect) const;

bool operator != (const CRect<T>& rect) const;

bool IsEmpty();

T Width();

T Height();

CSize2D<T> Size();

CPt2D<T> Center();

CPt2D<T> LeftTop();

CPt2D<T> RightTop();

CPt2D<T> LeftBottom();

CPt2D<T> RightBottom();

void SetRect(T l, T t, T r, T b);

11

Page 12: Doc Imgp Roc Lib

void Inflate(T l, T t, T r, T b);

void Deflate(T l, T t, T r, T b);

void Inflate(T x, T y);

void Deflate(T x, T y);

void Offset (T x, T y);

void Offset (const CPt2D<T> &pt);

bool PtIn (T x, T y) const;

bool PtIn (const CPt2D<T> &pt) const;

bool RectIn (const CRect<T> &rc) const;

void Normalize();

};

Variable Descriptionleft left bound of the rectangletop top bound of the rectangleright right bound of the rectanglebottom bottom bound of the rectangle

Function Descriptionoperator = Assignment operatoroperator &= *this = Intersect of two rectanglesoperator |= *this = Union of two rectanglesoperator & Return intersect of two rectanglesoperator | Return union of two rectanglesoperator == Check if *this is equal to rectoperator != Check if *this is not equal to rect

Function DescriptionIsEmpty Check if it is an empty rectangle. A rectangle is empty if its width or height

is <= 0.Width Get the width (right− left) of the rectangleHeight Get the height (bottom− top) of the rectangleSize Get the size of the rectangleCenter Return the center of the rectangleLeftTop Return the left-top point of the rectangleRightTop Return the right-top point of the rectangleLeftBottom Return the left-bottom point of the rectangleRightBottom Return the right-bottom point of the rectangle

Function DescriptionSetRect Set the left, top, right and bottom value of the rectangleInflate Inflate the rectangle by l, t, r, b on the left, top, right, and bottom sides

respectively.Inflate the rectangle by x on the left and right sides and by y on the topand bottom sides respectively.

Deflate Deflate the rectangle by l, t, r, b on the left, top, right, and bottom sidesrespectively.Deflate the rectangle by x on the left and right sides and by y on the topand bottom sides respectively.

Offset Offset the position of the rectanglePtIn Check if a point is inside the rectangle. A point is side a rectangle, if

pt.x ∈ [left, right) AND pt.y ∈ [top, bottom),RectIn Check if a rectangle is completely inside or on *this.Normalize Make sure the rectangle has positive width and height. If left > right,

swap left and right. If top > bottom, swap top and bottom.

2.5 CRectRot: rotating rectangle class

template <class T> class CRectRot

{

12

Page 13: Doc Imgp Roc Lib

0 100 200 X0

100

200

Y

left

top

right

bottom

Figure 3: An example of a rectangle.

public:

CRectRot<T>() : m_ptCenter(0,0), m_width(0), m_height(0), m_dAngle(0) { }

CRectRot<T>(const CRectRot &rectRot);

CRectRot<T>(T x, T y, T width, T height, double dAngle = 0);

CRectRot<T>(const CPt2D<T> &ptCenter, T width, T height, double dAngle = 0);

CRectRot<T>(const CPt2D<T> &ptCenter, CSize2D<T> size, double dAngle = 0);

CRectRot<T>(const CRect<T> &rect, double dAngle = 0);

CRectRot<T>& operator = (const CRectRot &rectRot);

T Width() const;

T Height() const;

CPt2D<T> Center() const;

double Angle() const;

void SetWidth (T width);

void SetHeight(T height);

void SetAngle (double dAngle);

void SetCenter(const CPt2D<T> &ptCenter);

void Corner(CPt2D<T> ppt[4]) const;

CRect<T> BoundingRect() const;

void Inflate(T w, T h);

void Deflate(T w, T h);

void Offset (T x, T y);

void Offset (const CPt2D<T> &ptOffset);

int PtIn (T x, T y); const;

int PtIn (const CPt2D<T> &pt) const;

void Rotate (const CPt2D<T> &ptRot, double dAngle);

private:

CPt2D<T> m_ptCenter; // center of the rectangle

T m_width; // width of the rectangle

T m_height; // height of the rectangle

double m_dAngle; // anticlockwise angle (radius) of the rectangle

};

Function Descriptionoperator = Assignment operatorWidth Get the width of the rectangleHeight Get the height of the rectangleCenter Get the center of the rectangleAngle Get the angle of the rectangle, anticlockwise in radiusSetWidth Set the width of the rectangleSetHeight Set the height of the rectangleSetCenter Set the center of the rectangleSetAngle Set the angle of the rectangle, anticlockwise in radiusCorner Get 4 corner points of the rectangle

continued on next page

13

Page 14: Doc Imgp Roc Lib

BoundingRect Get the bounding box of the rectangleInflate Increasing the width and height by w and h respectively.Deflate Decreasing the width and height by w and h respectively.Offset Offset the position of the rectanglePtIn Check if a point is inside the rectangleRotate Rotate the rectangle with respect to a point

0 100 200 X0

100

200

Y

width

height

angle

center

Figure 4: An example of a rotating rectangle.

2.6 CFan: fan shape class

template <class T> class CFan

{

public:

CFan<T>() : m_ptCenter(0,0), m_Radius(0),

m_dFanAngle(0), m_dStartAngle(0) { }

CFan<T>(const CFan &fan);

CFan<T>(const CPt2D<T> &ptCenter, T radius,

double dFanAngle, double dStartAngle);

CFan<T>(T x, T y, T radius, double dFanAngle, double dStartAngle);

CFan<T>& operator = (const CFan &fan);

CPt2D<T> Center() const;

T Radius() const;

double FanAngle() const;

double StartAngle() const;

CPt2D<T> StartPoint() const;

CPt2D<T> EndPoint() const;

void SetCenter(const CPt2D<T> &ptCenter);

void SetRadius(T radius);

void SetFanAngle (double dAngle);

void SetStartAngle(double dAngle);

void SetStartPoint(const CPt2D<T> &pt);

void SetEndPoint (const CPt2D<T> &pt);

CRect<T> BoundingRect() const;

void Offset(T x, T y);

void Offset(const CPt2D<T> &ptOffset);

int PtIn (T x, T y); const;

int PtIn (const CPt2D<T> &pt) const;

void Rotate(const CPt2D<T> &ptRot, double dAngle);

private:

CPt2D<T> m_ptCenter; // center of the fan

T m_Radius; // radius of the fan

double m_dFanAngle; // anticlockwise angle (radius) of the fan

double m_dStartAngle; // anticlockwise start angle (radius)

};

14

Page 15: Doc Imgp Roc Lib

Function Descriptionoperator = Assignment operatorCenter Get the center of the fan, which is the center of the full circle.Radius Get the radius of the fanFanAngle Get the angle of the fan, anticlockwise in radiusStartAngle Get the start angle of the fan, anticlockwise in radiusStartPoint Get the start point of the fanEndPoint Get the end point of the fanSetCenter Set the center of the fanSetRadius Set the radius of the fanSetFanAngle Set the angle of the fan, anticlockwise in radiusSetStartAngle Set the start angle of the fan, anticlockwise in radiusSetStartPoint Set the start point of the fanSetEndPoint Set the end point of the fanBoundingRect Get the bounding box of the fanOffset Offset the position of the fanPtIn Check if a point is inside the fanRotate Rotate the fan with respect to a point

0 100 200 X0

100

200

Y

start anglefan angle

radius

center

start pointend point

Figure 5: An example of a fan.

2.7 CCircle: circle class

template <class T> class CCircle

{

public:

CCircle<T>() : m_ptCenter(0,0), m_Radius(0) { }

CCircle<T>(const CCircle<T> &circle);

CCircle<T>(const CPt2D<T> &ptCenter, T radius)

CCircle<T>(T x, T y, T radius)

CCircle<T>& operator = (const CCircle<T> &circle);

CPt2D<T> Center() const

T Radius() const

void SetCenter(const CPt2D<T> &pt)

void SetRadius(T radius)

CRect<T> BoundingRect() const;

void Offset(T x, T y);

void Offset(const CPt2D<T> &ptOffset);

int PtIn (T x, T y) const;

int PtIn (const CPt2D<T> &pt) const;

void Rotate(const CPt2D<T> &ptRot, double dAngle);

private:

CPt2D<T> m_ptCenter; // center of the circle

T m_Radius; // radius of the circle

};

15

Page 16: Doc Imgp Roc Lib

Function Descriptionoperator = Assignment operatorCenter Get the center of the circleRadius Get the radius of the circleSetCenter Set the center of the circleSetRadius Set the radius of the circleBoundingRect Get the bounding box of the circleOffset Offset the position of the circlePtIn Check if a point is inside the circleRotate Rotate the circle with respect to a point

0 100 200 X0

100

200

Y

radius center

Figure 6: An example of a circle.

2.8 CEllipse: ellipse class

template <class T> class CEllipse

{

public:

CEllipse<T>() : m_ptCenter(0,0), m_width(0), m_height(0), m_dAngle(0) { }

CEllipse<T>(const CEllipse &ellipse);

CEllipse<T>(T x, T y, T width, T height, double dAngle = 0);

CEllipse<T>(const CPt2D<T> &ptCenter, T width, T height, double dAngle = 0);

CEllipse<T>(const CPt2D<T> &ptCenter, CSize2D<T> size, double dAngle = 0);

CEllipse<T>& operator = (const CEllipse &ellipse);

T Width() const;

T Height() const;

CPt2D<T> Center() const;

double Angle() const;

void SetWidth (T width);

void SetHeight(T height);

void SetAngle (double dAngle);

void SetCenter(const CPt2D<T> &ptCenter);

double DistToFoci(const CPt2D<T> &pt) const;

CRect<T> BoundingRect() const;

void Offset(T x, T y);

void Offset(const CPt2D<T> &ptOffset);

int PtIn (T x, T y); const;

int PtIn (const CPt2D<T> &pt) const;

void Rotate(const CPt2D<T> &ptRot, double dAngle);

bool SetEqu(const double pdCoe[5]);

private:

CPt2D<T> m_ptCenter; // center of the ellipse

T m_width; // width of the ellipse

T m_height; // height of the ellipse

double m_dAngle; // anticlockwise angle (radius) of the ellipse

};

16

Page 17: Doc Imgp Roc Lib

Function Descriptionoperator = Assignment operatorWidth Get the width of the ellipseHeight Get the height of the ellipseCenter Get the center of the ellipseAngle Get the angle of the ellipse, anticlockwise in radiusSetWidth Set the width of the ellipseSetHeight Set the height of the ellipseSetCenter Set the center of the ellipseSetAngle Set the angle of the ellipse, anticlockwise in radiusDistToFoci Get the distance of a point to the two foci of the ellipseBoundingRect Get the bounding box of the ellipseOffset Offset the position of the ellipsePtIn Check if a point is inside the ellipseRotate Rotate the ellipse with respect to a point

SetEqu Set the ellipse based on an equationEllipse equation: Ax2 +Bxy + Cy2 +Dx+ Ey = 1

pdCoe parameters, A,B,C,D and E, of the ellipse equationReturn true: succeeded; false: the input equation is not an ellipse equation and no

change is made.

0 100 200 X0

100

200

Y

width

height

angle

center

Figure 7: An example of an ellipse.

2.9 CPoly2D: 2D polygon class

template <class T> class CPoly2D

{

public:

CPoly2D<T>() { }

CPoly2D<T>(const CPoly2D<T> &poly);

CPoly2D<T>(const vector<CPt2D<T> > &vpt)

CPoly2D<T>(int nCount, const CPt2D<T> &pt);

CPoly2D<T>(const CPt2D<T> *ppt, int nCount)

CPoly2D<T>& operator = (const CPoly2D<T> &poly);

int Assign(const vector<CPt2D<T> > &vpt);

int Count() const;

const CPt2D<T>& Vertex(int idx) const;

void AddVertex ( const CPt2D<T> &pt);

void SetVertex (int idx, const CPt2D<T> &pt);

void InsertVertex(int idx, const CPt2D<T> &pt);

void RemoveVertex(int idx);

void RemoveNearbyVertices(double dDist);

void Clear();

CPt2D<T> Center() const;

CRect<T> BoundingRect() const;

void Offset(T x, T y);

17

Page 18: Doc Imgp Roc Lib

void Offset(const CPt2D<T> &ptOffset);

int PtIn (T x, T y) const;

int PtIn (const CPt2D<T> &pt) const;

void Rotate(const CPt2D<T> &ptRot, double dAngle);

private:

vector<CPt2D<T> > m_vpt; // vertices of the polygon

};

Function Descriptionoperator = Assignment operatorCount Get the number of vertices of the polygonVertex Get the idx-th vertex of the polygonAddVertex Add a vertex at the end of the vertex listSetVertex Set the idx-th vertex valueInsertVertex Insert a vertex at idx position of the vertex listRemoveVertex Remove the idx-th vertex from the vertex listRemoveNearbyVertices Remove vertices too close together, which makes sure that the

length of each edge is >= dDist.Clear Remove all vertices of the polygonCenter Get the centroid of the polygonBoundingRect Get the bounding box of the polygonOffset Offset the position of the polygonPtIn Check if a point is inside the polygonRotate Rotate the polygon with respect to a point

0 100 200 X0

100

200

Y

v0v1

v2

v3 v4

v5

v6

Figure 8: An example of a polygon.

2.10 CLCSpl: a linear spline class

template <class T> class CLSpl

{

public:

CLSpl<T>() { }

CLSpl<T>(const CLSpl<T> &lspl);

CLSpl<T>(const vector<CPt2D<T> > &vpt);

CLSpl<T>(const CPt2D<T> *ppt, int nCount);

CLSpl<T>& operator = (const CLSpl<T> &lspl);

int Count() const;

const CPt2D<T>& Knot(int idx) const;

void AddKnot ( const CPt2D<T> &pt);

void SetKnot (int idx, const CPt2D<T> &pt);

void InsertKnot(int idx, const CPt2D<T> &pt);

void RemoveKnot(int idx);

void Clear();

CPt2D<T> Center() const;

CPt2D<T> BoundingRect() const;

void Offset(T x, T y);

18

Page 19: Doc Imgp Roc Lib

void Offset(const CPt2D<T> &ptOffset);

void Rotate(const CPt2D<T> &ptRot, double dAngle);

private:

vector<CPt2D<T> > m_vpt; // knots of the spline

};

Function Descriptionoperator = Assignment operatorCount Get the number of knots of the splineKnot Get the idx-th knot of the splineAddKnot Add a knot to the knot listSetKnot Set the idx-th knot valueRemoveKnot Remove the idx-th knot from the knot listClear Remove all knots of the splineCenter Get the centroid of all knotsOffset Offset the position of all knotsRotate Rotate the spline with respect to a point

0 100 200 X0

100

200

Y

k0 k1

k2

k3

k4

k5

Figure 9: An example of a linear spline.

2.11 CNCSpl: a natural cubic spline class

template <class T> class CNCSpl

{

public:

CNCSpl<T>() { }

CNCSpl<T>(const CNCSpl<T> &ncspl);

CNCSpl<T>& operator = (const CNCSpl<T> &ncspl);

int Count() const;

CPt2D<T> Knot(int idx) const;

void AddKnot ( const CPt2D<T> &pt);

void SetKnot (int idx, const CPt2D<T> &pt);

void InsertKnot(int idx, const CPt2D<T> &pt);

void RemoveKnot(int idx);

void Clear();

CPt2D<T> Center() const;

void Offset(T x, T y);

void Offset(const CPt2D<T> &ptOffset);

void Rotate(const CPt2D<T> &ptRot, double dAngle);

bool GetPointAt(int nSec, double dFra, CPt2D<T> *ppt) const;

};

Function Descriptionoperator = Assignment operatorCount Get the number of knots of the spline

continued on next page

19

Page 20: Doc Imgp Roc Lib

Knot Get the idx-th knot of the splineAddKnot Add a knot to the knot listSetKnot Set the idx-th knot valueRemoveKnot Remove the idx-th knot from the knot listClear Remove all knots of the splineCenter Get the centroid of all knotsOffset Offset the position of all knotsRotate Rotate the spline with respect to a point

GetPointAt Get a point at a specified place on the splinenSec the index of the section of the spline

For example: i is the section between knot i and knot i+1.dFra fractional distance [0, 1] from the point to the start point of the section in

x direction. For example: assume i-th section is chosen;fFraction = 0, returns the start point (x[i], y[i]).fFraction = 1, returns the end point (x[i+1], y[i+1]).fFraction = 0.3, returns the point (0.7*x[i]+0.3*x[i+1], y). y is calculatedfrom the spline.dFra can also be set < 0 or > 1. In these cases, the point’s x coordinate is< x[i] or > x[i+1].

ppt stores output point if foundReturn true: point found; false: spline is not valid or nSec out of range, in that

case ppt is unchanged.

0 100 200 X0

100

200

Y

k0k1

k2

k3k4

k5

k6k7

Figure 10: An example of a natural cubic spline.

2.12 Distance

template <class T> double Distance

(const CPt2D<T> &pt1, const CPt2D<T> &pt2);

Description Get the distance between two pointspt1 1st input pointpt2 2nd input pointReturn the distance between the two points

0 100 200 X0

100

200

Y

pt1

pt2distance

Figure 11: An example of the Distance function. Distance between two points.

template <class T> double Distance

(const CPt2D<T> &pt, double dA, double dB, double dC);

20

Page 21: Doc Imgp Roc Lib

Description Get the distance between a point and a lineThe line equation is A*x + B*y = C.

pt input pointdA, dB, dC parameters of the lineReturn the distance between the point and the line

0 100 200 X0

100

200

Y

pt

line

distance

Figure 12: An example of the Distance function. Distance between a point and a line.

2.13 Area2

template <class T> double Area2

(const CPt2D<T> &pt1, const CPt2D<T> &pt2, const CPt2D<T> &pt3);

Description Get twice the area enclosed by three pointspt1 1st input pointpt2 2nd input pointpt3 3rd input pointReturn twice the area enclosed by three points

Use double type to prevent overflow.area > 0 indicates pt1, pt2, pt3 are clockwise.area < 0 indicates pt1, pt2, pt3 are anticlockwise.area = 0 indicates three points are on the same line.

0 100 200 X0

100

200

Y pt1

pt2

pt3

0 100 200 X0

100

200

Y pt3

pt2

pt1

(a) (b)

Figure 13: An example of the Area2 function. (a) Positive area. (b) Negative area.

2.14 Angle

template <class T> double Angle

(const CPt2D<T> &pt1, const CPt2D<T> &pt2, const CPt2D<T> &pt3);

Description Get the angle in (−π, π], formed by pt2, pt1, pt3pt1 1st input point (vertex of the angle)pt2 2nd input pointpt3 3rd input pointReturn the angle formed by pt2, pt1, pt3

angle > 0 indicates pt1, pt2, pt3 are clockwise.angle < 0 indicates pt1, pt2, pt3 are anticlockwise.angle = 0 or π indicates three points are on the same line.

21

Page 22: Doc Imgp Roc Lib

0 100 200 X0

100

200

Y

pt1

pt2

pt3

0 100 200 X0

100

200

Y

pt1

pt3

pt2

(a) (b)

Figure 14: An example of the Angle function. (a) Positive angle. (b) Negative angle.

2.15 MidPoint

template <class T> CPt2D<T> MidPoint

(const CPt2D<T> &pt1, const CPt2D<T> &pt2);

Description Get the midpoint of two pointspt1 1st input pointpt2 2nd input pointReturn the midpoint

2.16 Centroid

template <class T> CPt2D<T> Centroid

(const CPt2D<T> *ppt, int nCount);

Description Get the centroid of a set of pointsppt an array of pointsnCount the number of pointsReturn the centroid of the input points

2.17 Intersect

template <class T> bool Intersect

(const CLine2D<T> &line1, const CLine2D<T> &line2, CPt2D<double> *ppt);

Description Get the intersect of two line segmentsline1 1st input line segmentline2 2nd input line segmentppt store point of intersection, if found; otherwise, its value is unchanged.Return true: intersect found; false: no intersect.

bool Intersect

(double dA1, double dB1, double dC1,

(double dA2, double dB2, double dC2,

CPt2D<double> *ppt);

Description Get the intersect of two linesLine equation: A*x + B*y = C

dA1, dB1, dC1 parameters of the 1st input linedA2, dB2, dC2 parameters of the 2nd input lineppt store point of intersection, if found; otherwise, its value is unchanged.Return true: intersect found; false: no intersect.

template <class T> bool Intersect

(const CLine2D<T> &line1, double dA2, double dB2, double dC2,

CPt2D<double> *ppt);

22

Page 23: Doc Imgp Roc Lib

Description Get the intersect of a line segment with a lineLine equation: A*x + B*y = C

line1 the line segmentdA2, dB2, dC2 parameters of the lineppt store point of intersection of two lines, if found. One of the line is derived

from the line segment. If return true, the point is also on the line segment.Return true: intersect found; false: no intersect.

0 100 200 X0

100

200

Y

0 100 200 X0

100

200

Y

(a) (b)

Figure 15: An example of the Intersect function. (a) Two line segments intersect. (b) Two line segmentsdo not intersect.

2.18 CutLineInRect

template <class T> bool CutLineInRect

(const CRect<T> &rect, double dA, double dB, double dC,

CLine2D<T> *pLine);

Description Cut a line within a rectangleA line is cut to fit inside a rect, which means a point’s x is within [left,right] and y within [top, bottom]. If you do not want to include the rightand bottom borders, you may use the integer version of the function.

rect rectangle to intersect with the linedA, dB, dC line equation: A*x + B*y = CpLine return the cut line if it exists; otherwise its value is unchanged.Return true: the line is cut in the rect;

false: the line does not intersect with the rect.

bool CutLineInRect(const CRect<int> &rect, CLine2D<int> *pLine);

Description Cut a line segment within a rectangleA line segment is cut to fit inside a rect, which means a point’s x is within[left, right) and y within [top, bottom). Note that the right and bottomborder are not “inside”.

rect rectangle to intersect with the line segmentpLine the line segment to be cut. It also returns the cut line if it exists; otherwise

its value is unchanged.Return true: the line segment is cut or is already in the rect;

false: the line segment is outside the rect and no intersect.

2.19 BoundingRect

template <class T> CRect<T> BoundingRect

(const CPt2D<T> *pptVtx, int nCount);

23

Page 24: Doc Imgp Roc Lib

0 100 200 X0

100

200

Y

0 100 200 X0

100

200

Y

(a) (b)

Figure 16: An example of the CutLineInRect function. (a) Before applying CutLineInRect(...). (b) Afterapplying CutLineInRect(...).

Description Get the bounding rectangle of a set of pointsppt an array of pointsnCount the number of pointsReturn The bounding rectangle, whose left and right equal to the min and max x

coordinates of the points; and top and bottom equal to the min and max ycoordinates of the points.

Note Right and bottom of bounding rect is equal but not larger than the max ofx or y coordinates.

2.20 PointInPolygon

template <class T> int PointInPolygon

(const CPt2D<T> *ppt, int nCount, T x, T y);

Description Check if a point is inside, on, or outside a polygon.Reference: ”Computational geometry in C” 7.4, Joseph O’Rourke.Ray-crossing algorithm. (Winding number algo is slower.)

ppt polygon verticesnCount the number of verticesx, y point to be testedReturn 0: outside,

1: strictly inside,2: on one of the edges but not on a vertex,3: on one of the vertices.

0 100 200 X0

100

200

Y

pt1

pt2

pt3

pt4

Figure 17: An example of the PointInPolygon function. pt1 is outside the polygon; pt2 is strictly inside;pt3 is on one of the edges but not on a vertex; pt4 is on one of the vertices.

2.21 ConvexHull

template <class T> int ConvexHull

(const vector<CPt2D<T> > &vPos, vector<int> *pvIdx);

24

Page 25: Doc Imgp Roc Lib

Description Get convex hull points’ indexPrecondition see ConvexHull(vector<CPt2D<T> >*, vector<CPt2D<T> >*)

vPos input point setpvIdx indices of convex hull points in vPos (clockwise order)Return the number of convex hull points

template <class T> int ConvexHull

(vector<CPt2D<T> > *pvPos, vector<CPt2D<T> > *pvCH);

Description Get the convex hull points in clockwise orderReference ”Computational geometry in C” by Joseph O’Rourke. The firstpoint of vPos must be the topmost of all points, meaning that it has thesmallest y coordinate. If there are more than one topmost points, the firstpoint should be the leftmost of them, with the smallest x coordinate.

pvPos input points (the array will be modified)pvCH a vector pointer that stores the found convex hull pointsReturn the number of convex hull points

2.22 BoundingBox

template <class T> bool BoundingBox

(const vector<CPt2D<T> > &vPos, double dAngle,

CPt2D<double> *pptCenter, CSize2D<double> *pSize);

Description Get the fixed-angle bounding box of a set of pointsvPos input object pointsdAngle angle of the bounding box (radius, anticlockwise)pptCenter center of the boxpSize size of the boxReturn true: succeeded; false: failed.

2.23 MinBoundingBox

template <class T> bool MinBoundingBox

(const vector<CPt2D<T> > *pvPos, CPt2D<double> *pptCenter,

CSize2D<double> *pSize, double *pdAngle);

Description Get the min bounding box of a set of pointsMinBoundingBox function is based on the ”rotating caliper” algorithm.The output angle is anticlockwise; while some internal angles are clockwise.Since ConvexHull(...) outputs points in clockwise order, it is convenient touse clockwise angle internally.

Precondition see ConvexHull(vector<CPt2D<T> >*, vector<CPt2D<T> >*)

pvPos input points (the array will be modified)pptCenter center of the boxpSize size of the boxpdAngle angle of the box (radius, anticlockwise)Return true: succeeded; false: failed.

2.24 MinBoundingCircle

template <class T> bool MinBoundingCircle

(const vector<CPt2D<T> > *pvPos, CPt2D<double> *pptCenter, double *pdRadius);

Description Get the min bounding circle of a set of points1 Call the convex hull of the set of points H. Pick any side of H, say S.2 For each vertex of H other than those of S, compute the angle subtended

by S. The minimum such angle, α, occurs at vertex v.If α ≥ 90 deg, done! (The circle is the diametric circle of S.) If α < 90 deg,check the remaining vertices of the triangle formed by S and v.

continued on next page

25

Page 26: Doc Imgp Roc Lib

3 If no vertices are obtuse, done! (The circle is determined by the vertices ofS and the vertex v.)If one of the other angles of the triangle formed by S and v is obtuse, thenset S to be the side opposite the obtuse angle and go to step 2. (The newS may not be a side on the convex hull.)

Precondition see ConvexHull(vector<CPt2D<T> >*, vector<CPt2D<T> >*)

pvPos input points (the array will be modified)pptCenter center of the circlepdRadius radius of the circleReturn true: succeeded; false: failed.

2.25 DelaunayTriangulation

template <class T> int DelaunayTriangulation

(const vector<CPt2D<T> > &vPos,

vector<CLine2D<T> > *pvLine, vector<CPoly2D<T> > *pvPoly);

Description Get the Delaunay triangulation of a set of pointsReference ”Computational geometry in C” by Joseph O’Rourke.Delaunay triangulation in 2D is based on convex hull in 3D.

vPos input point setpvLine edges of Delaunay triangles (input 0 to ignore)pvPoly Delaunay triangles (input 0 to ignore). Each polygon contains only 3 ver-

tices.Return the number of triangles

(a) (b) (c) (d) (e)

Figure 18: Examples of the ConvexHull, BoundingBox, MinBoundingBox, MinBoundingCircle and De-launayTriangulation functions. (a) Convex hull. (b) Bounding box at a fixed angle of 15 deg. (c)Minimum-area bounding box. (d) Minimum-area bounding circle. (e) Delaunay triangulation.

2.26 FitLine

template <class T> bool FitLine

(const CPt2D<T> *ppt, int nCount, double *pdKx, double *pdY0);

Description Least squares fitting of a lineLine equation: y = kxx + y0. Fitting is based on the y-direction error, notthe perpendicular distance error.

ppt points to be fitted (size must be nCount)nCount size of ppt arraypdKx parameter kx of the line equationpdY0 parameter y0 of the line equationReturn true: succeeded; false: failed.

2.27 FitCircle

template <class T> bool FitCircle

(const CPt2D<T> *ppt, int nCount,

CPt2D<double> *pptCenter, double *pdRadius);

26

Page 27: Doc Imgp Roc Lib

Description Least squares fitting of a circleCircle equation: (x− a)2 + (y − b)2 = R2

Internally fit the modified equation: Ax+By + C = x2 + y2

If a fitted circle is found, circle parameters are stored in pptCenter, pdRa-dius; otherwise they are unchanged.

ppt points to be fitted (size must be nCount)nCount size of ppt arraypptCenter pointer to the center of the circlepdRadius pointer to the radius of the circleReturn true: succeeded; false: failed.

2.28 FitEllipse

template <class T> bool FitEllipse

(const CPt2D<T> *ppt, int nCount, double pdCoe[5]);

Description Least squares fitting of an ellipseEllipse equation: Ax2 +Bxy + Cy2 +Dx+ Ey = 1

ppt points to be fitted (size must be nCount)nCount size of ppt arraypdCoe coefficients, A,B,C,D and E, of the ellipse equationReturn true: succeeded; false: failed.

2.29 FitPolynomialCurve

template <class T> bool FitPolynomialCurve

(const CPt2D<T> *ppt, int nCount, vector<double> *pvCoe, int N);

Description Least squares fitting of a polynomial curvePolynomial curve equation:y = C[0] + coe[1] ∗ x+ C[2] ∗ x2 + ...+ C[N ] ∗ xN

ppt points to be fitted (size must be nCount)nCount size of ppt arraypvCoe stored coefficients. If succeed, its size is N+1.N the order of the curve equation (should be >= 0)Return true: succeeded; false: failed.

X

0 10 20 30 40 50 60 70 80 90 100

Y

-200

0

200

400

Input points

2nd order curve

3rd order curve

4th order curve

5th order curve

Figure 19: An example of the FitPolynomialCurve function.

2.30 FitGauss

template <class T> bool FitGuass

(const CPt2D<T> *ppt, int nCount, double *pdA, double *pdB, double *pdC);

27

Page 28: Doc Imgp Roc Lib

Description Least squares fitting of a Gaussian function

Gaussian function: y = Ae−(x−B)2/(2C). Fitting is based on a linear leastsquares method: the y coordinate of all points is converted to ln(y), whichrequires that y > 0.

ppt points to be fitted (size must be nCount)nCount size of ppt arraypdA parameter A of the Gaussian equationpdB parameter B of the Gaussian equationpdC parameter C of the Gaussian equationReturn true: succeeded; false: failed.

X

0 20 40 60 80 100

Y

0

20

40

Input points

FitGauss

Figure 20: An example of the FitGauss function.

2.31 HoughLine

template <class T> int HoughLine

(const CPt2D<T> *ppt, int nCount,

double dRMin, double dRMax, int nRSec,

double dAngleMin, double dAngleMax, int nAngleSec,

double *pdR, double *pdAngle);

Description Hough transform line detectionDetect one line with maximum hough parameter.Line equation: R = x*cos(angle) + y*sin(angle)Resolution of R is (dRMax-dRMin)/nRSec.Resolution of angle is (dAngleMax-dAngleMin)/nAngleSec.

ppt points to be analyzed (size must be nCount)nCount size of ppt arraydRMin minimum boundary of R [dRMin, dRMax)dRMax maximum boundary of R [dRMin, dRMax)nRSec the number of sections in [dRMin, dRMax)dAngleMin minimum boundary of angle [dAngleMin, dAngleMax)dAngleMax maximum boundary of angle [dAngleMin, dAngleMax)nAngleSec the number of sections in [dAngleMin, dAngleMax)pdR found R of the line equationpdAngle found angle (in radius) of the line equationReturn 0: line not found;

>0 line found (return the number of points on the line).

2.32 HoughCircle

template <class T> int HoughCircle

(const CPt2D<T> *ppt, int nCount,

double dXMin, double dXMax, int nXSec,

double dYMin, double dYMax, int nYSec,

double dRMin, double dRMax, int nRSec,

CPt2D<double> *pptCenter, double *pdR);

28

Page 29: Doc Imgp Roc Lib

Description Hough transform circle detectionDetect one circle with maximum hough parameter.Circle equation: R = (x− x0)2 + (y − y0)2

Resolution of center x is (dXMax-dXMin)/nXSec.Resolution of center y is (dYMax-dYMin)/nYSec.Resolution of radius is (dRMax-dRMin)/nRSec.

ppt points to be analyzed (size must be nCount)nCount size of ppt arraydXMin minimum boundary of the circle center x coordinatedXMax maximum boundary of the circle center x coordinatedYMin minimum boundary of the circle center y coordinatedYMax maximum boundary of the circle center y coordinatedRMin minimum boundary of R [dRMin, dRMax)dRMax maximum boundary of R [dRMin, dRMax)nXSec the number of sections in [dXMin, dXMax)nYSec the number of sections in [dYMin, dYMax)nRSec the number of sections in [dRMin, dRMax)pptCenter found circle centerpdR found circle radiusReturn 0: circle not found;

>0 circle found (return the number of points on the circle).

X

0 20 40 60 80 100

Y

0

50

Input points

FitLine

HoughLine

X

20 40 60 80 100

Y

0

20

40

60

Input points

FitCircle

HoughCircle

(a) (b)

Figure 21: Examples of the FitLine, HoughLine, FitCircle and HoughCircle functions. (a) FitLine andHoughLine. (b) FitCircle and HoughCircle.

2.33 GetLinePoints

int GetLinePoints

(const CRect<int> &rcROI,

const CPt2D<int> &ptStart, const CPt2D<int> &ptEnd,

vector<CPt2D<int> > *pvPos);

Description Get points on a line within a bounding rectrcROI bounding rect (only get points inside the rect)ptStart start point of the lineptEnd end point of the linepvPos store points on the lineReturn the number of pixels on the line

int GetLinePoints

(const CPt2D<int> &ptStart, const CPt2D<int> &ptEnd,

vector<CPt2D<int> > *pvPos);

Description Bresenham’s line algorithmptStart start point of the lineptEnd end point of the linepvPos store points on the lineReturn the number of pixels on the line

29

Page 30: Doc Imgp Roc Lib

2.34 GetCirclePoints

int GetCirclePoints

(const CPt2D<int> &ptCenter, int nRadius,

vector<CPt2D<int> > *pvPos);

Description Bresenham’s circle algorithmptCenter center of the circlenRadius radius of the circlepvPos store points on the circleReturn the number of pixels on the circle

30

Page 31: Doc Imgp Roc Lib

3 2D image processing functions

Some parameters are common to most functions. They are listed in Table 2.

Type Name Description

T* pImg Pointer to a continuous memory space which must be equal toor larger than w*h*sizeof(T) bytes. The y-th row, x-th columnelement can be retrieved by *(pImg+y*w+x) or pImg[y*w+x].

T* pSrc Pointer to the source imageT* pDst Pointer to the destination imageint w, h Image width and heightCRect<int>& rcROI Region of interest. Only image data within the ROI will be used

or modified.T ObjID Pixels whose value==ObjID are treated as object points; other-

wise background.

Table 2: Common parameters of 2D image processing functions.

3.1 ImgAssign

template <class T> void ImgAssign

(T *pImg, int w, int h, const CRect<int> &rcROI,

T value);

Description Assign image data in an ROI to the input valueParameters See Table 2 for common parametersvalue the value to be assigned to the image data

3.2 ImgAssignBorder

template <class T> void ImgAssignBorder

(T *pImg, int w, int h, const CRect<int> &rcROI,

T value, int nLeft, int nTop, int nRight, int nBottom);

Description Assign image data on the border of an ROI to the input valuePixels within the distance are assigned to the input value. The modifiedregion is a rectangular ring.

Parameters See Table 2 for common parametersnLeft distance toward the center on the left bordernTop distance toward the center on the top bordernRight distance toward the center on the right bordernBottom distance toward the center on the bottom border

ROI ROI

(a) (b) (c)

Figure 22: Examples of the ImgAssign and ImgAssignBorder functions. (a) Original image. (b) The ROIis assigned with value 220. (c) The border of the ROI is assigned with value 220. Left border: 7 pixels,top: 10 pixels, right: 5 pixels, bottom: 3 pixels.

31

Page 32: Doc Imgp Roc Lib

3.3 ImgCopy

template <class T1, class T2> void ImgCopy

(const T1 *pSrc, int w1, int h1, const CRect<int> &rcROI1,

T2 *pDst, int w2, int h2, const CRect<int> &rcROI2);

Description Copy image data from source image ROI1 to destination image ROI2Width and height of ROI1 and ROI2 must be the same.

Parameters See Table 2 for common parameters

ROI1

ROI2

(a) (b) (c)

Figure 23: An example of the ImgCopy function. (a) Source image ROI1 is copied to (b) destinationimage ROI2. (c) Resultant image after ImgCopy.

3.4 ImgCopySubpixel

template <class T1, class T2> void ImgCopySubpixel

(const T1 *pSrc, int w1, int h1, const CRect<int> &rcROI1,

T2 *pDst, int w2, int h2, const CRect<int> &rcROI2,

const CPt2D<float> &ptOffset);

Description Copy image data at subpixel accuracyCopy image ROI1 offset by ptOffset to ROI2. Each subpixel in ROI1 islinearly interpolated from interger pixels. Width and height of ROI1 andROI2 must be the same.

Parameters See Table 2 for common parametersptOffset subpixel offset of ROI1

3.5 ImgBlend

template <class T1, class T2> void ImgBlend

(const T1 *pImg1, int w1, int h1, const CRect<int> &rcROI1,

T2 *pImg2, int w2, int h2, const CRect<int> &rcROI2,

double dCoe1, double dCoe2);

Description Blend img1 into img2Blending equation: img2 = dCoe1*img1 + dCoe2*img2.

Parameters See Table 2 for common parametersdCoe1 coefficient multiplied to each pixel in ROI1dCoe2 coefficient multiplied to each pixel in ROI2

3.6 ImgGradientX

template <class T1, class T2> void ImgGradientX

(const T1 *pSrc, int w1, int h1, const CRect<int> &rcROI1,

T2 *pDst, int w2, int h2, const CRect<int> &rcROI2,

int nGradLen);

32

Page 33: Doc Imgp Roc Lib

Sample

(a) (b) (c)

Figure 24: An example of the ImgBlend function. (a) Image 1. (b) Image 2. (c) The resultant image ofblending image 1 into image 2.

Description 1D gradient in the x directionTake the image data gradient in the x direction. If the gradient length is 2,the x gradient pDst[x] = pSrc[x+1] - pSrc[x-1], meaning the gap betweenthe two pixels used to calculate a gradient point is 2.

Parameters See Table 2 for common parametersnGradLen length of the gradient operator

3.7 ImgGradientY

template <class T1, class T2> void ImgGradientY

(const T1 *pSrc, int w1, int h1, const CRect<int> &rcROI1,

T2 *pDst, int w2, int h2, const CRect<int> &rcROI2,

int nGradLen);

Description 1D gradient in the y directionTake the image data gradient in the y direction. If the gradient lengthis 2, the y gradient pDst[y*w+x] = pSrc[(y+1)*w+x] - pSrc[(y-1)*w+x],meaning the gap between the two pixels used to calculate a gradient pointis 2.

Parameters See Table 2 for common parametersnGradLen length of the gradient operator

3.8 ImgClamp

template <class T> void ImgClamp

(T *pImg, int w, int h, const CRect<int> &rcROI,

T toMin, T toMax);

Description Clamp image data to the range [toMin, toMax]Parameters See Table 2 for common parameterstoMin minimum bound to clamp the image datatoMax maximum bound to clamp the image data

3.9 ImgLinear

template <class T1, class T2> void ImgLinear

(const T1 *pSrc, int w1, int h1, const CRect<int> &rcROI1,

T2 *pDst, int w2, int h2, const CRect<int> &rcROI2,

T1 fromMin, T1 fromMax, T2 toMin, T2 toMax);

Description Linear translate Src data in the range [fromMin, fromMax] to Dst datato the range [toMin, toMax]. Linear translation parameters a and b isdetermined by:a * fromMin + b = toMin;a * fromMax + b = toMax;

continued on next page

33

Page 34: Doc Imgp Roc Lib

Data points in Src that are < fromMin or > fromMax will be translate totoMin and toMax. pSrc and pDst may point to the same image buffer, ifROI1 and ROI2 are the same.

Parameters See Table 2 for common parametersfromMin source minimum boundfromMax source maximum boundtoMin destination minimum boundtoMax destination maximum bound

(a) (b) (c)

Figure 25: Examples of the ImgClamp and ImgLinear functions. (a) Source image. (b) Image data areclamped to [50, 200]. (c) Image data are linearly translated to [50, 200].

3.10 ImgGamma

template <class T1, class T2> void ImgGamma

(const T1 *pSrc, int w1, int h1, const CRect<int> &rcROI1,

T2 *pDst, int w2, int h2, const CRect<int> &rcROI2,

double dGamma);

Description Apply gamma transformGamma transform equation: y = xgamma, where x ∈ [0, 1]. This functionfirst maps all data from [min, max] to [0, 1]; then applies gamma transform;and finally maps the data back to [min, max]. pSrc and pDst may point tothe same image buffer, if ROI1 and ROI2 are the same.

Parameters See Table 2 for common parametersdGamma gamma value

(a) (b) (c)

Figure 26: Examples of the ImgGamma function. (a) Source image. (b) The image after transformed bygamma = 0.7. (c) The image after transformed by gamma = 1.5.

3.11 ImgMin

template <class T> T ImgMin

(const T *pImg, int w, int h, const CRect<int> &rcROI);

Description Get the minimum value of an ROIParameters See Table 2 for common parametersReturn the minimum value of an ROI

34

Page 35: Doc Imgp Roc Lib

3.12 ImgMax

template <class T> T ImgMax

(const T *pImg, int w, int h, const CRect<int> &rcROI);

Description Get the maximum value of an ROIParameters See Table 2 for common parametersReturn the maximum value of an ROI

3.13 ImgMinMax

template <class T> void ImgMinMax

(const T *pImg, int w, int h, const CRect<int> &rcROI,

T *pMin, T *pMax);

Description Get the minimum and maximum value of an ROIParameters See Table 2 for common parameterspMin return the minimum value of an ROIpMax return the maximum value of an ROI

3.14 ImgMean

template <class T> T ImgMean

(const T *pImg, int w, int h, const CRect<int> &rcROI);

Description Get the mean value of an ROIParameters See Table 2 for common parametersReturn the mean value of an ROI

3.15 ImgMedian

template <class T> T ImgMedian

(const T *pImg, int w, int h, const CRect<int> &rcROI);

Description Get the median value of an ROIParameters See Table 2 for common parametersReturn the median value of an ROI

3.16 ImgVariance

template <class T> double Variance

(const T *pImg, int w, int h, const CRect<int> &rcROI);

Description Get the variance of an ROIParameters See Table 2 for common parametersReturn the variance of an ROI

3.17 ImgStdDev

template <class T> double ImgStdDev

(const T *pImg, int w, int h, const CRect<int> &rcROI);

Description Get the standard deviation of an ROIParameters See Table 2 for common parametersReturn the standard deviation of an ROI

3.18 ImgStatistics

template <class T> void ImgStatistics

(const T *pImg, int w, int h, const CRect<int> &rcROI);

T *pMin, T *pMax, T *pMean, double *pdStdDev);

35

Page 36: Doc Imgp Roc Lib

Description Get statistics of an ROIParameters See Table 2 for common parameterspMin return the minimum value of an ROIpMax return the maximum value of an ROIpMean return the mean value of an ROIpdStdDev return the standard deviation of an ROI

3.19 ImgFltMean

template <class T> void ImgFltMean

(const T *pSrc, int w1, int h1, const CRect<int> &rcROI1,

T *pDst, int w2, int h2, const CRect<int> &rcROI2,

int nKerW, int nKerH);

Description Mean filter, kernel certer equal to kernel meanHalf-filter-length data at the ROI boundary are filtered with reduced sizedkernel.

Parameters See Table 2 for common parametersnKerW filter kernel widthnKerH filter kernel heightExample nKerW*nKerH = 3*3 or 7*5

template <class T1, class T2, class T3> void ImgFltMean

(const T1 *pImg, int w1, int h1, const CRect<int> &rcROI1,

const T2 *pWei, int w2, int h2, const CRect<int> &rcROI2,

T3 *pDst, int w3, int h3, const CRect<int> &rcROI3,

int nKerW, int nKerH);

Description Weighted mean filterParameters See Table 2 for common parameterspWei weighting. (rcROI1, rcROI2 and rcROI3 must be the same size)

3.20 ImgFltMedian

template <class T> void ImgFltMedian

(const T *pSrc, int w1, int h1, const CRect<int> &rcROI1,

T *pDst, int w2, int h2, const CRect<int> &rcROI2,

int nKerW, int nKerH);

Description Median filter, kernel certer equal to kernel medianHalf-filter-length data at the ROI boundary are filtered with reduced sizedkernel.

Parameters See Table 2 for common parametersnKerW filter kernel widthnKerH filter kernel heightExample nKerW*nKerH = 3*3 or 7*5

(a) (b) (c)

Figure 27: Examples of the ImgFltMean and ImgFltMedian functions. (a) Source image. (b) The imageafter filtered by ImgFltMean (3 by 3 kernel). (c) The image after filtered by ImgFltMedian (3 by 3kernel).

36

Page 37: Doc Imgp Roc Lib

3.21 ImgFltVariance

template <class T1, class T2> void ImgFltVariance

(const T1 *pSrc, int w1, int h1, const CRect<int> &rcROI1,

T2 *pDst, int w2, int h2, const CRect<int> &rcROI2,

int nKerW, int nKerH);

Description Variance filter, kernel certer equal to kernel varianceHalf-filter-length data at the ROI boundary are filtered with reduced sizedkernel.

Parameters See Table 2 for common parametersnKerW filter kernel widthnKerH filter kernel heightExample nKerW*nKerH = 3*3 or 7*5

(a) (b)

Figure 28: An example of the ImgFltVariance function. (a) Source image. (b) The image after filtered byImgFltVariance (3 by 3 kernel). Dark pixels indicate high variance; bright pixels indicate low variance.

3.22 ImgFltISEF

template <class T> void ImgFltISEF

(T *pImg, int w, int h, const CRect<int> &rcROI,

double dStrength);

Description Infinite symmetric exponential filterParameters See Table 2 for common parametersdStrength ISEF filter strength [0, 1) (larger, smoother)

(a) (b) (c)

Figure 29: Examples of the ImgFltISEF function. (a) Source image. (b) The image after filtered by 0.5filter strength. (c) The image after filtered by 0.8 filter strength.

3.23 ImgConvX

template <class T1, class T2> void ImgConvX

(T1 *pImg, int w, int h, const CRect<int> &rcROI,

const T2 *pMask, int nLen);

37

Page 38: Doc Imgp Roc Lib

Description 1D convolution in x directionConvolution is applied on each row (x direction). Half-filter-length data atthe ROI boundary are unchanged. The mask data array size must satisfynLen+(nLen-1)/2 ≤ ROI.Width(); otherwise nothing is done.

Parameters See Table 2 for common parameterspMask pointer to the convolution mask data arraynLen mask array size

3.24 ImgConvY

template <class T1, class T2> void ImgConvY

(T1 *pImg, int w, int h, const CRect<int> &rcROI,

const T2 *pMask, int nLen);

Description 1D convolution in y directionConvolution is applied on each column (y direction). Half-filter-length dataat the ROI boundary are unchanged. The mask data array size must satisfynLen+(nLen-1)/2 ≤ ROI.Height(); otherwise nothing is done.

Parameters See Table 2 for common parameterspMask pointer to the convolution mask data arraynLen mask array size

(a) (b) (c)

Figure 30: Examples of the ImgConvX and ImgConvY functions. (a) Source image. (b) The resultantimage processed by ImgConvX. (c) The resultant image processed by ImgConvY. The convolution maskimage is pMask[5] = {0.2, 0.2, 0.2, 0.2, 0.2}.

3.25 ImgConv

template <class T1, class T2> void ImgConv

(const T1 *pSrc, int w1, int h1, const CRect<int> &rcROI1,

T1 *pDst, int w2, int h2, const CRect<int> &rcROI2,

const T2 *pMask, int nKerW, int nKerH);

Description 2D convolutionHalf-filter-length data at the ROI boundary are unchanged.

Parameters See Table 2 for common parameterspMask pointer to the convolution mask imagenKerW mask image widthnKerH mask image heightExample 1 Sobel convolution mask

pMask[0] = -1, pMask[1] = 0, pMask[2] = 1pMask[3] = -2, pMask[4] = 0, pMask[5] = 2pMask[6] = -1, pMask[7] = 0, pMask[8] = 1

Example 2 Sharpening convolution maskpMask[0] = -0.2, pMask[1] = -0.5, pMask[2] = -0.2pMask[3] = -0.5, pMask[4] = +3.8, pMask[5] = -0.5

continued on next page

38

Page 39: Doc Imgp Roc Lib

pMask[6] = -0.2, pMask[1] = -0.5, pMask[2] = -0.2

(a) (b) (c)

Figure 31: Examples of the ImgConv function. (a) Source image. (b) Image produced by Sobel convo-lution mask. Dark pixels indicate high values; bright pixels indicate low values. (c) Image produced bythe sharpening convolution mask.

3.26 ImgResize

template <class T1, class T2> void ImgResize

(const T1 *pSrc, int w1, int h1, const CRect<int> &rcROI1,

T2 *pDst, int w2, int h2, const CRect<int> &rcROI2,

int nAlgo);

Description Resize ROI1 of Src and copy to ROI2 of DstIf linear (nAlgo: 1) or spline (nAlgo: 2–4) is used, minification is alwaysa downsampling process based on averaging. Magnification is done by thespecified method. If nearest neighbour (nAlgo: 0) is used, both minificationand magnification are based on nearest neighbour.

Parameters See Table 2 for common parametersnAlgo interpolation algorithm

0: nearest neighbour interpolation,1: linear interpolation,2: cubic B-spline interpolation.3: Catmull-Rom spline interpolation.4: natural cubic spline interpolation.

ROI1

ROI2

(a) (b) (c)

Figure 32: An example of the ImgResize function. (a) Source image. (b) Destination image. (c) ResizedROI1 is copied to ROI2.

3.27 ImgRotate

template <class T1, class T2> void ImgRotate

(const T1 *pSrc, int w1, int h1, const CRect<int> &rcROI1,

T2 *pDst, int w2, int h2, const CRect<int> &rcROI2,

int nAngle, bool bLinear);

39

Page 40: Doc Imgp Roc Lib

Pixel

0 10 20 30 40 50 60 70

Inte

nsity

100

200Source pixels

Nearest neighbour

Linear interpolation

Cubic B-spline

Catmull-Rom spline

Natural cubic spline

Figure 33: An example of different algorithms of the ImgResize function.

Description Rotate ROI1 of Src and copy to ROI2 of Dst1 Rotation center is the center of ROI1. When copied to ROI2, the rotation

center is matched with the center of ROI2.2 If nAngle is not a multiple of 9000 (90 deg). The rotated points in ROI1

that do not fall in ROI2 are ignored. Those points in ROI2 that do not havecounterpart in ROI1 are unchanged. Hence, there is no prerequisite on thesize of ROI1 and ROI2.

3 If nAngle is a multiple of 9000 (90 deg), the size of ROI1 and ROI2 must berelated accordingly. If the rotation angle is 0 or 180 deg, the size of ROI1and ROI2 must be the same. If the rotation angle is 90 or 270 deg, thewidth and height of ROI1 and ROI2 must be exchanged.

Parameters See Table 2 for common parametersnAngle anti-clock wise angle of rotation in 0.01 degreebLinear true for linear interpolation,

false for nearest neighbour interpolation.

ROI1

ROI2

(a) (b) (c)

Figure 34: An example of the ImgRotate function. (a) Source image. (b) Destination image. (c) RotatedROI1 is copied to ROI2.

3.28 ImgFlip

template <class T> void ImgFlip

(T *pImg, int w, int h, const CRect<int> &rcROI,

bool bLeftRight);

Description Flip the image data in an ROIParameters See Table 2 for common parametersbLeftRight true: left-right flip, false: top-bottom flip.

3.29 ImgFan

template <class T1, class T2> void ImgFan

(const T1 *pSrc, int w1, int h1, const CRect<int> &rcROI1,

40

Page 41: Doc Imgp Roc Lib

(a) (b) (c)

Figure 35: An example of the ImgFlip function. (a) Source image. (b) Image is flipped left to right. (c)Image is flipped top to bottom.

T2 *pDst, int w2, int h2, const CRect<int> &rcROI2,

double dAngle, double dTopRadius, double dBottomRadius, bool bLinear);

Description Create a fan-shaped imageEmpty region around the fan shape is unchanged.

Parameters See Table 2 for common parametersdAngle angle in radius of the fan shape (0, TWOPI]dTopRadius radius of the top line in the source ROIdBottomRadius radius of the bottom line in the source ROIbLinear true for linear interpolation,

false for nearest neighbour interpolation.

(a) (b)

Figure 36: An example of the ImgFan function. (a) Source image. (b) Fan-shaped image.

3.30 ImgRadial

template <class T1, class T2> void ImgRadial

(const T1 *pSrc, int w1, int h1, const CRect<int> &rcROI1,

T2 *pDst, int w2, int h2, const CRect<int> &rcROI2,

const CPt2D<double> &ptCen, double dK);

Description Create a radially distorted imageROI1 is radially distorted around ptCen based on equationx2 = ptCen.x+ (1 +KR2

1)(x1 − ptCen.x)y2 = ptCen.y + (1 +KR2

1)(y1 − ptCen.y), where(x1, y1) and (x2, y2) is a pixel in ROI1 and ROI2 respectively,R2

1 = (x1 − ptCen.x)2 + (y1 − ptCen.y)2.The distorted data are copied to ROI2. Pixels in ROI2 that do not have acounterpart in ROI1 are set to 0. Linear interpolation is used.

Parameters See Table 2 for common parametersptCen center of radial distortion in CRect(0,0,w1,h1) coordinatedK radial distortion coefficient

41

Page 42: Doc Imgp Roc Lib

(a) (b) (c)

Figure 37: An example of the ImgRadial function. (a) Source image. (b) Barrel distortion. (c) Pincushiondistortion.

3.31 ImgMosaic

template <class T> void ImgMosaic

(T *pImg, int w, int h, const CRect<int> &rcROI,

int nKerW, int nKerH);

Description Create mosaic effectParameters See Table 2 for common parametersnKerW mosaic kernel widthnKerH mosaic kernel height

ROI

(a) (b)

Figure 38: An example of the ImgMosaic function. (a) Source image. (b) The image data in the ROI aremosaicked.

3.32 ImgMatch

template <class T> void ImgMatch

(const T *pMother, int w1, int h1, const CRect<int> &rcROI1,

const T *pChild, int w2, int h2, const CRect<int> &rcROI2,

int nDSX, int nDSY, CPt2D<int> *pptMatch, double *pdCorr);

Description Search for ROI2 of pChild in ROI1 of pMotherROI1.Width() must be >= ROI2.Width()ROI1.Height() must be >= ROI2.Height()

1 Initially, ROI2 is matched to windows in ROI1 separated by nDSX andnDSY in the x and y directions. After the best-match window is found;nDSX and nDSY are halved.

2 Then ROI2 is matched to 9 windows, a 3 by 3 grid centered on the best-match window. The windows are separated by nDSX and nDSY. After anew best-match window is found, nDSX and nDSY are further halved.

3 Go back to 2 until both nDSX and nDSY equal to 1 pixel.Parameters See Table 2 for common parametersnDSX initial downsampling gap in x direction

continued on next page

42

Page 43: Doc Imgp Roc Lib

nDSY initial downsampling gap in y directionThe downsampling gaps are used in shifting ROI1 across ROI2 and in ac-cessing the pixels in ROI1 and ROI2,

pptMatch the best-match point in ROI1 coordinate (left-top corner of the matchedrect)

pdCorr normalized correlation coefficient, in [-1,1]

ROI1

ROI2

pptMatch

(a) (b)

Figure 39: An example of the ImgMatch function. (a) Mother image. (b) Child image. (c) Matchedposition of the child image in the mother image.

3.33 ImgMatchSubpixel

template <class T> void ImgMatchSubpixel

(const T *pMother, int w1, int h1, const CRect<int> &rcROI1,

const T *pChild, int w2, int h2, const CRect<int> &rcROI2,

int nLevel, CPt2D<float> *pptMatch, double *pdCorr);

Description Image matching at subpixel accuracySubpixel search for ROI2 of pChild in ROI1 of pMother. The matchingwindow is given by ROI2. ROI1 specifies the limit of the search region.Data matching is performed at several levels of increasing subpixel accruacy.At each level, 3 by 3 windows are searched. The best match is used as thecenter window of the next level search,

Parameters See Table 2 for common parametersnLevel subpixel level (1: 0.5 pixel, 2: 0.25 pixel, etc)pptMatch At input, it represents the initial offset (eg. matching point at pixel accu-

racy). At output, it is the best match (left-top corner) of ROI2 in ROI1 atsubpixel accuracy.

pdCorr normalized correlation coefficient, in [-1,1]

3.34 ImgMotion

template <class T> void ImgMotion

(const T *pImg1, int w1, int h1, const CRect<int> &rcROI1,

const T *pImg2, int w2, int h2, const CRect<int> &rcROI2,

int nCoarW, int nCoarH, int nCoarX, int nCoarY, int nCoarDSX, int nCoarDSY,

int nFineW, int nFineH, int nFineX, int nFineY, int nFineDSX, int nFineDSY,

int &nWinX, int &nWinY, vector<CPt2D<int> > *pvptCenter,

vector<CPt2D<int> > *pvptDisp, vector<double> *pvCorr);

Description Pixel flow or motion estimationEstimate the pixel motion from ROI1 in pImg1 to ROI2 in pImg2. ROI1and ROI2 must be the same size.

1 There are two levels of search: a coarse search based on a few coarse windowsand a quality-guided fine search on all fine windows. Results of the coarsesearch are used as the initial guess for the fine search.

continued on next page

43

Page 44: Doc Imgp Roc Lib

2 A large coarse search window and search distance may be used to obtainrobust initial estimates, and a small fine search window and search distanceto obtain a high resolution. The downsampling gap at each level is setindependently. See ImgMatch(...) for details of the downsampling gap.

3 Results are stored in pvptCenter, pvptDisp, pvCorr, which are arrays of thesame size: nWinX * nWinY (at output).

Parameters See Table 2 for common parametersnCoarW coarse search window widthnCoarH coarse search window heightnCoarX coarse search distance in x directionnCoarY coarse search distance in y directionnCoarDSX coarse search downsampling gap in x directionnCoarDSY coarse search downsampling gap in y directionnFineW fine search window widthnFineH fine search window heightnFineX fine search distance in x directionnFineY fine search distance in y directionnFineDSX fine search downsampling gap in x directionnFineDSY fine search downsampling gap in y directionnWinX input desired number of fine search windows in x direction

output actual number of fine search windows in x directionnWinY input desired number of fine search windows in y direction

output actual number of fine search windows in y directionpvptCenter center of fine search windows in pImg1pvptDisp displacement of fine windows from pImg1 to pImg2

This includes the global shift from ROI1 to ROI2.pvCorr correlation coefficient of each fine window

Pixel

Fine window

Coarse window

Figure 40: Illustration of the dual level search of the ImgMotion function.

3.35 ImgDIC

template <class T> void ImgDIC

(const T *pImg1, int w1, int h1, const CRect<int> &rcROI1,

const T *pImg2, int w2, int h2, const CRect<int> &rcROI2,

int nSpeckleSize, int &nWinX, int &nWinY, vector<CPt2D<int> > *pvptCenter,

vector<float> *pvDispX, vector<float> *pvDispY, vector<float> *pvCorr);

44

Page 45: Doc Imgp Roc Lib

(a) (b)

Figure 41: An example of the ImgMotion function. (a) Image 1. (b) Image 2. (c) Local motion vectorsfrom image 1 to image 2.

Description Digital image correlationCalculate subpixel displacement from ROI1 to ROI2. It relies on ImgMo-tion(..) to calculate integer pixel displacement and then applies linear inter-polation to obtain subpixel accuracy. Search parameters of ImgMotion(..)are estimated by the ROI size.

Parameters See Table 2 for common parametersnSpeckleSize average speckle size. It is used to estimate the search window size.nWinX input desired number of fine search windows in x direction

output actual number of fine search windows in x directionnWinY input desired number of fine search windows in y direction

output actual number of fine search windows in y directionpvptCenter center of fine search windows in pImg1pvDispX displacement in the x directionpvDispY displacement in the y directionpvCorr the correlation coefficient of each window

3.36 ImgHistogram

template <class T> void ImgHistogram

(const T *pImg, int w, int h, const CRect<int> &rcROI,

T tMin, T tMax, int *pnHisto, int nLevel);

Description Get histogram of the image data in an ROIParameters See Table 2 for common parameterstMin minimum bound. Pixles < tMin contribute to pnHisto[0].tMax maximum bound. Pixels > tMax contribute to pnHisto[nLevel-1].pnHisto store histogram in nLevel entries. Each entry records the number of pixels

falling into a specific range. Its size must be nLevel.nLevel level of histogram. The larger the nLevel, the finer is the resolution of the

histogram.

ROI

Intensity

Pe

rce

nta

ge

%

0 20 40 60 80 100 120 140 160 180 200 2200

0.5

1

1.5

(a) (b)

Figure 42: An example of the ImgHistogram function. (a) Source image. (b) Histogram of the ROI.

45

Page 46: Doc Imgp Roc Lib

3.37 ImgHistoEqu

template <class T> void ImgHistoEqu

(T *pImg, int w, int h, const CRect<int> &rcROI,

T tMin, T tMax, int nLevel);

Description Equalize histogram of the image data in an ROIParameters See Table 2 for common parameterstMin minimum bound. Pixles < tMin contribute to pnHisto[0].tMax maximum bound. Pixels > tMax contribute to pnHisto[nLevel-1].nLevel level of histogram. The larger the nLevel, the finer is the resolution of the

histogram equalization.

ROI

IntensityP

erc

en

tag

e %

0 50 100 150 200 2500

0.2

0.4

0.6

0.8

1

1.2

1.4

(a) (b)

Intensity

Pe

rce

nta

ge

%

0 50 100 150 200 2500

0.2

0.4

0.6

0.8

1

1.2

1.4

(c) (d)

Figure 43: An example of the ImgHistoEqu function. (a) Source image ROI. (b) Histogram of the sourceimage ROI. (c) Histogram-equalized ROI. (d) Histogram of the equalized ROI.

3.38 ImgClampExtreme

template <class T> void ImgClampExtreme

(T *pImg, int w, int h, const CRect<int> &rcROI,

double dPerMin, double dPerMax, T *pMin = 0, T *pMax = 0);

Description Clamp extreme values in an ROIThe biggest value of the bottom “perMin” percent data is set as the mini-mum bound. The smallest value of the top “perMax” percent is set as themaximum bound. Image data that fall below the minimum bound or abovethe maximum are clamped to the bounds, respectively.

Parameters See Table 2 for common parametersdPerMin percentage of minimum to be clamped. Should be in [0, 1].dPerMax percentage of maximum to be clamped. Should be in [0, 1].pMin the minimum value of the ROI after processing (input 0 to ignore)pMax the maximum value of the ROI after processing (input 0 to ignore)

3.39 ImgThre TwoPeak

template <class T> T ImgThre_TwoPeak

(const T *pImg, int w, int h, const CRect<int> &rcROI);

46

Page 47: Doc Imgp Roc Lib

Description Get threshold of an ROI based on the two peaks methodFirst, histogram of the ROI is computed. Then two peaks are located. Thethreshold is the mid value of the two peak values. Locate the max peak isstraightforward. The 2nd peak is the max multiplication of the histogramvalues by the square of the distance from the first peak. This helps not tochoose a pseudo-peak near the max (1st) peak.

Parameters See Table 2 for common parametersReturn threshold of an ROI

3.40 ImgThre IterSel

template <class T> T ImgThre_IterSel

(const T *pImg, int w, int h, const CRect<int> &rcROI);

Description Get threshold of an ROI based on the iterative selection methodTake an initial guess of the threshold. Compute mean of all pixels belowthe threshold and the mean of all pixels above the threshold. The newthreshold is (Tb+Ta)/2. Continue this process until there is no change inthe threshold value.

Parameters See Table 2 for common parametersReturn threshold of an ROI

(a) (b) (c)

Figure 44: Examples of the ImgThre TwoPeak and ImgThre IterSel functions. (a) Source image. (b)Threshold obtained by the two-peak method. (c) Threshold obtained by the iterative selection method.

3.41 ImgDilation

template <class T> void ImgDilation

(const T *pSrc, int w1, int h1, const CRect<int> &rcROI1,

T *pDst, int w2, int h2, const CRect<int> &rcROI2,

const CPt2D<int> *ppt, int nCount);

Description Morphology dilationDilation is applied to intensity data rather than binary data. The inputrelative position array is used as structuring elements. A destination pixelis set to the max value (in ImgDilation) or min value (in ImgErosion) ofthe source pixels located by the relative positions. If a destnation pixel’sall relative positions are outside the source region, it is set to the sourceregion’s global min (in ImgDilation) or max (in ImgErosion) value.

Parameters See Table 2 for common parametersppt relative position array (size must be nCount)nCount the size of pptExample Dilate in 135 deg: ppt[0]=(-1,-1); [1]=(1,1);

3.42 ImgErosion

template <class T> void ImgErosion

(const T *pSrc, int w1, int h1, const CRect<int> &rcROI1,

T *pDst, int w2, int h2, const CRect<int> &rcROI2,

const CPt2D<int> *ppt, int nCount);

47

Page 48: Doc Imgp Roc Lib

Description Morphology erosionErosion is applied to intensity data rather than binary data. The inputrelative position array is used as structuring elements. A destination pixelis set to the max value (in ImgDilation) or min value (in ImgErosion) ofthe source pixels located by the relative positions. If a destnation pixel’sall relative positions are outside the source region, it is set to the sourceregion’s global min (in ImgDilation) or max (in ImgErosion) value.

Parameters See Table 2 for common parametersppt relative position array (size must be nCount)nCount the size of pptExample Dilate in 135 deg: ppt[0]=(-1,-1); [1]=(1,1);

(a) (b) (c)

Figure 45: Examples of the ImgDilation and ImgErosion functions. (a) Source image. (b) Image dilation.(c) Image erosion.

3.43 ImgProjX

template <class T> void ImgProjX

(const T *pImg, int w, int h, const CRect<int> &rcROI,

T *pProj);

Description Get x direction projection of an ROIParameters See Table 2 for common parameterspProj average of values along x direction. Its size must be >= ROI.Height(). Its

index increases with y.Example 3, 5, 234, 90 | pvDst: 336/4

7, 23, 34, 29 | 93/4

12, 21, 9, 32 | 74/4

3.44 ImgProjY

template <class T> void ImgProjY

(const T *pImg, int w, int h, const CRect<int> &rcROI,

T *pProj);

Description Get y direction projection of an ROIParameters See Table 2 for common parameterspProj average of values along y direction. Its size must be >= ROI.Width(). Its

index increases with x.Example 3, 5, 234, 90

7, 23, 34, 29

12, 21, 9, 32

------------------------

pvDst: 22/3, 49/3, 277/3, 151/3

3.45 ImgProjAny

template <class T> void ImgProjAny

(const T *pImg, int w, int h, const CRect<int> &rcROI,

48

Page 49: Doc Imgp Roc Lib

ROI

Pixel

Inte

nsity

0 200 400 600 800 1000 120070

90

110

130

150

Pixel

Inte

nsity

0 200 400 600 800 1000 1200 1400 160020

70

120

170

(a) (b) (c)

Figure 46: Examples of the ImgProjX and ImgProjY functions. (a) Source image. (b) Intensity projectionof the ROI in the x direction. (c) Intensity projection of the ROI in the y direction.

const CLine2D<int> &lnStart, const CLine2D<int> &lnEnd,

T *pProj, int nLine);

Description Get the projection of an arbitrary-shaped regionThe arbitrary-shaped region is specified by 2 lines. The projection sweepsfrom lnStart to lnEnd with “nLine” projection lines. If a line is outside theROI, its projection value will be set to 0.

Parameters See Table 2 for common parameterslnStart the start projection linelnEnd the end projection line (lnStart and lnEnd may intersect)pProj store average value of each projection line. Its size must be nLine.nLine the number of projection lines

ptSta

rt ptEnd

Pixel

Inte

nsity

0 20 40 60 80 10040

60

80

100

120

140

160

180

(a) (b)

Figure 47: An example of the ImgProjAny function. (a) Source image with start and end projectionlines. (b) Intensity projection of the ROI.

3.46 ImgRadon

template <class T1, class T2> void ImgRadon

(const T1 *pSrc, int w1, int h1, const CRect<int> &rcROI1,

T2 *pDst, int w2, int h2, const CRect<int> &rcROI2,

double dStartAngle, double dStepInc);

Description Radon transformRadon transform is the integral of an image over straight lines. Parallel ge-ometry is assumed. The projection width is ROI2.Width() and the numberof angles is ROI2.Height(). The projection at each angle is stored in a linein ROI2.

Parameters See Table 2 for common parametersdStartAngle the start angle of projectiondStepInc the angular increase at each step

3.47 ImgRadonVH

template <class T1, class T2, class Pred> void ImgRadonVH

(const T1 *pSrc, int w1, int h1, const CRect<int> &rcROI1,

49

Page 50: Doc Imgp Roc Lib

(a) (b) (c)

Figure 48: An example of the ImgRadon function. (a) Source image. (b) Radon transformed image. (c)Inverse Radon trasformed image by filtered backprojection.

T2 *pDst, int w2, int h2, const CRect<int> &rcROI2,

Pred pred, double dStartAngle, double dStepInc, T2 tObj);

Description Radon transform for visual hullThis function mimics non-penetrating light projection on objects. It is dif-ferent from X-ray penetration of objects, modeled in ImgRadon(..). Parallelgeometry is assumed. The projection width is ROI2.Width() and the num-ber of angles is ROI2.Height(). The projection at each angle is stored in aline in ROI2.

Parameters See Table 2 for common parameterspDst Pixels == 0 are definitely background, == tObj are definitely objects, in

between are edge points.pred if pred(pSrc[y*w+x]) is true, point (x,y) is considered an object point.dStartAngle the start angle of projectiondStepInc the angular increase at each steptObj value in pDst to indicate object pixels

3.48 ImgBackprojVH

template <class T1, class T2, class Pred> void ImgBackprojVH

(const T1 *pSrc, int w1, int h1, const CRect<int> &rcROI1,

T2 *pDst, int w2, int h2, const CRect<int> &rcROI2,

Pred pred, double *pdAngle, T2 tObj);

Description Backprojection for visual hullBackprojection for visual hull (VH) is an erosive process in contrast to theaccumulative process in the filtered backprojection for computed tomog-raphy (CT). At the start, all pixels in the backprojected field are set asobjects. A background projection datum sets the pixels in the backpro-jected line to be background while an object datum does not change thebackprojected field. Each line of ROI1 is the CP Radon transform projec-tion at an angle (given by pdAngle). The size of pdAngle should be thesame as ROI1.Height().

Parameters See Table 2 for common parameterspDst Pixels == 0 are definitely background, == tObj are definitely objects, in

between are edge points.pred if pred(pSrc[y*w+x]) is true, point (x,y) is considered an object point.pdAngle angle of each projection linetObj value in pDst to indicate object pixels

3.49 ImgIntegral

template <class T1, class T2> void ImgIntegral

(const T1 *pSrc, int w1, int h1, const CRect<int> &rcROI1,

T2 *pSum, int w2, int h2, const CRect<int> &rcROI2);

template <class T1, class T2> void ImgIntegral

(const T1 *pSrc, int w1, int h1, const CRect<int> &rcROI1,

50

Page 51: Doc Imgp Roc Lib

T2 *pSum, int w2, int h2, const CRect<int> &rcROI2,

T2 *pSum2, int w3, int h3, const CRect<int> &rcROI3);

Description Calculate two integral imagesThe first version calculates the summation and the second version calculatesboth the summation and the summation of square. Each pixel in pSum(pSum2) is the summation (summation of square) of all pSrc’s pixels in theleft-top region with respect to and including that pixel.

Parameters See Table 2 for common parameters

3.50 ImgLabeling

template <class T1, class T2, class Pred> int ImgLabeling

(const T1 *pSrc, int w1, int h1, const CRect<int> &rcROI1,

T2 *pDst, int w2, int h2, const CRect<int> &rcROI2,

Pred pred, int nConnectivity, int nMinArea = 0, int nMaxArea = 0,

vector<CRect<int> > *pvRect = 0, vector<int> *pvArea = 0);

Description Connectivity labeling algorithmParameters See Table 2 for common parameterspred if pred(pSrc[y*w+x]) is true, the point (x,y) is considered an object point.nConnectivity Euclidean connectivity of an object.

0: 4-neighbour labeling algorithm,1: 8-neighbour labeling algorithm,>1: pixels within this distance are one object.

nMinArea Object fewer than nMinArea pixels are discarded.nMaxArea Object larger than nMaxArea pixels are discarded.pvRect bounding rectangle of each object (input 0 to ignore)

It is with respect to ROI2. To obtain a bounding rect with respect to ROI1,each rect should be offset r1.LeftTop()-r2.LeftTop().

pvArea number of points of each object (input 0 to ignore)Return the number of objects foundExample The 1st object can be found by checking if(pDst[y*w+x]==1).

The 1st object’s bounding rect is (*pvRect)[0].The 3rd object’s number of points is (*pvArea)[2].

(a) (b) (c)

Figure 49: An example of the ImgLabeling function. (a) Source image. Dark pixels are to be labeled.(b) Labeled image. Each color represents an object and the background is white. (c) Labeled image withnMinArea = 150, Objects fewer than 150 pixels are discarded.

3.51 ImgFilling

template <class T, class Pred> void ImgFilling

(T *pImg, int w, int h, const CRect<int> &rcROI, T value,

Pred pred, CPt2D<int> ptSeed, int nConnectivity);

51

Page 52: Doc Imgp Roc Lib

Description Fill a region bounded by a predicate conditionParameters See Table 2 for common parametersvalue value to fill withpred if pred(pImg[y*w+x],value) is true, the point (x,y) is considered boundary.

Only three predicates are valid: equal to, greater equal, less equal. Otherpredicates such as greater or less may cause dead loop, so they should bestrictly prohibited.

ptSeed initial seed point for fillingnConnectivity Euclidean connectivity of a filling region.

0: 4-neighbour filling algorithm,1: 8-neighbour filling algorithm,>1: pixels within this distance are filled.

Seed

(a) (b)

Figure 50: An example of the ImgFilling function. (a) Source image. The point is the seed of filling.(b) Filled image. Filling value: 200. pred (boundary condition): less equal. Connectivity: 4-neighbourfilling algorithm.

3.52 ImgThinning

template <class T> void ImgThinning

(T *pImg, int w, int h, const CRect<int> &rcROI,

T ObjID, T NonObjID);

Description Thinning algorithmParameters See Table 2 for common parametersNonObjID Pixels with value==NonObjID are treated as background.

(a) (b)

Figure 51: An example of the ImgThinning function. (a) Source image. (b) Thinned image.

3.53 ImgCentroid

template <class T> bool ImgCentroid

(const T *pImg, int w, int h, const CRect<int> &rcROI,

T ObjID, double *pdXCen, double *pdYCen);

52

Page 53: Doc Imgp Roc Lib

Description Get the centroid of an objectParameters See Table 2 for common parameterspdXCen x coordinate of the center, if found.pdYCen y coordinate of the center, if found.Return true: the centroid of an object is found; otherwise, false.

3.54 ImgBoundaryOrdered

template <class T> int ImgBoundaryOrdered

(const T *pImg, int w, int h, const CRect<int> &rcROI,

T ObjID, vector<CPt2D<int> > *pvPos);

Description Get clockwise ordered boundary points, based on 8-neighbour connectivity.Holes are not detected as boundary.

Parameters See Table 2 for common parameterspvPos a vector pointer that stores the found boundary pointsReturn the number of boundary points

3.55 ImgBoundaryUnordered

template <class T> int ImgBoundaryUnordered

(const T *pImg, int w, int h, const CRect<int> &rcROI,

T ObjID, vector<CPt2D<int> > *pvPos);

Description Get unordered boundary points, based on 8-neighbour connectivity. Holesare detected as boundary.

Parameters See Table 2 for common parameterspvPos a vector pointer that stores the found boundary pointsReturn the number of boundary points

(a) (b) (c)

Figure 52: Examples of the ImgBoundaryOrdered and ImgBoundaryUnordered functions. (a) Sourceimage. (b) Ordered boundary. Holes are ignored. (c) Unordered boundary. Holes are detected.

3.56 ImgOuterCorners

template <class T> int ImgOuterCorners

(const T *pImg, int w, int h, const CRect<int> &rcROI,

T ObjID, vector<CPt2D<int> > *pvPos);

Description Get the outer corner points of an objectOuter corner points are used to calculate the convex hull.

Parameters See Table 2 for common parameterspvPos stores the outer corner points, in clockwise order.Return the number of outer corner points

53

Page 54: Doc Imgp Roc Lib

(a) (b)

Figure 53: An example of the ImgOuterCorners function. (a) Source image. Dark pixels are consideredas objects for detecting corner points. (b) Corner points of each object. Note that inner corners of a holeare not treated as corner points.

3.57 ImgConvexHull

template <class T> int ImgConvexHull

(const T *pImg, int w, int h, const CRect<int> &rcROI,

T ObjID, vector<CPt2D<int> > *pvCH);

Description Get the convex hull points in clockwise orderParameters See Table 2 for common parameterspvCH output convex hull points (in clockwise order)Return the number of convex hull points

(a) (b)

Figure 54: An example of the ImgConvexHull function. (a) Source image. Dark pixels are considered asobjects for detecting convex hulls. (b) Convex hulls of each object.

3.58 ImgBoundingBox

template <class T> bool ImgBoundingBox

(const T *pImg, int w, int h, const CRect<int> &rcROI, T ObjID,

double dAngle, CPt2D<double> *pptCenter, CSize2D<double> *pSize);

Description Get the fixed-angle bounding box of an objectParameters See Table 2 for common parametersdAngle angle of the bounding box (radius, anticlockwise)pptCenter center of the boxpSize size of the boxReturn true: succeeded; false: failed (no object points).

3.59 ImgMinBoundingBox

template <class T> bool ImgMinBoundingBox

(const T *pImg, int w, int h, const CRect<int> &rcROI, T ObjID,

CPt2D<double> *pptCenter, CSize2D<double> *pSize, double *pdAngle);

Description Get the minimum bounding box of an objectParameters See Table 2 for common parameterspptCenter center of the boxpSize size of the boxpdAngle angle of the box (radius, anticlockwise)Return true: succeeded; false: failed (no object points).

54

Page 55: Doc Imgp Roc Lib

3.60 ImgMinBoundingCircle

template <class T> bool ImgMinBoundingCircle

(const T *pImg, int w, int h, const CRect<int> &rcROI,

T ObjID, CPt2D<double> *pptCenter, double *pdRadius);

Description Get the minimum bounding circle of an objectParameters See Table 2 for common parameterspptCenter center of the circlepdRadius radius of the circleReturn true: succeeded; false: failed (no object points).

3.61 ImgMinBoundingPolygon

template <class T> bool ImgMinBoundingPolygon

(const T *pImg, int w, int h, const CRect<int> &rcROI,

T ObjID, vector<CPt2D<int> > *pvPos);

Description Get the minimum bounding polygon of an objectSuch polygon differs from a complete list of boundary points in that itremoves points on any straight line parallel to X or Y axis. Only end pointsof a line are kept as the polygon vertices.

Parameters See Table 2 for common parameterspvPos stores the polygon vertices, in clockwise order.Return the number of polygon vertices

(a) (b) (c) (d)

Figure 55: Examples of the ImgBoundingBox, ImgMinBoundingBox, ImgMinBoundingCircle, and Img-MinBoundingPolygon functions. (a) Bounding box at a fixed angle of 27 deg. (b) Minimum area boundingbox. (c) Minimum area bounding circle. (d) Minimum area bounding polygon.

3.62 ImgDistTrans

template <class T> void ImgDistTrans

(const T *pSrc, int w1, int h1, const CRect<int> &rcROI1,

unsigned long *pnDst, int w2, int h2, const CRect<int> &rcROI2,

T threshold);

Description Distance transform algorithmReference ”Euclidean Distance Mapping” by P. Danielsson.

Parameters See Table 2 for common parameterspnDst a background pixel is set to 0; an object pixel is set to the square of the

Euclidean distance to its nearest background pixel.threshold pixels >= threshold in pSrc are objects;

pixels < threshold in pSrc are background.

3.63 ImgWatershed

template <class T1, class T2> int ImgWatershed

(const T1 *pSrc, int w1, int h1, const CRect<int> &rcROI1,

T2 *pDst, int w2, int h2, const CRect<int> &rcROI2,

int nDist, int nMinPts);

55

Page 56: Doc Imgp Roc Lib

Description Watershed algorithm for segmentationThis function assumes the input pSrc a distance map, for example, obtainedfrom ImgDistTrans(...). It outputs an object map similar to that producedby ImgLabeling(...). In the object map, previously connected regions aresegmented.

1 Local maxima are located for each object.2 The first-pass propagation estimates the number of points grown by each

maxima within each object.3 Maxima whose number of points are < nMinPts are discarded.4 The second-pass propagation finishes the segmentation.Parameters See Table 2 for common parametersnDist the distance for merging local maxima.

0: 4-neighbour merging,1: 8-neighbour merging,>1: local maxima within this distance are merged.

nMinPts threshold of the number of points grown by a maximum.Both parameters are used to reduce over-segmentation.

Return The number of segmented objects, if succeeded; otherwise -1.

255

0

128

(a) (b) (c)

Figure 56: Examples of the ImgDistTrans and ImgWatershed functions. (a) Source image. (b) Distancetransformed image. (c) Watershed segmented image.

3.64 ImgEdgeX

template <class T> int ImgEdgeX

(T *pImg, int w, int h, const CRect<int> &rcROI,

T tThre, int nEdgeType, int nHalfFltLen, int nIndex,

int nDSY, vector<CPt2D<int> > *pvptEdge);

Description 1D edge detection in x direction (description applicable to y direction)1D edge detection based on the contrast of image data along x or y direction.The scan of edge point in x direction is left-to-right; the scan in y directionis top-to-bottom.

1 At each point, nHalfFltLen specifies how many data to check to the left andright (x direction) or to the top and bottom (y direction) of a point. Theaverage intensity of the two sections are calculated. If their difference is >=tThre, the center point is considered as an edge point. nHalfFltLen-1 pixelsboundary of the ROI is not scanned.

2 There are two types of edges: positive and negative. In the direction ofscan (x direction: left-to-right, y direction: top-to-bottom), a positive edgeindicates the data values are increasing; while a negative edge indicatesdecreasing.

3 One or multiple edge points at each line can be output to vptEdge. IfnIndex is non-zero, one edge point is selected. For example, 1 means thefirst edge point, 2: the second, etc; -1 means the last, -2: the second last,etc. If nIndex is 0, all edge points are output.

continued on next page

56

Page 57: Doc Imgp Roc Lib

4 If two edge points of the same type are found within 2*nHalfFltLen pixels,only the one with larger contrast is output because they are likely to bepart of a steep edge.

Parameters See Table 2 for common parameterstThre edge contrast thresholdnEdgeType edge type (-1: negative, 1: positive, 0: both)nHalfFltLen half-filter-length at each side of a pointnIndex 1-based index of the edge point to be outputnDSY downsampling gap in y direction (eg. If 1, each line, or if 2, every other line

is scanned.)pvptEdge store output edge points. Some line may not have any edge point.Return the number of edge points found

3.65 ImgEdgeY

template <class T> int ImgEdgeY

(T *pImg, int w, int h, const CRect<int> &rcROI,

T tThre, int nEdgeType, int nHalfFltLen, int nIndex,

int nDSX, vector<CPt2D<int> > *pvptEdge);

Description 1D edge detection in y direction (see ImgEdgeX for details)Parameters See Table 2 for common parametersnDSX downsampling gap in x direction (eg. If 1, each row, or if 2, every other row

is scanned.)

3.66 ImgEdgeXSubpixel

template <class T> int ImgEdgeXSubpixel

(T *pImg, int w, int h, const CRect<int> &rcROI,

T tThre, int nEdgeType, int nHalfFltLen, int nIndex,

int nDSY, vector<CPt2D<double> > *pvptEdge);

Description 1D edge detection in x direction at subpixel accuracyAll the parameters and the return value are the same as those of ImgEdgeXexcept for pvptEdge, whose x coord is at subpixel accuracy and y coord isstill in integer pixel.

3.67 ImgEdgeYSubpixel

template <class T> int ImgEdgeYSubpixel

(T *pImg, int w, int h, const CRect<int> &rcROI,

T tThre, int nEdgeType, int nHalfFltLen, int nIndex,

int nDSX, vector<CPt2D<double> > *pvptEdge);

Description 1D edge detection in y direction at subpixel accuracyAll the parameters and the return value are the same as those of ImgEdgeYexcept for pvptEdge, whose y coord is at subpixel accuracy and x coord isstill in integer pixel.

3.68 ImgEdgeAny

template <class T> int ImgEdgeAny

(T *pImg, int w, int h, const CRect<int> &rcROI,

const CLine2D<int> &lnGuide, T tThre, int nEdgeType, int nScanLen,

int nHalfFltLen, int nNumScan, vector<CPt2D<double> > *pvptEdge,

vector<CLine2D<int> > *pvlnScan = 0);

Description Detect 1D edge based on a guiding line1 Multiple scan lines are generated perpendicular to the guiding line. Their

direction is determined by lnGuide: lnGuide.Start() → lnGuide.End() →lnScan.Start() is anti-clockwise and lnGuide.Start() → lnGuide.End() →lnScan.End() is clockwise.

continued on next page

57

Page 58: Doc Imgp Roc Lib

2 In the direction of lnScan, a positive edge indicates increasing data values;while a negative edge indicates decreasing data values.

3 On each scan line, the maximum contrast (strongest) edge point is output,if its contrast is larger than tThre.

4 Edge contrast is the difference of the average intensity of the nHalfFltLenpixels before and after an edge point.

Parameters See Table 2 for common parameterslnGuide a guiding line for searching for edge pointstThre edge contrast thresholdnEdgeType edge type (-1: negative, 1: positive, 0: both)nScanLen scan length for an edge pointnHalfFltLen half-filter-length at each side of a pointnNumScan the number of scan lines on the guiding linepvptEdge store output edge points at subpixel accuracypvlnScan scan lines (input 0 to ignore)Return the number of edge points found

3.69 ImgEdgeOnLine

template <class T> int ImgEdgeOnLine

(T *pImg, int w, int h, const CRect<int> &rcROI,

const CLine2D<int> &lnScan, T tThre, int nEdgeType, int nHalfFltLen,

vector<CPt2D<int> > *pvptEdge);

Description Detect 1D edge on a lineScan from lnScan.Start() to lnScan.End() for edge points. In the directionof lnScan, a positive edge indicates the data values are increasing; while anegative edge indicates decreasing. All edge points with contrast >= tThreare output to pvptEdge but if two edge points of the same type are foundwithin 2*nHalfFltLen pixels, only the one with larger contrast is outputbecause they are likely to be part of a steep edge. Edge contrast is thedifference of the average intensity of the nHalfFltLen pixels before and afteran edge point.

Parameters See Table 2 for common parameterslnScan scan line for edge pointstThre edge contrast thresholdnEdgeType edge type (-1: negative, 1: positive, 0: both)nScanLen scan length for an edge pointnHalfFltLen half-filter-length at each side of a pointpvptEdge store output edge pointsReturn the number of edge points found

guiding linescan lines edge points scan lineedge points

(a) (b)

Figure 57: Examples of the ImgEdgeAny and ImgEdgeOnLine functions. (a) ImgEdgeAny and (b)ImgEdgeOnLine function.

58

Page 59: Doc Imgp Roc Lib

3.70 ImgCannyEdge

template <class T> void ImgCannyEdge

(T *pImg, int w, int h, const CRect<int> &rcROI,

T lowThre, T highThre, int nFltLen);

Description Canny edge detection algorithmThe edge magnitude (gradient in the x and y directions) at each pixel iscalculated and non-local-maximum pixels are set to 0. Then, a hystere-sis thresholding procedure is applied to extract the edge pixels. Pixels ≥lowThre and connected to any pixel ≥ highThre are thresholded as edge(set to 1). Those that are < lowThre or < highThre but not connected toany pixel > highThre are set to 0.

Parameters See Table 2 for common parameterslowThre low threshold for hysteresis thresholdinghighThre high threshold for hysteresis thresholdingnFltLen Gaussian filter length (≥3)

If highThre > lowThre, pImg stores edge (1) and non-edge (0).If highThre ≤ lowThre, pImg stores edge magnitude.

3.71 ImgShenCastanEdge

template <class T> void ImgShenCastanEdge

(T *pImg, int w, int h, const CRect<int> &rcROI,

T lowThre, T highThre, double dStrength, int nWinSize);

Description Shen and Castan edge detection algorithmParameters See Table 2 for common parameterslowThre low threshold for hysteresis thresholdinghighThre high threshold for hysteresis thresholdingdStrength ISEF filter strength [0, 0.9]nWinSize window size for calculating the edge contrast

If highThre > lowThre, pImg stores edge (1) and non-edge (0).If highThre ≤ lowThre, pImg stores edge magnitude.

(a) (b) (c)

Figure 58: Examples of the ImgCannyEdge and ImgShenCastanEdge functions. (a) Source image. (b)Canny edge. (c) ShenCastan edge.

3.72 ImgFitPlane

template <class T> bool ImgFitPlane

(const T *pImg, int w, int h, const CRect<int> &rcROI,

double *pdKx, double *pdKy, double *pdZ0);

Description Fit a plane to image data of an ROIPlane equation z(x,y) = Kx*x + Ky*y + Z0

Parameters See Table 2 for common parameterspdKx Kx of the plane equation, if succeed; or undefined.pdKy Ky of the plane equation, if succeed; or undefined.pdZ0 Z0 of the plane equation, if succeed; or undefined.Return true: succeeded; false: failed.

59

Page 60: Doc Imgp Roc Lib

template <class T1, class T2> bool ImgFitPlane

(const T1 *pImg, int w1, int h1, const CRect<int> &rcROI1,

const T2 *pWei, int w2, int h2, const CRect<int> &rcROI2,

double *pdKx, double *pdKy, double *pdZ0);

Description Fit a plane to weighted image dataParameters See Table 2 for common parameterspWei weighting. (rcROI1, rcROI2 must be the same size)

3.73 ImgFitPolynomialSurf

template <class T> bool ImgFitPolynomialSurf

(const T *pImg, int w, int h, const CRect<int> &rcROI,

int N, vector<double> *pvCoe,

vector<int> *pvPowOfX, vector<int> *pvPowOfY);

Description Fit a polynomial surface to the image data in an ROIPolynomial surface equation: z(x,y) =+C0 + C1x+ C3x

2 + C6x3 + ...+ C?x

N

+C2y + C4xy + C7x2y + ...

+C5y2 + C8xy

2 + ...

+...+ ...+ ...

+C(N+1)(N+2)/2−1yN

Parameters See Table 2 for common parametersN the order of the surface equation (should be >= 0)pvCoe store coefficients, if succeed; or undefined.pvPowOfX store power of x of each term, if succeed; or undefined.pvPowOfY store power of y of each term, if succeed; or undefined.

After processing, the size of pvCoe, pvPowOfX and pvPowOfY will be(N+1)(N+2)/2 and their elements are ordered according to the index ofC in the equation.

Return true: succeeded; false: failed.

template <class T1, class T2> bool ImgFitPolynomialSurf

(const T1 *pImg, int w1, int h1, const CRect<int> &rcROI1,

const T2 *pWei, int w2, int h2, const CRect<int> &rcROI2,

int N, vector<double> *pvCoe,

vector<int> *pvPowOfX, vector<int> *pvPowOfY);

Description Weight version of ImgFitPolynomialSurfParameters See Table 2 for common parameterspWei weighting. (rcROI1, rcROI2 must be the same size)

3.74 ImgFitGauss

template <class T> bool ImgFitGauss

(const T *pImg, int w, int h, const CRect<int> &rcROI,

T threshold, double pdCoe[5]);

Description Fit a 2D Gaussian function to image data of an ROI

Gaussian function: z = Ae−(x−B)2/(2C)−(y−D)2/(2E). Fitting is based on alinear least squares method: the z coord of all points is converted to ln(z),which requires that the image data > 0.

Parameters See Table 2 for common parametersthreshold image data < threshold are not used for fitting. The threshold must be >

0 because of logarithm.pdCoe parameters of the Gaussian function

pdCoe[0], pdCoe[1], ... pdCoe[4] are A, B, ... E respectively.Return true: succeeded; false: failed.

60

Page 61: Doc Imgp Roc Lib

3.75 ImgFitImg

template <class T> bool ImgFitImg

(const T1 *pImg1, int w1, int h1, const CRect<int> &rcROI1,

const T2 *pImg2, int w2, int h2, const CRect<int> &rcROI2,

int N, vector<double> *pvCoe);

Description Fit a plane to image data of an ROILet pImg1 and pImg2 be x and y respectively, the fitting equation is y =C0+C1x+C2x

2+ ...+CNxN . The least squares method is applied to eachpair of pixels in pImg1 and pImg2 to find the coefficients.

Parameters See Table 2 for common parametersN the order of the polynomial equation (should be >= 0)pvCoe store coefficients, if succeed; or undefined.Return true: succeeded; false: failed.

3.76 ImgLineData

template <class T> int ImgLineData

(const T *pImg, int w, int h, const CRect<int> &rcROI,

const CPt2D<int> &ptStart, const CPt2D<int> &ptEnd,

vector<T> *pvDst, vector<CPt2D<int> > *pvPos = 0);

Description Get the image data falling on a lineA point on the line is round off to integer sample accuracy.

Parameters See Table 2 for common parametersptStart start point of the lineptEnd end point of the linepvDst store image data falling on the linepvPos store points on the line (input 0 to ignore)Return the number of pixels on the line

3.77 ImgLineSum

template <class T> int ImgLineSum

(const T *pImg, int w, int h, const CRect<int> &rcROI,

const CPt2D<int> &ptStart, const CPt2D<int> &ptEnd, double *pdSum);

Description Get the sum of a line’s dataA point on the line is kept with subsample accuracy and obtains its datumby bilinear interpolation.

Parameters See Table 2 for common parametersptStart start point of the lineptEnd end point of the linepdSum sum of the line’s dataReturn the number of pixels on the line

3.78 ImgContour

template <class T1, class T2> void ImgContour

(const T1 *pSrc, int w, int h, const CRect<int> &rcROI,

T2 *pDst, int nContourLine);

Description Get contour lines of the image data in an ROI1 Compute each contour line’s value.2 For each pixel, check which contour line is the nearest.3 Compare the pixel’s 4-way neighbours’ value. If the contour line’s value is

in between, then the pixel is a contour point.Parameters See Table 2 for common parameterspDst image of contour object. size must be w*h.

value = i represent the ith contour. background is 0.nContourLine number of contour lines to extract

61

Page 62: Doc Imgp Roc Lib

(a) (b)

Figure 59: An example of the ImgContour function. (a) Source image. (b) Contours of intensity. Eachgray scale represents a level of intensity.

3.79 ImgMask

template <class T1, class T2> void ImgMask

(const T1 *pMask, int w1, int h1, const CRect<int> &rcROI1,

T2 *pImg, int w2, int h2, const CRect<int> &rcROI2, T2 value);

Description Set image data based on a mask imagePixels of pImg are set to the mask value, if the corresponding pixels in themask image are <= 0. ROI1 and ROI2 must be the same size.

Parameters See Table 2 for common parametersvalue the mask value

3.80 ImgMaskPolygon

template <class T> void ImgMaskPolygon

(T *pImg, int w, int h, const CRect<int> &rcROI,

const CPt2D<int> *ppt, int nCount, T value, bool bMaskInner);

Description Mask a polygon regionThe function first creates an internal image mask by filling; then uses themask to remove inner or outer image data. This strategy only works onsimple polygons (no two non-adjacent edges intersect). If the ROI cuts thepolygon into separate regions, only one of these regions will be masked. Themain advantage of this function is speed. It is much faster than explicitlychecking if each pixel in the ROI is in or out of the polygon region.

Parameters See Table 2 for common parametersppt an array of polygon corners (size must be nCount)nCount the number of polygon cornersvalue the value to fill the polygon regionbMaskInner indicate if mask inner or outer of the polygon

(a) (b)

Figure 60: An example of the ImgMaskPolygon function. (a) Source image. (b) A polygon region ismasked with value 255.

62

Page 63: Doc Imgp Roc Lib

3.81 ImgMandelbrot

template <class T> int ImgMandelbrot

(T *pImg, int w, int h, const CRect<int> &rcROI,

double dX1, double dY1, double dX2, double dY2,

int nMaxIteration);

Description Create a Mandelbrot patternParameters See Table 2 for common parameterspImg output Mandelbrot image. Pixel value contains how many iterations used

to decide that the point is in or out of the Mandelbrot set.dX1 minimum bound of the Mandelbrot set in the x directiondY1 minimum bound of the Mandelbrot set in the y directiondX2 maximum bound of the Mandelbrot set in the x directiondY2 maximum bound of the Mandelbrot set in the y directionnMaxIteration maximum iterations to be appliedReturn the min iterations used among all points to reach a decision that whether a

point is in or out of the Mandelbrot set

ROIROI

(a) (b) (c)

Figure 61: An example of the ImgMandelbrot function. (a) Large scale Mandelbrot image. dX1 = -2.05,dY1 = -1.3, dX2 = 0.7, dY2 = 1.3. (b) Zoom into the ROI shown in (a). (c) Zoom into the ROI shownin (b). The colors of the images are created by applying an intensity-to-color map.

3.82 ImgPhaseShifting

template <class T1, class T2> void ImgPhaseShifting

(T1 **ppSrc, int w1, int h1, const CRect<int> &rcROI1,

T2 *pPhase, int w2, int h2, const CRect<int> &rcROI2,

const T2 *pPS, int nStep);

template <class T1, class T2> void ImgPhaseShifting

(T1 **ppSrc, int w1, int h1, const CRect<int> &rcROI1,

T2 *pPhase, int w2, int h2, const CRect<int> &rcROI2,

T2 *pBK, int w3, int h3, const CRect<int> &rcROI3,

T2 *pCon, int w4, int h4, const CRect<int> &rcROI4,

const T2 *pPS, int nStep);

Description General phase-shifting algorithmThe general phase-shifting algorithm takes an arbitrary number of (≥3)phase-shifted images to compute a wrapped phase image. The phase-shiftangle of each image may also be arbitrary. All input images share the sameROI. T1 can be any integer or float type. T2 must be float or double tostore the phase angles [−π, π].

Parameters See Table 2 for common parametersppSrc pointer to an array of images (array size = nStep)pPhase output wrapped phase imagepBK output background imagepCon output contrast imagepPS phase-shift value at each step (array size = nStep)nStep the number of phase-shifting steps (at least 3)

63

Page 64: Doc Imgp Roc Lib

(a) (b) (c)

(d) (e) (f)

Figure 62: An example of the ImgPhaseShifting function. (a) (b) (c) Three phase-shifted images. Al-though they look similar, the fringe pattern in each image shifts a constant phase angle. (d) Wrappedphase image. (e) Background intensity image. (f) Fringe contrast image.

3.83 ImgPhaseShifting Carre

template <class T1, class T2> T2 ImgPhaseShifting_Carre

(T1 *ppSrc[4], int w1, int h1, const CRect<int> &rcROI1,

T2 *pPhase, int w2, int h2, const CRect<int> &rcROI2);

template <class T1, class T2> void ImgPhaseShifting_Carre

(T1 **ppSrc[4], int w1, int h1, const CRect<int> &rcROI1,

T2 *pPhase, int w2, int h2, const CRect<int> &rcROI2,

T2 *pBK, int w3, int h3, const CRect<int> &rcROI3,

T2 *pCon, int w4, int h4, const CRect<int> &rcROI4);

Description Carre phase-shifting algorithmCarre algorithm takes four images with a constant unknown phase shift. Allinput images share the same ROI. T1 can be any integer or float type. T2must be float or double to store the phase angles [−π, π].

Parameters See Table 2 for common parametersppSrc pointer to an array of images (array size = 4)pPhase output wrapped phase imagepBK output background imagepCon output contrast imageReturn the estimated constant phase shift at each step

3.84 ImgPhaseShifting 5Frame

template <class T1, class T2> T2 ImgPhaseShifting_5Frame

(T1 *ppSrc[5], int w1, int h1, const CRect<int> &rcROI1,

T2 *pPhase, int w2, int h2, const CRect<int> &rcROI2);

template <class T1, class T2> void ImgPhaseShifting_5Frame

(T1 **ppSrc[5], int w1, int h1, const CRect<int> &rcROI1,

T2 *pPhase, int w2, int h2, const CRect<int> &rcROI2,

T2 *pBK, int w3, int h3, const CRect<int> &rcROI3,

T2 *pCon, int w4, int h4, const CRect<int> &rcROI4);

64

Page 65: Doc Imgp Roc Lib

Description 5-frame phase-shifting algorithmThis algorithm takes five images with a constant unknown phase shift. Allinput images share the same ROI. T1 can be any integer or float type. T2must be float or double to store the phase angles [−π, π].

Parameters See Table 2 for common parametersppSrc pointer to an array of images (array size = 5)pPhase output wrapped phase imagepBK output background imagepCon output contrast imageReturn the estimated constant phase shift at each step

3.85 ImgPhaseUnw LToR

template <class T> void ImgPhaseUnw_LToR

(const T *pWph, int w1, int h1, const CRect<int> &rcROI1,

T *pUnw, int w2, int h2, const CRect<int> &rcROI2);

Description Left-to-right phase unwrapping algorithmParameters See Table 2 for common parameterspWph input wrapped phasepUnw output unwrapped phase

(a) (b)

Figure 63: An example of the ImgPhaseUnw LToR function. Wrapped phase image is as shown inFig. 62(d). (a) Unwrapped phase image. (b) A linear carrier phase component is removed from theunwrapped phase image.

3.86 ImgPhaseUnw QualGui

template <class T> void ImgPhaseUnw_QualGui

(const T *pWph, int w1, int h1, const CRect<int> &rcROI1,

T *pQual, int w2, int h2, const CRect<int> &rcROI2,

T *pUnw, int w3, int h3, const CRect<int> &rcROI3);

Description Quality-guided phase unwrapping algorithmParameters See Table 2 for common parameterspWph input wrapped phasepQual input quality mappUnw output unwrapped phase

3.87 ImgPhaseGradVar

template <class T> void ImgPhaseGradVar

(const T *pPhase, int w1, int h1, const CRect<int> &rcROI1,

T *pGradVar, int w2, int h2, const CRect<int> &rcROI2);

Description Get phase-gradient-variance imageParameters See Table 2 for common parameterspPhase input phasepGradVar output the inverse of phase gradient variance

65

Page 66: Doc Imgp Roc Lib

(a) (b)

Figure 64: An example of the ImgPhaseUnw QualGui function. Wrapped phase image is as shown inFig. 62(d). (a) Unwrapped phase image. (b) A linear carrier phase component is removed from theunwrapped phase image.

3.88 ImgFaceDetection

template <class T1, class T2> void ImgFaceDetection

(const T1 *pImg, int w, int h, const CRect<int> &rcROI,

T1 tContrast, int nMinFaceW, int nMaxFaceW,

vector<T2> *pvSum, vector<T2> *pvSum2,

vector<CLine2D<int> > *pvlnEye, vector<CRect<int> > *pvrcFace = 0);

Description Face detection algorithmParameters See Table 2 for common parameterstContrast min contrast of a valid face regionnMinFaceW minimum possible face width in pixelnMaxFaceW maximum possible face width in pixelpvlnEye line from the left to the right eye ball of each detected facepvrcFace region of each detected face (For debugging purpose, input 0 to ignore.)pvSum summation of left-top region pixelspvSum2 summation of left-top region pixels’ square

pvSum and pvSum2 are used internally. Their memory space are allocatedonly if necessary.

Return the number of faces detected

3.89 ImgSquareDetection

template <class T1, class T2> void ImgSquareDetection

(const T1 *pImg, int w, int h, const CRect<int> &rcROI,

T1 tContrast, int nMinSqu, int nMaxSqu, vector<T2> *pvSum,

vector<CRect<int> > *pvRect);

Description Detect black squares in an imageAny square smaller than 6 pixels in width or larger than the image widthor height/1.4 is ignored.

Parameters See Table 2 for common parameterstContrast min contrast of a valid square regionnMinSqu minimum possible square size in pixelnMaxSqu maximum possible square size in pixelpvSum summation of left-top region pixels. pvSum is used internally. Its memory

space are allocated only if necessary.pvRect region of each detected squareReturn the number of squares detected

3.90 ImgBlackSquareGrid

template <class T1, class T2> bool ImgBlackSquareGrid

(const T1 *pImg, int w, int h, const CRect<int> &rcROI,

T1 tContrast, int nMinSqu, int nMaxSqu, vector<T2> *pvSum,

vector<CPt2D<double> > *pvptCor, int *pnCx, int *pnCy,

66

Page 67: Doc Imgp Roc Lib

vector<CRect<int> > *pvRect = 0, vector<CPt2D<int> > *pvptEdge = 0,

vector<CLine2D<int> > *pvlnEdge = 0, bool bRough = false);

Description Detect a black square grid in an imageIn camera calibration, a black-square-grid pattern is often used. This func-tion detects the corners of all the squares.

1 Detect squares using ImgSquareDetection(..).2 Estimate corner locations by intersecting lines with outer squares. The lines

are derived from the center of the four squares at the grid corner.3 Refine corner locations of each square by intersecting edge lines, which are

fitted to a square’s edge points.Parameters See Table 2 for common parameterstContrast min contrast of a valid square regionnMinSqu minimum possible square size in pixelnMaxSqu maximum possible square size in pixelpvSum summation of left-top region pixels. pvSum is used internally. Its memory

space are allocated only if necessary.pvptCor the corner points of black square grid detectedpnCx the number of square corners in each row. It is twice the number of squares

in each row.pnCy the number of square corners in each column. It is twice the number of

squares in each column.pvRect rough location of each square (input 0 to ignore)pvptEdge edge points on each square (input 0 to ignore)pvlnEdge edge lines of each square (input 0 to ignore)bRough indicate if search for rough corners onlyReturn true: a black square grid is detected;

false: squares are too small or the number of squares is not identical in eachrow or column.

(a) (b) (c)

Figure 65: Examples of the ImgSquareDetection and ImgBlackSquareGrid functions. (a) ImgSquareDe-tection. (b) ImgBlackSquareGrid with bRough set to true. (c) ImgBlackSquareGrid with bRough set tofalse (retrieve refined corners).

3.91 ImgReadASCII

template <class T> bool ImgReadASCII

(const char *pcFileName, vector<T> *pvImg, int *pW, int *pH);

Description Read image data from ASCII filesApplicable to integer, float ...; Not to char, unsigned char.

Parameters See Table 2 for common parameterspcFileName file namepvImg output image containerpW image widthpH image heightReturn true: succeeded; otherwise, false.

67

Page 68: Doc Imgp Roc Lib

3.92 ImgSaveASCII

template <class T> bool ImgSaveASCII

(const T *pImg, int w, int h, const CRect<int> &rcROI,

const char *pcFileName);

Description Save image data to an ASCII fileInt, float type will be saved as in numbers. char, unsigned char will be savedas characters, which may be undesired. So, if you want to save char value,convert it to int first.

Parameters See Table 2 for common parameterspcFileName file nameReturn true: succeeded; otherwise, false.

3.93 ImgReadRaw

template <class T> bool ImgReadRaw

(T *pImg, int w, int h, const CRect<int> &rcROI,

const char *pcFileName, int nOffset);

Description Read image data from a binary fileThe raw image data must match the data type T. The size of the data to beread is ROI.Width()*ROI.Height(). If the file cannot be opened, the inputbuffer are unchanged.

Parameters See Table 2 for common parameterspcFileName file namenOffset offset in bytes from the beginning of the file, where raw image data start to

be read.Return true: succeeded; otherwise, false.

3.94 ImgSaveRaw

template <class T> bool ImgSaveRaw

(const T *pImg, int w, int h, const CRect<int> &rcROI,

const char *pcFileName, bool bAppend = false);

Description Save raw image data as a binary fileParameters See Table 2 for common parameterspcFileName file namebAppend whether append to the end of the file or create a new file. If ’pcFileName’

does not exist, a new file will be created.Return true: succeeded; otherwise, false.

68

Page 69: Doc Imgp Roc Lib

4 3D geometry classes and functions

4.1 CPt3D: 3D point class

template <class T> class CPt3D

{

public:

T x, y, z;

CPt3D<T>() : x(0), y(0), z(0) { }

CPt3D<T>(const CPt3D<T> &pt) : x(pt.x), y(pt.y), z(pt.z) { }

CPt3D<T>(T tx, T ty, T tz) : x(tx), y(ty), z(tz) { }

CPt3D<T>& operator = (const CPt3D<T> &pt);

CPt3D<T>& operator += (const CPt3D<T> &pt);

CPt3D<T>& operator -= (const CPt3D<T> &pt);

CPt3D<T> operator + (const CPt3D<T> &pt) const;

CPt3D<T> operator - (const CPt3D<T> &pt) const;

bool operator == (const CPt3D<T> &pt) const;

bool operator != (const CPt3D<T> &pt) const;

void SetPoint(T tx, T ty, T tz) { x = tx; y = ty; z = tz; }

void Rotate(const CLine3D<T> &lnAxis, double dAngle);

};

template <class T>

CPt3D<T> operator * (T val, const CPt3D<T> &pt);

template <class T>

CPt3D<T> operator * (const CPt3D<T> &pt, T val);

Variable Descriptionx x coordinate of the pointy y coordinate of the pointz z coordinate of the point

Function Descriptionoperator = Assignment operatoroperator + = *this = *this + ptoperator − = *this = *this − ptoperator + Return *this + ptoperator − Return *this − ptoperator == Check if *this is equal to ptoperator != Check if *this is not equal to ptoperator * Each x, y, z coordinate multiples a value

Function DescriptionSetPoint Set the x, y and z coordinates of the pointRotate Rotate the point with respect to an arbitrary line

4.2 CSize3D: 3D size class

template <class T> class CSize3D

{

public:

T cx, cy, cz;

CSize3D<T>() : cx(0), cy(0), cz(0) { }

CSize3D<T>(const CSize3D<T> &size);

CSize3D<T>(T tcx, T tcy, T tcz) : cx(tcx), cy(tcy), cz(tcz) { }

CSize3D<T>& operator = (const CSize3D<T> &size);

69

Page 70: Doc Imgp Roc Lib

CSize3D<T>& operator += (const CSize3D<T> &size);

CSize3D<T>& operator -= (const CSize3D<T> &size);

CSize3D<T> operator + (const CSize3D<T> &size) const;

CSize3D<T> operator - (const CSize3D<T> &size) const;

bool operator == (const CSize3D<T> &size) const;

bool operator != (const CSize3D<T> &size) const;

void SetSize(T tcx, T tcy, T tcz) { cx = tcx; cy = tcy; cz = tcz; }

};

Variable Descriptioncx size in x directioncy size in y directioncz size in z direction

Function Descriptionoperator = Assignment operatoroperator + = *this = *this + sizeoperator − = *this = *this − sizeoperator + Return *this + sizeoperator − Return *this − sizeoperator == Check if *this is equal to sizeoperator != Check if *this is not equal to size

4.3 CLine3D: 3D line segment class

template <class T> class CLine3D

{

public:

CLine3D<T>() : m_ptStart(0,0,0), m_ptEnd(0,0,0) { }

CLine3D<T>(const CLine3D<T> &line);

CLine3D<T>(T x1, T y1, T z1, T x2, T y2, T z2);

CLine3D<T>(const CPt3D<T> &ptStart, const CPt3D<T> &ptEnd);

CLine3D<T>& operator = (const CLine3D<T> &line);

bool operator == (const CLine3D<T> &line) const;

bool operator != (const CLine3D<T> &line) const;

CPt3D<T> Start() const;

CPt3D<T> End() const;

CPt3D<T> Center() const;

double Length() const;

void SetLine (const CPt3D<T> &ptStart, const CPt3D<T> &ptEnd);

void SetStart (const CPt3D<T> &pt);

void SetEnd (const CPt3D<T> &pt);

void SetCenter (const CPt3D<T> &pt);

void Offset (T x, T y, T z);

void Offset (const CPt3D<T> &ptOffset);

bool GetPointAt(double dDist, CPt3D<T> *ppt) const;

bool IntersectPlane(const CPt3D<T> &ptVec, const CPt3D<T> &ptArb,

CPt3D<T> *ppt);

private:

CPt3D<T> m_ptStart, m_ptEnd; // Start and end point

};

Function Descriptionoperator = Assignment operatoroperator == Check if *this is equal to lineoperator != Check if *this is not equal to line

70

Page 71: Doc Imgp Roc Lib

Function DescriptionStart Get the start point of the lineEnd Get the end point of the lineCenter Get the center of the lineLength Get the length of the lineSetLine Set the start and the end points of the lineSetStart Set the start point of the lineSetEnd Set the end point of the lineSetCenter Set the center of the lineOffset Offset the position of the line

GetPointAt Get a point on the line that has a specified distance from the star point.Take the start point as a reference, dDist > 0 indicates the point is at thesame side as the end point; while dDist < 0 indicates the point is at theopposite side of the end point.

dDist the distance of the point from the start point.ppt the point foundReturn true: point found; false: cannot find such a point.

IntersectPlane Get the intersect of the line segment with a planeptVec normal vector of the planeptArb an arbitrary point on the planeppt store intersection point if foundReturn true: intersect found; false: no intersect.

4.4 CCube: cube class

template <class T> class CCube

{

public:

T left, top, front, right, bottom, back;

CCube<T>() : left(0), top(0), front(0), right(0), bottom(0), back(0) { }

CCube<T>(const CCube<T> &cube);

CCube<T>(T lt, T tp, T ft, T rt, T bm, T bk);

CCube<T>(const CRect<T>& rect, T ft, T bk);

CCube<T>& operator = (const CCube<T> &cube);

CCube<T>& operator &= (const CCube<T> &cube);

CCube<T>& operator |= (const CCube<T> &cube);

CCube<T> operator & (const CCube<T> &cube) const;

CCube<T> operator | (const CCube<T> &cube) const;

bool operator == (const CCube<T> &cube) const;

bool operator != (const CCube<T> &cube) const;

bool IsEmpty() const;

T Width() const;

T Height() const;

T Depth() const;

CSize3D<T> Size() const;

CPt3D<T> Center() const;

CPt3D<T> LeftTopFront() const;

void SetCube(T lt, T tp, T ft, T rt, T bm, T bk);

void Inflate(T lt, T tp, T ft, T rt, T bm, T bk);

void Deflate(T lt, T tp, T ft, T rt, T bm, T bk);

void Inflate(T x, T y, T z);

void Deflate(T x, T y, T z);

void Offset (T x, T y, T z);

void Offset (const CPt3D<T> &ptOffset);

bool PtIn (T x, T y, T z) const;

71

Page 72: Doc Imgp Roc Lib

bool PtIn (const CPt3D<T>& pt) const;

};

Variable Descriptionleft left bound of the cubetop top bound of the cubefront front bound of the cuberight right bound of the cubebottom bottom bound of the cubeback back bound of the cube

Function Descriptionoperator = Assignment operatoroperator &= *this = Intersect of two cubesoperator |= *this = Union of two cubesoperator & Return intersect of two cubesoperator | Return union of two cubesoperator == Check if *this is equal to cubeoperator != Check if *this is not equal to cube

Function DescriptionIsEmpty Check if it is an empty cube. A cube is empty if its width, height or depth

is <= 0.Width Get the width (right− left) of the cubeHeight Get the height (bottom− top) of the cubeDepth Get the depth (back − front) of the cubeSize Get the size of the cubeCenter Return the center of the cubeLeftTopFront Return the left-top-front point of the cube

Function DescriptionSetCube Set the left, top, front, right, bottom and back value of the cubeInflate Inflate the cube by lt, tp, ft, rt, bm, bk on the left, top, front, right, bottom

and back sides respectively.Inflate the cube by x on the left and right sides, by y on the top and bottomsides, and by z on the front and back sides respectively.

Deflate Deflate the cube by lt, tp, ft, rt, bm, bk on the left, top, front, right, bottomand back side respectively.Deflate the cube by x on the left and right sides, by y on the top and bottomsides, and by z on the front and back sides respectively.

Offset Offset the position of the cubePtIn Check if a point is inside the cube. A point is side a cube, if pt.x ∈

[left, right) AND pt.y ∈ [top, bottom) AND pt.z ∈ [front, back),

4.5 CTri3D: 3D triangle class

template <class T> class CTri3D

{

public:

CTri3D<T>() { }

CTri3D<T>(const CTri3D<T> &triangle);

CTri3D<T>& operator = (const CTri3D<T> &triangle);

CPt3D<T> operator [] (int idx) const { return m_ppt[idx]; }

CPt3D<T>& operator [] (int idx) { return m_ppt[idx]; }

const CPt3D<T>& Vertex(int idx) const;

void SetVertex(int idx, const CPt3D<T> &pt);

void ReverseVertexSequence();

private:

72

Page 73: Doc Imgp Roc Lib

CPt3D<T> m_ppt[3];

};

Function Descriptionoperator = Assignment operatoroperator [ ] Get vertex operator. Valid index is 0, 1, and 2. The 1st operator

[ ] is for retrieving a vertex value. The 2nd operator [ ] is forsetting a vertex value.

Vertex Get a vertex value. Valid index is 0, 1, and 2.SetVertex Set a vertex value. Valid index is 0, 1, and 2.ReverseVertexSequence Reverse vertex sequence. Example:

Old sequence: tri[0] = pt1, tri[1] = pt2, tri[2] = pt3.New sequence: tri[0] = pt3, tri[1] = pt2, tri[2] = pt1.

4.6 CPoly3D: 3D polygon class

template <class T> class CPoly3D

{

public:

CPoly3D<T>() { }

CPoly3D<T>(const CPoly3D<T> &poly);

CPoly3D<T>(int nCount, const CPt3D<T> &pt);

CPoly3D<T>& operator = (const CPoly3D<T> &poly);

int Count() const { return int(m_vpt.size()); }

const CPt3D<T>& Vertex(int idx) const;

void AddVertex( const CPt3D<T> &pt);

void SetVertex(int idx, const CPt3D<T> &pt);

void Clear() { m_vpt.clear(); }

void ReverseVertexSequence();

private:

vector<CPt3D<T> > m_vpt;

};

Function Descriptionoperator = Assignment operatorCount Get the number of vertices of the polygonVertex Get the idx-th vertex of the polygonAddVertex Add a vertex at the end of the vertex listSetVertex Set the idx-th vertex valueClear Remove all vertices of the polygonReverseVertexSequence Reverse vertex sequence

4.7 CCylinder: cylinder class

template <class T> class CCylinder

{

public:

CCylinder<T>() : m_Radius(0) { }

CCylinder<T>(const CCylinder<T> &cylinder);

CCylinder<T>(const CLine3D<T> &lnCenter, T radius);

CCylinder<T>& operator = (const CCylinder<T> &cylinder);

CLine3D<T> CenterLine() const { return m_lnCenter; }

T Radius() const { return m_Radius; }

void SetCenterLine(const CLine3D<T> &lnCenter);

void SetRadius(T radius) { m_Radius = radius; }

void SetCylinder(const CLine3D<T> &lnCenter, T radius);

73

Page 74: Doc Imgp Roc Lib

private:

CLine3D<T> m_lnCenter; // center line of the cylinder

T m_Radius; // radius of the cylinder

};

Function Descriptionoperator = Assignment operatorCenterLine Get the center line of the cylinderRadius Get the radius of the cylinderSetCenterLine Set the center line of the cylinderSetRadius Set the radius of the cylinderSetCylinder Set the center line and radius of the cylinder

X0204060

Z

0

20

40

Y-40-200204060

radius

center line

Figure 66: An example of a cylinder.

4.8 Distance

template <class T> double Distance

(const CPt3D<T> &pt1, const CPt3D<T> &pt2);

Description Get the distance between two pointspt1 1st input pointpt2 2nd input pointReturn the distance between the two points

4.9 Area

template <class T> double Area

(const CPt3D<T> &pt1, const CPt3D<T> &pt2, const CPt3D<T> &pt3);

Description Get the area enclosed by three pointspt1, pt2, pt3 input pointsReturn the area enclosed by three points

4.10 Angle

template <class T> double Angle

(const CPt3D<T> &pt1, const CPt3D<T> &pt2, const CPt3D<T> &pt3);

Description Get the angle in [0, π], formed by pt2, pt1, pt3pt1 1st input point (vertex of the angle)pt2 2nd input pointpt3 3rd input pointReturn the angle formed by pt2, pt1, pt3

74

Page 75: Doc Imgp Roc Lib

4.11 MidPoint

template <class T> CPt3D<T> MidPoint

(const CPt3D<T> &pt1, const CPt3D<T> &pt2);

Description Get the midpoint of two pointspt1 1st input pointpt2 2nd input pointReturn the midpoint

4.12 Centroid

template <class T> CPt3D<T> Centroid

(const CPt3D<T> *ppt, int nCount);

Description Get the centroid of a set of pointsppt an array of pointsnCount the number of pointsReturn the centroid of the input points

4.13 Collinear

template <class T> bool Collinear

(const CPt3D<T> &pt1, const CPt3D<T> &pt2, const CPt3D<T> &pt3);

Description Check collinearity of three pointspt1, pt2, pt3 input pointsReturn true: collinear; otherwise, false.

4.14 NormalVector

template <class T> CPt3D<T> NormalVector

(const CPt3D<T> &pt1, const CPt3D<T> &pt2, const CPt3D<T> &pt3);

Description Get normal vector of a planeThree points defines a plane. This function returns the normal vector ofthe plane. If the input points are collinear, the return value is (0,0,0).

pt1, pt2, pt3 input pointsReturn true: collinear; otherwise, false.

4.15 Volume

template <class T> double Volume

(const CPt3D<T> &pt0, const CPt3D<T> &pt1,

const CPt3D<T> &pt2, const CPt3D<T> &pt3);

Description Get the volume of a tetrahedronThe tetrahedron’s four vertices are given by pt0, ... pt3. If right-hand ruleapplies (Four fingers curve in the order of pt1, pt2, pt3; and the thumbpoints to pt0.), the volume is positive; otherwise the volume is negative.

pt0, ... pt3 tetrahedron’s 4 verticesReturn volume of the tetrahedronNote Internal volume computation is based on double; therefore, for integer data

type, it is better to determine the volume sign by testing if the volume is >0.5 or < -0.5 rather than > 0 or < 0. This helps to prevent float precisionerrors.

4.16 ConvexHull3D

template <class T> bool ConvexHull3D

(vector<CPt3D<T> > *pvptIn, vector<CPt3D<T> > *pvptOut,

vector<CLine3D<T> > *pvLine, vector<CPoly3D<T> > *pvPoly,

bool bTriFace);

75

Page 76: Doc Imgp Roc Lib

Description Get the 3D convex hull of a set of pointsReference ”Computational geometry in C” by Joseph O’Rourke.Based on the book, the convex hull is composed of triangles. I add a post-processing step to output polygon faces of the hull. ConstructPolygon(..)is for this purpose.Due to volume computation required by the function, large input value mayrender the computation go out of range. I roughly tested that 1024 is a verysafe bound. You can try higher value but be careful.

pvptIn input point set; (modified during processing)pvptOut output convex hull vertexes (input 0 to ignore)pvLine output convex hull edges (input 0 to ignore)pvPoly output convex hull polygon faces (must NOT be 0)bTriFace if true, output triangular faces that may be coplanar. This is used for

DelaunayTriangulation.if false, output non-coplanar polygon faces.

Return true: succeeded; false: failed (points are coplanar).

X 0

50

100

Z

0

20

40

Y020

4060

80100

Figure 67: An example of the ConvexHull3D function.

4.17 FitPlane

template <class T1, class T2> bool FitPlane

(const CPt3D<T1> *ppt, const T2 *pWei, int nCount,

double *pdKx, double *pdKy, double *pdZ0);

Description Least squares fitting of a planePlane equation z(x,y) = Kx*x + Ky*y + Z0

ppt points to be fitted (size must be nCount)pWei weighting factor array (input 0 to ignore)nCount size of ppt array and weighting arraypdKx parameter Kx of the plane equation, if succeed; or undefined.pdKy parameter Ky of the plane equation, if succeed; or undefined.pdZ0 parameter Z0 of the plane equation, if succeed; or undefined.Return true: succeeded; false: failed.

4.18 FitPolynomialSurf

template <class T1, class T2> bool FitPolynomialSurf

(const CPt3D<T1> *ppt, const T2 *pWei, int nCount, int N,

vector<double> *pvCoe, vector<int> *pvPowOfX, vector<int> *pvPowOfY);

Description Least squares fitting of a polynomial surfacePolynomial surface equation: z(x,y) =+C0 + C1x+ C3x

2 + C6x3 + ...+ C?x

N

+C2y + C4xy + C7x2y + ...

continued on next page

76

Page 77: Doc Imgp Roc Lib

+C5y2 + C8xy

2 + ...

+...+ ...+ ...

+C(N+1)(N+2)/2−1yN

ppt points to be fitted (size must be nCount)pWei weighting factor array (input 0 to ignore)N the order of the surface equation (should be >= 0)pvCoe store coefficients, if succeed; or undefined.pvPowOfX store power of x of each term, if succeed; or undefined.pvPowOfY store power of y of each term, if succeed; or undefined.

After processing, the size of pvCoe, pvPowOfX and pvPowOfY will be(N+1)(N+2)/2 and their elements are ordered according to the index ofC in the equation.

Return true: succeeded; false: failed.

X0

50

100

Z

50

100

Y

0

50

100X

050

100

Z0

2000

4000

Y

020406080100

(a) (b)

Figure 68: Examples of the (a) FitPlane and the (b) FitPolynomialSurf functions.

4.19 FitGauss

template <class T> bool FitGauss

(const CPt3D<T> *ppt, int nCount, double pdCoe[5]);

Description Least squares fitting of a 2D Gaussian function

Gaussian function: z = Ae−(x−B)2/(2C)−(y−D)2/(2E). Fitting is based on alinear least squares method: the z coordinate of all points is converted toln(z), which requires that z > 0.

ppt points to be fitted (size must be nCount)nCount size of ppt arraypdCoe parameters of the Gaussian function

pdCoe[0], pdCoe[1], ... pdCoe[4] are A, B, ... E respectively.Return true: succeeded; false: failed.

X 0

50

100

Z

0

20

40

Y020

4060

80100

Figure 69: An example of the 2D FitGauss function.

77

Page 78: Doc Imgp Roc Lib

5 3D image processing functions

Some parameters are common to most functions. They are listed in Table 3.

Type Name Description

T* pImg Pointer to a continuous memory space which must be equal toor larger than w*h*d*sizeof(T) bytes. The z-th frame, y-th row,x-th column element can be retrieved by *(pImg+(z*h+y)*w+x)or pImg[(z*h+y)*w+x].

T* pSrc Pointer to the source imageT* pDst Pointer to the destination imageint w, h, d Image width, height and depthCCube<int>& cbROI Region of interest. Only image data within the ROI will be used

or modified.

Table 3: Common parameters of 3D image processing functions.

5.1 ImgAssign3D

template <class T> void ImgAssign3D

(T *pImg, int w, int h, int d, const CCube<int> &cbROI,

T value);

Description Assign image data in an ROI to the input valueParameters See Table 3 for common parametersvalue the value to be assigned to the image data

5.2 ImgCopy3D

template <class T1, class T2> void ImgCopy3D

(const T1 *pSrc, int w1, int h1, int d1, const CCube<int> &cbROI1,

T2 *pDst, int w2, int h2, int d2, const CCube<int> &cbROI2);

Description Copy image data from source image ROI1 to destination image ROI2Width, height and depth of ROI1 and ROI2 must be the same.

Parameters See Table 3 for common parameters

5.3 ImgCopyXY 2D

template <class T1, class T2> void ImgCopyXY_2D

(const T1 *pSrc, int w1, int h1, int d1, const CRect<int> &rcROI1, int z,

T2 *pDst, int w2, int h2, const CRect<int> &rcROI2);

Description Copy ROI1 in a X-Y slice of a 3D image to ROI2 in a 2D imageParameters See Tables 2 and 3 for common parametersz z-direction index of the X-Y slice in the 3D image

5.4 ImgCopyXZ 2D

template <class T1, class T2> void ImgCopyXZ_2D

(const T1 *pSrc, int w1, int h1, int d1, const CRect<int> &rcROI1, int y,

T2 *pDst, int w2, int h2, const CRect<int> &rcROI2);

Description Copy ROI1 in a X-Z slice of a 3D image to ROI2 in a 2D imageParameters See Table 2 and 3 for common parametersy y-direction index of the X-Z slice in the 3D image

78

Page 79: Doc Imgp Roc Lib

5.5 ImgCopyYZ 2D

template <class T1, class T2> void ImgCopyYZ_2D

(const T1 *pSrc, int w1, int h1, int d1, const CRect<int> &rcROI1, int x,

T2 *pDst, int w2, int h2, const CRect<int> &rcROI2);

Description Copy ROI1 in a Y-Z slice of a 3D image to ROI2 in a 2D imageParameters See Table 2 and 3 for common parametersx x-direction index of the Y-Z slice in the 3D image

5.6 ImgCopy2D XY

template <class T1, class T2> void ImgCopy2D_XY

(const T1 *pSrc, int w1, int h1, const CRect<int> &rcROI1,

T2 *pDst, int w2, int h2, int d2, const CRect<int> &rcROI2, int z);

Description Copy ROI1 in a 2D image to ROI2 in a X-Y slice of a 3D imageParameters See Tables 2 and 3 for common parametersz z-direction index of the X-Y slice in the 3D image

5.7 ImgCopy2D XZ

template <class T1, class T2> void ImgCopy2D_XZ

(const T1 *pSrc, int w1, int h1, const CRect<int> &rcROI1,

T2 *pDst, int w2, int h2, int d2, const CRect<int> &rcROI2, int y);

Description Copy ROI1 in a 2D image to ROI2 in a X-Z slice of a 3D imageParameters See Tables 2 and 3 for common parametersy y-direction index of the X-Z slice in the 3D image

5.8 ImgCopy2D YZ

template <class T1, class T2> void ImgCopy2D_YZ

(const T1 *pSrc, int w1, int h1, const CRect<int> &rcROI1,

T2 *pDst, int w2, int h2, int d2, const CRect<int> &rcROI2, int x);

Description Copy ROI1 in a 2D image to ROI2 in a Y-Z slice of a 3D imageParameters See Tables 2 and 3 for common parametersx x-direction index of the Y-Z slice in the 3D image

5.9 ImgLinear3D

template <class T1, class T2> void ImgLinear3D

(const T1 *pSrc, int w1, int h1, int d1, const CCube<int> &cbROI1,

T2 *pDst, int w2, int h2, int d2, const CCube<int> &cbROI2,

T1 fromMin, T1 fromMax, T2 toMin, T2 toMax);

Description Linear translate Src data in the range [fromMin, fromMax] to Dst datato the range [toMin, toMax]. Linear translation parameters a and b isdetermined by:a * fromMin + b = toMin;a * fromMax + b = toMax;Data points in Src that are < fromMin or > fromMax will be translate totoMin and toMax. pSrc and pDst may point to the same image buffer, ifROI1 and ROI2 are the same.

Parameters See Table 3 for common parametersfromMin source minimum boundfromMax source maximum boundtoMin destination minimum boundtoMax destination maximum bound

79

Page 80: Doc Imgp Roc Lib

5.10 ImgMin3D

template <class T> T ImgMin3D

(const T *pImg, int w, int h, int d, const CCube<int> &cbROI);

Description Get the minimum value of an ROIParameters See Table 3 for common parametersReturn the minimum value of an ROI

5.11 ImgMax3D

template <class T> T ImgMax3D

(const T *pImg, int w, int h, int d, const CCube<int> &cbROI);

Description Get the maximum value of an ROIParameters See Table 3 for common parametersReturn the maximum value of an ROI

5.12 ImgMinMax3D

template <class T> void ImgMinMax3D

(const T *pImg, int w, int h, int d, const CCube<int> &cbROI,

T *pMin, T *pMax);

Description Get the minimum and maximum value of an ROIParameters See Table 3 for common parameterspMin return the minimum value of an ROIpMax return the maximum value of an ROI

5.13 ImgMean3D

template <class T> T ImgMean3D

(const T *pImg, int w, int h, int d, const CCube<int> &cbROI);

Description Get the mean value of an ROIParameters See Table 3 for common parametersReturn the mean value of an ROI

5.14 ImgFltMean3D

template <class T> void ImgFltMean3D

(const T *pSrc, int w1, int h1, int d1, const CCube<int> &cbROI1,

T *pDst, int w2, int h2, int d2, const CCube<int> &cbROI2,

int nKerW, int nKerH, int nKerD);

Description Mean filter, kernel certer equal to kernel meanHalf-filter-length data at the ROI boundary are filtered with reduced sizedkernel.

Parameters See Table 3 for common parametersnKerW filter kernel widthnKerH filter kernel heightnKerD filter kernel depthExample nKerW*nKerH*nKerD = 3*3*3 or 7*5*3

5.15 ImgFltVariance3D

template <class T1, class T2> void ImgFltVariance3D

(const T1 *pSrc, int w1, int h1, int d1, const CCube<int> &cbROI1,

T2 *pDst, int w2, int h2, int d2, const CCube<int> &cbROI2,

int nKerW, int nKerH, int nKerD);

80

Page 81: Doc Imgp Roc Lib

Description Variance filter, kernel certer equal to kernel varianceHalf-filter-length data at the ROI boundary are filtered with reduced sizedkernel.

Parameters See Table 2 for common parametersnKerW filter kernel widthnKerH filter kernel heightnKerD filter kernel depthExample nKerW*nKerH = 3*3 or 7*5

5.16 ImgConv3DZ

template <class T1, class T2> void ImgConv3DZ

(T1 *pImg, int w, int h, int d, const CCube<int> &cbROI,

const T2 *pMask, int nLen);

Description 1D convolution in z directionConvolution is applied on each trace (z direction). Half-filter-length data atthe ROI boundary are unchanged. The mask data array size must satisfynLen+(nLen-1)/2 ≤ ROI.Depth(); otherwise nothing is done.

Parameters See Table 3 for common parameterspMask pointer to the convolution mask data arraynLen mask array size

5.17 ImgResize3D

template <class T1, class T2> void ImgResize3D

(const T1 *pSrc, int w1, int h1, int d1, const CCube<int> &cbROI1,

T2 *pDst, int w2, int h2, int d2, const CCube<int> &cbROI2,

int nAlgo);

Description Resize ROI1 of Src and copy to ROI2 of DstIf linear (nAlgo: 1) or spline (nAlgo: 2–4) is used, minification is alwaysa downsampling process based on averaging. Magnification is done by thespecified method. If nearest neighbour (nAlgo: 0) is used, both minificationand magnification are based on nearest neighbour.

Parameters See Table 3 for common parametersnAlgo interpolation algorithm

0: nearest neighbour interpolation,1: linear interpolation,2: cubic B-spline interpolation,3: Catmull-Rom spline interpolation,4: natural cubic spline interpolation.

5.18 ImgRotate3D

template <class T1, class T2> void ImgRotate3D

(const T1 *pSrc, int w1, int h1, int d1, const CCube<int> &cbROI1,

T2 *pDst, int w2, int h2, int d2, const CCube<int> &cbROI2,

int nAxis, int nAngle);

Description Rotate ROI1 of Src and copy to ROI2 of DstThe size of ROI1 and ROI2 must satisfy certain relationship based on theangle of rotation.

Parameters See Table 3 for common parametersnAxis with respect to which axis to rotate. 0: X axis (points to the right), 1: Y

axis (points to the bottom), 2: Z axis (points outside the screen).nAngle anti-clock wise angle (right-hand rule) of rotation in degree. Only multiples

of 90-degree are accepted.

81

Page 82: Doc Imgp Roc Lib

5.19 ImgFlip3D

template <class T> void ImgFlip3D

(T *pImg, int w, int h, int d, const CCube<int> &cbROI,

int nDirection);

Description Flip the image data in an ROIParameters See Table 3 for common parametersnDirection 0: left-right flipped, 1: top-bottom flipped, 2: front-back flipped.

5.20 ImgMatch3D

template <class T> void ImgMatch3D

(const T *pMother, int w1, int h1, int d1, const CCube<int> &cbROI1,

const T *pChild, int w2, int h2, int d2, const CCube<int> &cbROI2,

int nDSX, int nDSY, int nDSZ, CPt3D<int> *pptMatch, double *pdCorr);

Description Search for ROI2 of pChild in ROI1 of pMotherROI1.Width() must be >= ROI2.Width()ROI1.Height() must be >= ROI2.Height()ROI1.Depth() must be >= ROI2.Depth()

1 Initially, ROI2 is matched to windows in ROI1 separated by nDSX, nDSYand nDSZ in the x, y and z directions. After the best-match window isfound; nDSX, nDSY and nDSZ are halved.

2 Then ROI2 is matched to 27 windows, a 3*3*3 grid centered on the best-match window. The windows are separated by nDSX, nDSY and nDSZ.After a new best-match window is found, nDSX, nDSY and nDSZ are fur-ther halved.

3 Go back to 2 until nDSX, nDSY and nDSZ equal to 1 pixel.Parameters See Table 3 for common parametersnDSX initial downsampling gap in x directionnDSY initial downsampling gap in y directionnDSZ initial downsampling gap in z direction

The downsampling gaps are used in shifting ROI1 across ROI2 and in ac-cessing the pixels in ROI1 and ROI2,

pptMatch the best-match point in ROI1 coordinate (left-top-front corner of thematched cube)

pdCorr normalized correlation coefficient, in [-1,1]

5.21 ImgMotion3D

template <class T> void ImgMotion3D

(const T *pImg1, int w1, int h1, int d1, const CCube<int> &cbROI1,

const T *pImg2, int w2, int h2, int d2, const CCube<int> &cbROI2,

int nCoarW, int nCoarH, int nCoarD,

int nCoarX, int nCoarY, int nCoarZ,

int nCoarDSX, int nCoarDSY, int nCoarDSZ,

int nFineW, int nFineH, int nFineD,

int nFineX, int nFineY, int nFineZ,

int nFineDSX, int nFineDSY, int nFineDSZ,

int &nWinX, int &nWinY, int &nWinZ,

vector<CPt3D<int> > *pvptCenter,

vector<CPt3D<int> > *pvptDisp,

vector<double> *pvCorr);

Description Pixel flow or motion estimationEstimate the pixel motion from ROI1 in pImg1 to ROI2 in pImg2. ROI1and ROI2 must be the same size.

continued on next page

82

Page 83: Doc Imgp Roc Lib

1 There are two levels of search: a coarse search based on a few coarse windowsand a quality-guided fine search on all fine windows. Results of the coarsesearch are used as the initial guess for the fine search.

2 A large coarse search window and search distance may be used to obtainrobust initial estimates, and a small fine search window and search distanceto obtain a high resolution. The downsampling gap at each level is setindependently. See ImgMatch3D(...) for details of the downsampling gap.

3 Results are stored in pvptCenter, pvptDisp, pvCorr, which are arrays of thesame size: nWinX * nWinY * nWinZ (at output).

Parameters See Table 3 for common parametersnCoarW coarse search window widthnCoarH coarse search window heightnCoarD coarse search window depthnCoarX coarse search distance in x directionnCoarY coarse search distance in y directionnCoarZ coarse search distance in z directionnCoarDSX coarse search downsampling gap in x directionnCoarDSY coarse search downsampling gap in y directionnCoarDSZ coarse search downsampling gap in z directionnFineW fine search window widthnFineH fine search window heightnFineD fine search window depthnFineX fine search distance in x directionnFineY fine search distance in y directionnFineZ fine search distance in z directionnFineDSX fine search downsampling gap in x directionnFineDSY fine search downsampling gap in y directionnFineDSZ fine search downsampling gap in z directionnWinX input desired number of fine search windows in x direction

output actual number of fine search windows in x directionnWinY input desired number of fine search windows in y direction

output actual number of fine search windows in y directionnWinZ input desired number of fine search windows in z direction

output actual number of fine search windows in z directionpvptCenter center of fine search windows in pImg1pvptDisp displacement of fine windows from pImg1 to pImg2.

This includes the global shift from ROI1 to ROI2pvCorr correlation coefficient of each fine window

5.22 ImgLabeling3D

template <class T1, class T2, class Pred> int ImgLabeling3D

(const T1 *pSrc, int w1, int h1, int d1, const CCube<int> &cbROI1,

T2 *pDst, int w2, int h2, int d2, const CCube<int> &cbROI2,

Pred pred, int nConnectivity, int nMinVol = 0, int nMaxVol = 0,

vector<CCube<int> > *pvCube = 0, vector<int> *pvVol = 0);

Description Connectivity labeling algorithmParameters See Table 3 for common parameterspred if pred(pSrc[(z*h+y)*w+x]) is true, the point (x,y,z) is considered an object

point.nConnectivity Euclidean connectivity of an object.

0: 6-neighbour labeling algorithm,1: 26-neighbour labeling algorithm,>1: pixels within this distance are one object.

nMinVol Object fewer than nMinVol pixels are discarded.nMaxVol Object larger than nMaxVol pixels are discarded.

continued on next page

83

Page 84: Doc Imgp Roc Lib

pvCube bounding cube of each object (input 0 to ignore)It is with respect to ROI2. To obtain a bounding cube with respect toROI1, each cube should be offset cb1.LeftTopFront()-cb2.LeftTopFront().

pvVol number of points of each object (input 0 to ignore)Return the number of objects foundExample The 1st object can be found by checking if(pDst[(z*h+y)*w+x]==1).

The 1st object’s bounding cube is (*pvCube)[0].The 3rd object’s number of points is (*pvVol)[2].

5.23 ImgFilling3D

template <class T, class Pred> void ImgFilling3D

(T *pImg, int w, int h, int d, const CCube<int> &cbROI,

T value, Pred pred, CPt3D<int> ptSeed, int nConnectivity);

Description Fill a region bounded by a predicate conditionParameters See Table 3 for common parametersvalue value to fill withpred if pred(pImg[(z*h+y)*w+x],value) is true, the point (x,y,z) is consid-

ered boundary. Only three predicates are valid: equal to, greater equal,less equal. Other predicates such as greater or less may cause dead loop, sothey should be strictly prohibited.

ptSeed initial seed point for fillingnConnectivity Euclidean connectivity of a filling region.

0: 6-neighbour filling algorithm,1: 26-neighbour filling algorithm,>1: pixels within this distance are filled.

5.24 ImgDistTrans3D

template <class T> void ImgDistTrans3D

(const T *pSrc, int w1, int h1, int d1, const CCube<int> &cbROI1,

unsigned long *pnDst, int w2, int h2, int d2, const CCube<int> &cbROI2,

double dZSpacing, T threshold);

Description Distance transform algorithmReference ”Euclidean Distance Mapping” by P. Danielsson.

Parameters See Table 3 for common parameterspnDst a background pixel is set to 0; an object pixel is set to the square of the

Euclidean distance to its nearest background pixel.dZSpacing frame spacing in z direction. The pixel spacing in the x and y directions is

assumed to be 1.threshold pixels >= threshold in pSrc are objects;

pixels < threshold in pSrc are background.

5.25 ImgWatershed3D

template <class T1, class T2> int ImgWatershed3D

(const T1 *pSrc, int w1, int h1, int d1, const CCube<int> &cbROI1,

T2 *pDst, int w2, int h2, int d2, const CCube<int> &cbROI2,

int nDist, int nMinPts);

Description Watershed algorithm for segmentationThis function assumes the input pSrc a distance map, for example, obtainedfrom ImgDistTrans3D(...). It outputs an object map similar to that pro-duced by ImgLabeling3D(...). In the object map, connected regions aresegmented.

continued on next page

84

Page 85: Doc Imgp Roc Lib

1 Local maxima are located for each object.2 The first-pass propagation estimates the number of points grown by each

maxima within each object.3 Maxima whose number of points are < nMinPts are discarded.4 The second-pass propagation finishes the segmentation.Parameters See Table 3 for common parametersnDist the distance for merging local maxima.

0: 6-neighbour merging,1: 26-neighbour merging,>1: local maxima within this distance are merged.

nMinPts threshold of the number of points grown by a maximum.Both parameters are used to reduce over-segmentation.

Return The number of segmented objects, if succeeded; otherwise -1.

5.26 ImgIsoSurface

template <class T1, class T2> int IsoSurface

(const T *pImg, int w, int h, int d, const CCube<int> &cbROI,

T threshold, int nFltSize, vector<CTri3D<float> > *pvTri);

Description Extract isosurface from 3D dataReference “Regularised marching tetrahedra: improved iso-surface extrac-tion” by Graham Treece et al.

1 Binarize the 3D data based on the input threshold. The 3D data are bestto have the same pixel spacing in x, y and z directions but it is not arequirement.

2 Compute a distance map, which represents the distance of each voxel tothe object boundary. A distance code 22-31-38 is used. (Voxel width: 22.Diagonal: 31. Double diagonal: 38.) Voxels inside the object are > 0 andthose outside are < 0. Refer to the paper ”Distance transformations inarbitrary dimensions” by Gunilla Borgefors for basic ideas.

3 Smooth the distance map, if nFltSize is > 1.4 Use body-centered cube layout (partitioned by tetrahedra) to extract iso-

surface.Parameters See Table 3 for common parametersthreshold threshold for the object regionnFltSize filter kernel size (ignored if <= 1)pvTri3D output triangles of the isosurfaceReturn the number of triangles on the isosurface

X

100

150

Z

80

100

120

140

160

180

Y

100

150

Figure 70: An example of the ImgIsoSurface function. Isosurface of a sphere.

5.27 ImgReadRaw3D

template <class T> bool ImgReadRaw3D

(T *pImg, int w, int h, int d, const CCube<int> &cbROI,

const char *pcFileName, int nOffset);

85

Page 86: Doc Imgp Roc Lib

Description Read image data from a binary fileThe raw image data must match the data type T. The size of the data tobe read is ROI.Width()*ROI.Height()*ROI.Depth(). If the file cannot beopened, the input buffer are unchanged.

Parameters See Table 3 for common parameterspcFileName file namenOffset offset in bytes from the beginning of the file, where raw image data start to

be read.Return true: succeeded; otherwise, false.

5.28 ImgSaveRaw3D

template <class T> bool ImgSaveRaw3D

(const T *pImg, int w, int h, int d, const CCube<int> &cbROI,

const char *pcFileName, bool bAppend = false);

Description Save raw image data as a binary fileParameters See Table 3 for common parameterspcFileName file namebAppend whether append to the end of the file or create a new file. If ’pcFileName’

does not exist, a new file will be created.Return true: succeeded; otherwise, false.

86


Recommended