00001
00022 #ifndef __ARTOOLKIT_TRACKERIMPL_HEADERFILE__
00023 #define __ARTOOLKIT_TRACKERIMPL_HEADERFILE__
00024
00025 #include <ARToolKitPlus/ARToolKitPlus.h>
00026 #include <ARToolKitPlus/ar.h>
00027 #include <ARToolKitPlus/arMulti.h>
00028 #include <ARToolKitPlus/vector.h>
00029 #include <ARToolKitPlus/Camera.h>
00030 #include <ARToolKitPlus/extra/BCH.h>
00031 #include <ARToolKitPlus/extra/Hull.h>
00032
00033 #include <vector>
00034
00035
00036 namespace ARToolKitPlus {
00037
00052 class AR_EXPORT Tracker {
00053 public:
00054 Tracker(int imWidth, int imHeight, int maxImagePatterns = 8, int pattWidth = 6, int pattHeight = 6, int pattSamples = 6,
00055 int maxLoadPatterns = 0);
00056 virtual ~Tracker();
00057
00062 virtual bool setPixelFormat(PIXEL_FORMAT nFormat);
00063
00072 virtual bool loadCameraFile(const char* nCamParamFile, ARFloat nNearClip, ARFloat nFarClip);
00073
00081 virtual void setLoadUndistLUT(bool nSet) {
00082 loadCachedUndist = nSet;
00083 }
00084
00086 virtual int arDetectMarker(uint8_t *dataPtr, int thresh, ARMarkerInfo **marker_info, int *marker_num);
00087
00089 virtual int arDetectMarkerLite(uint8_t *dataPtr, int thresh, ARMarkerInfo **marker_info, int *marker_num);
00090
00092 virtual ARFloat arMultiGetTransMat(ARMarkerInfo *marker_info, int marker_num, ARMultiMarkerInfoT *config);
00093
00094 virtual ARFloat arMultiGetTransMatHull(ARMarkerInfo *marker_info, int marker_num, ARMultiMarkerInfoT *config);
00095
00097 virtual ARFloat arGetTransMat(ARMarkerInfo *marker_info, ARFloat center[2], ARFloat width, ARFloat conv[3][4]);
00098
00099 virtual ARFloat arGetTransMatCont(ARMarkerInfo *marker_info, ARFloat prev_conv[3][4], ARFloat center[2],
00100 ARFloat width, ARFloat conv[3][4]);
00101
00102
00103 virtual ARFloat rppMultiGetTransMat(ARMarkerInfo *marker_info, int marker_num, ARMultiMarkerInfoT *config);
00104 virtual ARFloat rppGetTransMat(ARMarkerInfo *marker_info, ARFloat center[2], ARFloat width, ARFloat conv[3][4]);
00105
00107 virtual int arLoadPatt(char *filename);
00108
00110 virtual int arFreePatt(int patno);
00111
00112 virtual int arMultiFreeConfig(ARMultiMarkerInfoT *config);
00113
00114 virtual ARMultiMarkerInfoT *arMultiReadConfigFile(const char *filename);
00115
00120 virtual void activateBinaryMarker(int nThreshold) {
00121 binaryMarkerThreshold = nThreshold;
00122 }
00123
00133 virtual void setMarkerMode(MARKER_MODE nMarkerMode);
00134
00147 virtual void activateVignettingCompensation(bool nEnable, int nCorners = 0, int nLeftRight = 0, int nTopBottom = 0);
00148
00155 static bool calcCameraMatrix(const char* nCamParamFile, ARFloat nNear, ARFloat nFar, ARFloat *nMatrix);
00156
00158 virtual void changeCameraSize(int nWidth, int nHeight);
00159
00165 virtual void setUndistortionMode(UNDIST_MODE nMode);
00166
00173 virtual bool setPoseEstimator(POSE_ESTIMATOR nMethod);
00174
00186 virtual void setHullMode(HULL_TRACKING_MODE nMode) {
00187 hullTrackingMode = nMode;
00188 }
00189
00197 virtual void setBorderWidth(ARFloat nFraction) {
00198 relBorderWidth = nFraction;
00199 }
00200
00202 virtual void setThreshold(int nValue) {
00203 thresh = nValue;
00204 }
00205
00207 virtual int getThreshold() const {
00208 return thresh;
00209 }
00210
00212 virtual void activateAutoThreshold(bool nEnable) {
00213 autoThreshold.enable = nEnable;
00214 }
00215
00217 virtual bool isAutoThresholdActivated() const {
00218 return autoThreshold.enable;
00219 }
00220
00231 virtual void setNumAutoThresholdRetries(int nNumRetries) {
00232 autoThreshold.numRandomRetries = nNumRetries >= 1 ? nNumRetries : 1;
00233 }
00234
00241 virtual void setImageProcessingMode(IMAGE_PROC_MODE nMode) {
00242 arImageProcMode = (nMode == IMAGE_HALF_RES ? AR_IMAGE_PROC_IN_HALF : AR_IMAGE_PROC_IN_FULL);
00243 }
00244
00246 virtual const ARFloat* getModelViewMatrix() const {
00247 return gl_para;
00248 }
00249
00251 virtual const ARFloat* getProjectionMatrix() const {
00252 return gl_cpara;
00253 }
00254
00256 virtual PIXEL_FORMAT getPixelFormat() const {
00257 return static_cast<PIXEL_FORMAT> (pixelFormat);
00258 }
00259
00261 virtual int getBitsPerPixel() const {
00262 return pixelSize * 8;
00263 }
00264
00270 virtual int getNumLoadablePatterns() const {
00271 return MAX_LOAD_PATTERNS;
00272 }
00273
00275 virtual Camera* getCamera() {
00276 return arCamera;
00277 }
00278
00280 virtual void setCamera(Camera* nCamera);
00281
00283 virtual void setCamera(Camera* nCamera, ARFloat nNearClip, ARFloat nFarClip);
00284
00286 virtual ARFloat calcOpenGLMatrixFromMarker(ARMarkerInfo* nMarkerInfo, ARFloat nPatternCenter[2],
00287 ARFloat nPatternSize, ARFloat *nOpenGLMatrix);
00288
00290 virtual ARFloat executeSingleMarkerPoseEstimator(ARMarkerInfo *marker_info, ARFloat center[2], ARFloat width,
00291 ARFloat conv[3][4]);
00292
00294 virtual ARFloat executeMultiMarkerPoseEstimator(ARMarkerInfo *marker_info, int marker_num,
00295 ARMultiMarkerInfoT *config);
00296
00297
00298
00299
00300
00301 virtual const CornerPoints& getTrackedCorners() const {
00302 return trackedCorners;
00303 }
00304
00305 protected:
00306 #ifdef SMALL_LUM8_TABLE
00307 static const int LUM_TABLE_SIZE = (0xffff >> 6) + 1;
00308 #else
00309 static const int LUM_TABLE_SIZE = 0xffff + 1;
00310 #endif
00311 const int PATTERN_WIDTH;
00312 const int PATTERN_HEIGHT;
00313 const int PATTERN_SAMPLE_NUM;
00314 const int MAX_LOAD_PATTERNS;
00315 const int MAX_IMAGE_PATTERNS;
00316 const int WORK_SIZE;
00317
00318 bool checkPixelFormat();
00319
00320 void checkImageBuffer();
00321
00322
00323 void convertTransformationMatrixToOpenGLStyle(ARFloat para[3][4], ARFloat gl_para[16]);
00324
00325
00326 static bool convertProjectionMatrixToOpenGLStyle(Camera *param, ARFloat gnear, ARFloat gfar, ARFloat m[16]);
00327 static bool convertProjectionMatrixToOpenGLStyle2(ARFloat cparam[3][4], int width, int height, ARFloat gnear,
00328 ARFloat gfar, ARFloat m[16]);
00329
00330 ARMarkerInfo2* arDetectMarker2(int16_t *limage, int label_num, int *label_ref, int *warea, ARFloat *wpos,
00331 int *wclip, int area_max, int area_min, ARFloat factor, int *marker_num);
00332
00333 int arGetContour(int16_t *limage, int *label_ref, int label, int clip[4], ARMarkerInfo2 *marker_infoTWO);
00334
00335 int check_square(int area, ARMarkerInfo2 *marker_infoTWO, ARFloat factor);
00336
00337 int
00338 arGetCode(uint8_t *image, int *x_coord, int *y_coord, int *vertex, int *code, int *dir, ARFloat *cf, int thresh);
00339
00340 int arGetPatt(uint8_t *image, int *x_coord, int *y_coord, int *vertex, uint8_t *ext_pat);
00341
00342 int pattern_match(uint8_t *data, int *code, int *dir, ARFloat *cf);
00343
00344 int downsamplePattern(uint8_t* data, unsigned char* imgPtr);
00345
00346 int bitfield_check_simple(uint8_t *data, int *code, int *dir, ARFloat *cf, int thresh);
00347
00348 int bitfield_check_BCH(uint8_t *data, int *code, int *dir, ARFloat *cf, int thresh);
00349
00350 void gen_evec(void);
00351
00352 ARMarkerInfo* arGetMarkerInfo(uint8_t *image, ARMarkerInfo2 *marker_info2, int *marker_num, int thresh);
00353
00354 ARFloat arGetTransMat5(ARFloat rot[3][3], ARFloat ppos2d[][2], ARFloat ppos3d[][3], int num, ARFloat conv[3][4],
00355 Camera *pCam);
00356
00357 ARFloat arGetTransMatSub(ARFloat rot[3][3], ARFloat ppos2d[][2], ARFloat pos3d[][3], int num, ARFloat conv[3][4],
00358 Camera *pCam);
00359
00360 ARFloat arModifyMatrix(ARFloat rot[3][3], ARFloat trans[3], ARFloat cpara[3][4], ARFloat vertex[][3],
00361 ARFloat pos2d[][2], int num);
00362
00363 ARFloat arModifyMatrix2(ARFloat rot[3][3], ARFloat trans[3], ARFloat cpara[3][4], ARFloat vertex[][3],
00364 ARFloat pos2d[][2], int num);
00365
00366 int arGetAngle(ARFloat rot[3][3], ARFloat *wa, ARFloat *wb, ARFloat *wc);
00367
00368 int arGetRot(ARFloat a, ARFloat b, ARFloat c, ARFloat rot[3][3]);
00369
00370 int arGetNewMatrix(ARFloat a, ARFloat b, ARFloat c, ARFloat trans[3], ARFloat trans2[3][4], ARFloat cpara[3][4],
00371 ARFloat ret[3][4]);
00372
00373 int arGetInitRot(ARMarkerInfo *marker_info, ARFloat cpara[3][4], ARFloat rot[3][3]);
00374
00375 int arGetInitRot2(ARMarkerInfo *marker_info, ARFloat rot[3][3], ARFloat center[2], ARFloat width);
00376
00377 ARFloat arGetTransMatContSub(ARMarkerInfo *marker_info, ARFloat prev_conv[3][4], ARFloat center[2], ARFloat width,
00378 ARFloat conv[3][4]);
00379
00380 int16_t* arLabeling(uint8_t *image, int thresh, int *label_num, int **area, ARFloat **pos, int **clip,
00381 int **label_ref);
00382
00383 int16_t* arLabeling_ABGR(uint8_t *image, int thresh, int *label_num, int **area, ARFloat **pos, int **clip,
00384 int **label_ref);
00385 int16_t* arLabeling_BGR(uint8_t *image, int thresh, int *label_num, int **area, ARFloat **pos, int **clip,
00386 int **label_ref);
00387 int16_t* arLabeling_RGB(uint8_t *image, int thresh, int *label_num, int **area, ARFloat **pos, int **clip,
00388 int **label_ref);
00389 int16_t* arLabeling_RGB565(uint8_t *image, int thresh, int *label_num, int **area, ARFloat **pos, int **clip,
00390 int **label_ref);
00391 int16_t* arLabeling_LUM(uint8_t *image, int thresh, int *label_num, int **area, ARFloat **pos, int **clip,
00392 int **label_ref);
00393
00394 int arActivatePatt(int patno);
00395
00396 int arDeactivatePatt(int patno);
00397
00398 int arMultiActivate(ARMultiMarkerInfoT *config);
00399
00400 int arMultiDeactivate(ARMultiMarkerInfoT *config);
00401
00402 int verify_markers(ARMarkerInfo *marker_info, int marker_num, ARMultiMarkerInfoT *config);
00403
00404 int arInitCparam(Camera *pCam);
00405
00406 int arGetLine(int x_coord[], int y_coord[], int vertex[], ARFloat line[4][3], ARFloat v[4][2]);
00407
00408 static int arUtilMatMul(ARFloat s1[3][4], ARFloat s2[3][4], ARFloat d[3][4]);
00409
00410 static int arUtilMatInv(ARFloat s[3][4], ARFloat d[3][4]);
00411
00412 static int arMatrixPCA(ARMat *input, ARMat *evec, ARVec *ev, ARVec *mean);
00413
00414 static int arMatrixPCA2(ARMat *input, ARMat *evec, ARVec *ev);
00415
00416 static int arCameraDecomp(Camera *source, Camera *icpara, ARFloat trans[3][4]);
00417
00418 static int arCameraDecompMat(ARFloat source[3][4], ARFloat cpara[3][4], ARFloat trans[3][4]);
00419
00420 int arCameraObserv2Ideal_none(Camera* pCam, ARFloat ox, ARFloat oy, ARFloat *ix, ARFloat *iy);
00421
00422 int arCameraObserv2Ideal_LUT(Camera* pCam, ARFloat ox, ARFloat oy, ARFloat *ix, ARFloat *iy);
00423
00424 int arCameraObserv2Ideal_std(Camera* pCam, ARFloat ox, ARFloat oy, ARFloat *ix, ARFloat *iy);
00425 int arCameraIdeal2Observ_std(Camera* pCam, ARFloat ix, ARFloat iy, ARFloat *ox, ARFloat *oy);
00426
00427 typedef int (Tracker::* ARPARAM_UNDIST_FUNC)(Camera* pCam, ARFloat ox, ARFloat oy, ARFloat *ix, ARFloat *iy);
00428
00429 typedef ARFloat (Tracker::* POSE_ESTIMATOR_FUNC)(ARMarkerInfo *marker_info, ARFloat center[2], ARFloat width,
00430 ARFloat conv[3][4]);
00431 typedef ARFloat (Tracker::* MULTI_POSE_ESTIMATOR_FUNC)(ARMarkerInfo *marker_info, int marker_num,
00432 ARMultiMarkerInfoT *config);
00433
00434 void buildUndistO2ITable(Camera* pCam);
00435
00436 void checkRGB565LUT();
00437
00438
00439 void setFittingMode(int nWhich) {
00440 arFittingMode = nWhich;
00441 }
00442
00443 ARFloat arGetTransMat3(ARFloat rot[3][3], ARFloat ppos2d[][2], ARFloat ppos3d[][2], int num, ARFloat conv[3][4],
00444 Camera *pCam);
00445
00446 static int arCameraObserv2Ideal(Camera *pCam, ARFloat ox, ARFloat oy, ARFloat *ix, ARFloat *iy);
00447 static int arCameraIdeal2Observ(Camera *pCam, ARFloat ix, ARFloat iy, ARFloat *ox, ARFloat *oy);
00448
00449 protected:
00450 struct AutoThreshold {
00451 enum {
00452 MINLUM0 = 255, MAXLUM0 = 0
00453 };
00454
00455 void reset() {
00456 minLum = MINLUM0;
00457 maxLum = MAXLUM0;
00458 }
00459
00460 void addValue(int nRed, int nGreen, int nBlue, int nPixelFormat) {
00461 int lum;
00462
00463
00464 if (nPixelFormat == PIXEL_FORMAT_RGB565 || nPixelFormat == PIXEL_FORMAT_LUM)
00465 lum = nRed;
00466 else
00467 lum = (nRed + (nGreen << 1) + nBlue) >> 2;
00468
00469 if (lum < minLum)
00470 minLum = lum;
00471 if (lum > maxLum)
00472 maxLum = lum;
00473 }
00474
00475 int calc() {
00476 return (minLum + maxLum) / 2;
00477 }
00478
00479 bool enable;
00480 int minLum, maxLum;
00481 int numRandomRetries;
00482 } autoThreshold;
00483
00484 PIXEL_FORMAT pixelFormat;
00485 int pixelSize;
00486
00487 int binaryMarkerThreshold;
00488
00489
00490 ARMarkerInfo2 *marker_info2;
00491 ARMarkerInfo *wmarker_info;
00492 int wmarker_num;
00493
00494 arPrevInfo *prev_info;
00495 int prev_num;
00496
00497 std::vector<std::vector<arPrevInfo> > sprev_info;
00498 int sprev_num[2];
00499
00500
00501 ARMarkerInfo2 *marker_infoTWO;
00502
00503
00504 int arGetContour_wx[AR_CHAIN_MAX];
00505 int arGetContour_wy[AR_CHAIN_MAX];
00506
00507
00508 int pattern_num;
00509 int *patf;
00510 std::vector<std::vector<std::vector<int> > > pat;
00511 ARFloat (*patpow)[4];
00512 std::vector<std::vector<std::vector<int> > > patBW;
00513 ARFloat (*patpowBW)[4];
00514
00515 std::vector<std::vector<ARFloat> > evec;
00516 ARFloat (*epat)[4][EVEC_MAX];
00517 int evec_dim;
00518 int evecf;
00519 std::vector<std::vector<ARFloat> > evecBW;
00520 ARFloat (*epatBW)[4][EVEC_MAX];
00521 int evec_dimBW;
00522 int evecBWf;
00523
00524
00525
00526 ARMarkerInfo *marker_infoL;
00527
00528
00529
00530 ARFloat pos2d[P_MAX][2];
00531 ARFloat pos3d[P_MAX][3];
00532
00533
00534
00535 int16_t *l_imageL;
00536 int16_t *l_imageR;
00537 int l_imageL_size;
00538
00539 int *workL;
00540 int *work2L;
00541
00542 int *workR;
00543 int *work2R;
00544 int *wareaR;
00545 int *wclipR;
00546 ARFloat *wposR;
00547
00548 int wlabel_numL;
00549 int wlabel_numR;
00550 int *wareaL;
00551 int *wclipL;
00552 ARFloat *wposL;
00553
00554 int arFittingMode;
00555 int arImageProcMode;
00556 Camera *arCamera;
00557 bool loadCachedUndist;
00558 int arImXsize, arImYsize;
00559 int arTemplateMatchingMode;
00560 int arMatchingPCAMode;
00561
00562 uint8_t* arImageL;
00563
00564 MARKER_MODE markerMode;
00565
00566 unsigned char *RGB565_to_LUM8_LUT;
00567
00568
00569
00570 UNDIST_MODE undistMode;
00571 unsigned int *undistO2ITable;
00572
00573
00574 MarkerPoint hullInPoints[MAX_HULL_POINTS];
00575 MarkerPoint hullOutPoints[MAX_HULL_POINTS];
00576
00577 CornerPoints trackedCorners;
00578
00579 ARFloat relBorderWidth;
00580
00581 ARPARAM_UNDIST_FUNC arCameraObserv2Ideal_func;
00582
00583
00584 POSE_ESTIMATOR poseEstimator;
00585
00586 HULL_TRACKING_MODE hullTrackingMode;
00587
00588 static int screenWidth;
00589 static int screenHeight;
00590 int thresh;
00591
00592 ARFloat gl_para[16];
00593 ARFloat gl_cpara[16];
00594
00595 char *descriptionString;
00596
00597 struct {
00598 bool enabled;
00599 int corners, leftright, bottomtop;
00600 } vignetting;
00601
00602 unsigned short *DIV_TABLE;
00603
00604 BCH *bchProcessor;
00605 };
00606
00607 }
00608
00609 #endif //__ARTOOLKIT_TRACKERIMPL_HEADERFILE__