1 /*********************************************************************
2  * klt.h
3  *
4  * Kanade-Lucas-Tomasi tracker
5  *********************************************************************/
6 
7 #ifndef _KLT_H_
8 #define _KLT_H_
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
14 typedef float KLT_locType;
15 typedef unsigned char KLT_PixelType;
16 
17 #define KLT_BOOL int
18 
19 #ifndef TRUE
20 #define TRUE  1
21 #define FALSE 0
22 #endif
23 
24 #ifndef NULL
25 #define NULL  0
26 #endif
27 
28 #define KLT_TRACKED           0
29 #define KLT_NOT_FOUND        -1
30 #define KLT_SMALL_DET        -2
31 #define KLT_MAX_ITERATIONS   -3
32 #define KLT_OOB              -4
33 #define KLT_LARGE_RESIDUE    -5
34 
35 #include "klt_util.h" /* for affine mapping */
36 
37 /*******************
38  * Structures
39  */
40 
41 typedef struct  {
42   /* Available to user */
43   int mindist;			/* min distance b/w features */
44   int window_width, window_height;
45   KLT_BOOL sequentialMode;	/* whether to save most recent image to save time */
46   /* can set to TRUE manually, but don't set to */
47   /* FALSE manually */
48   KLT_BOOL smoothBeforeSelecting;	/* whether to smooth image before */
49   /* selecting features */
50   KLT_BOOL writeInternalImages;	/* whether to write internal images */
51   /* tracking features */
52   KLT_BOOL lighting_insensitive;  /* whether to normalize for gain and bias (not in original algorithm) */
53 
54   /* Available, but hopefully can ignore */
55   int min_eigenvalue;		/* smallest eigenvalue allowed for selecting */
56   float min_determinant;	/* th for determining lost */
57   float min_displacement;	/* th for stopping tracking when pixel changes little */
58   int max_iterations;		/* th for stopping tracking when too many iterations */
59   float max_residue;		/* th for stopping tracking when residue is large */
60   float grad_sigma;
61   float smooth_sigma_fact;
62   float pyramid_sigma_fact;
63   float step_factor;  /* size of Newton steps; 2.0 comes from equations, 1.0 seems to avoid overshooting */
64   int nSkippedPixels;		/* # of pixels skipped when finding features */
65   int borderx;			/* border in which features will not be found */
66   int bordery;
67   int nPyramidLevels;		/* computed from search_ranges */
68   int subsampling;		/* 		" */
69 
70 
71   /* for affine mapping */
72   int affine_window_width, affine_window_height;
73   int affineConsistencyCheck; /* whether to evaluates the consistency of features with affine mapping
74                               -1 = don't evaluates the consistency
75                               0 = evaluates the consistency of features with translation mapping
76                               1 = evaluates the consistency of features with similarity mapping
77                               2 = evaluates the consistency of features with affine mapping
78   */
79   int affine_max_iterations;
80   float affine_max_residue;
81   float affine_min_displacement;
82   float affine_max_displacement_differ; /* th for the difference between the displacement calculated
83   by the affine tracker and the frame to frame tracker in pel*/
84 
85   /* User must not touch these */
86   void *pyramid_last;
87   void *pyramid_last_gradx;
88   void *pyramid_last_grady;
89 }  KLT_TrackingContextRec, *KLT_TrackingContext;
90 
91 
92 typedef struct  {
93   KLT_locType x;
94   KLT_locType y;
95   int val;
96   /* for affine mapping */
97   _KLT_FloatImage aff_img;
98   _KLT_FloatImage aff_img_gradx;
99   _KLT_FloatImage aff_img_grady;
100   KLT_locType aff_x;
101   KLT_locType aff_y;
102   KLT_locType aff_Axx;
103   KLT_locType aff_Ayx;
104   KLT_locType aff_Axy;
105   KLT_locType aff_Ayy;
106 }  KLT_FeatureRec, *KLT_Feature;
107 
108 typedef struct  {
109   int nFeatures;
110   KLT_Feature *feature;
111 }  KLT_FeatureListRec, *KLT_FeatureList;
112 
113 typedef struct  {
114   int nFrames;
115   KLT_Feature *feature;
116 }  KLT_FeatureHistoryRec, *KLT_FeatureHistory;
117 
118 typedef struct  {
119   int nFrames;
120   int nFeatures;
121   KLT_Feature **feature;
122 }  KLT_FeatureTableRec, *KLT_FeatureTable;
123 
124 
125 
126 /*******************
127  * Functions
128  */
129 
130 /* Create */
131 KLT_TrackingContext KLTCreateTrackingContext(void);
132 KLT_FeatureList KLTCreateFeatureList(
133   int nFeatures);
134 KLT_FeatureHistory KLTCreateFeatureHistory(
135   int nFrames);
136 KLT_FeatureTable KLTCreateFeatureTable(
137   int nFrames,
138   int nFeatures);
139 
140 /* Free */
141 void KLTFreeTrackingContext(
142   KLT_TrackingContext tc);
143 void KLTFreeFeatureList(
144   KLT_FeatureList fl);
145 void KLTFreeFeatureHistory(
146   KLT_FeatureHistory fh);
147 void KLTFreeFeatureTable(
148   KLT_FeatureTable ft);
149 
150 /* Processing */
151 void KLTSelectGoodFeatures(
152   KLT_TrackingContext tc,
153   KLT_PixelType *img,
154   int ncols,
155   int nrows,
156   KLT_FeatureList fl);
157 void KLTTrackFeatures(
158   KLT_TrackingContext tc,
159   KLT_PixelType *img1,
160   KLT_PixelType *img2,
161   int ncols,
162   int nrows,
163   KLT_FeatureList fl);
164 void KLTReplaceLostFeatures(
165   KLT_TrackingContext tc,
166   KLT_PixelType *img,
167   int ncols,
168   int nrows,
169   KLT_FeatureList fl);
170 
171 /* Utilities */
172 int KLTCountRemainingFeatures(
173   KLT_FeatureList fl);
174 void KLTPrintTrackingContext(
175   KLT_TrackingContext tc);
176 void KLTChangeTCPyramid(
177   KLT_TrackingContext tc,
178   int search_range);
179 void KLTUpdateTCBorder(
180   KLT_TrackingContext tc);
181 void KLTStopSequentialMode(
182   KLT_TrackingContext tc);
183 void KLTSetVerbosity(
184   int verbosity);
185 float _KLTComputeSmoothSigma(
186   KLT_TrackingContext tc);
187 
188 /* Storing/Extracting Features */
189 void KLTStoreFeatureList(
190   KLT_FeatureList fl,
191   KLT_FeatureTable ft,
192   int frame);
193 void KLTExtractFeatureList(
194   KLT_FeatureList fl,
195   KLT_FeatureTable ft,
196   int frame);
197 void KLTStoreFeatureHistory(
198   KLT_FeatureHistory fh,
199   KLT_FeatureTable ft,
200   int feat);
201 void KLTExtractFeatureHistory(
202   KLT_FeatureHistory fh,
203   KLT_FeatureTable ft,
204   int feat);
205 
206 /* Writing/Reading */
207 void KLTWriteFeatureListToPPM(
208   KLT_FeatureList fl,
209   KLT_PixelType *greyimg,
210   int ncols,
211   int nrows,
212   const char *filename);
213 void KLTWriteFeatureList(
214   KLT_FeatureList fl,
215   const char *filename,
216   const char *fmt);
217 void KLTWriteFeatureHistory(
218   KLT_FeatureHistory fh,
219   const char *filename,
220   const char *fmt);
221 void KLTWriteFeatureTable(
222   KLT_FeatureTable ft,
223   const char *filename,
224   const char *fmt);
225 KLT_FeatureList KLTReadFeatureList(
226   KLT_FeatureList fl,
227   const char *filename);
228 KLT_FeatureHistory KLTReadFeatureHistory(
229   KLT_FeatureHistory fh,
230   const char *filename);
231 KLT_FeatureTable KLTReadFeatureTable(
232   KLT_FeatureTable ft,
233   const char *filename);
234 #ifdef __cplusplus
235 }
236 #endif
237 
238 #endif
239 
240 
241 
242 
243 
244 
245