25#ifndef __BTK_PREREQUISITES_H__ 
   26#define __BTK_PREREQUISITES_H__ 
   28#include <scolPlugin.h> 
   29#include <scolMemoryHelper.hpp> 
   35#include <opencv2/opencv.hpp> 
   40#include <boost/filesystem/operations.hpp> 
   43#pragma warning(disable : 4290) 
   46#define SCOL_ML_TRAINING_FINISHED_CB 0 
   49#define SCOL_ML_DETECTION_CB 1 
   67#pragma warning(disable : 4290) 
   92    return sqrtf(
x*
x + 
y * 
y);
 
 
   98    if (magnitude == 0.0f)
 
  100    return Vector2(
x / magnitude, 
y / magnitude);
 
 
  105    return x * other.
x + 
y * other.
y;
 
 
 
  197    return sqrtf(
x*
x + 
y * 
y + 
z * 
z);
 
 
  203    if (magnitude == 0.0f)
 
  204      return Vector3(0.0f, 0.0f, 0.0f);
 
  205    return Vector3(
x / magnitude, 
y / magnitude, 
z / magnitude);
 
 
  210    return x * other.
x + 
y * other.
y + 
z * other.
z;
 
 
  215    return Vector3(
y * other.
z - 
z * other.
y, 
z * other.
x - 
x * other.
z, 
x * other.
y - 
y * other.
x);
 
 
  237    return Vector3(
x * scalar, 
y * scalar, 
z * scalar);
 
 
  242    return Vector3(
x / scalar, 
y / scalar, 
z / scalar);
 
 
 
  269  return Vector3(u.
x * scalar, u.
y * scalar, u.
z * scalar);
 
 
  273  return Vector3(u.
x * scalar, u.
y * scalar, u.
z * scalar);
 
 
  277  return Vector3(u.
x / scalar, u.
y / scalar, u.
z / scalar);
 
 
  323    return (rhs.
x == 
x) && (rhs.
y == 
y) &&
 
  324      (rhs.
z == 
z) && (rhs.
w == 
w);
 
 
  351      w * rkQ.
w - 
x * rkQ.
x - 
y * rkQ.
y - 
z * rkQ.
z,
 
  352      w * rkQ.
x + 
x * rkQ.
w + 
y * rkQ.
z - 
z * rkQ.
y,
 
  353      w * rkQ.
y + 
y * rkQ.
w + 
z * rkQ.
x - 
x * rkQ.
z,
 
  354      w * rkQ.
z + 
z * rkQ.
w + 
x * rkQ.
y - 
y * rkQ.
x 
 
  370    float fNorm = 
w * 
w + 
x * 
x + 
y * 
y + 
z * 
z;
 
  373      float fInvNorm = 1.0f / fNorm;
 
  374      return BtQuaternion(
w*fInvNorm, -
x * fInvNorm, -
y * fInvNorm, -
z * fInvNorm);
 
 
  403    kRot[0][0] = rotMatrix[0];
 
  404    kRot[0][1] = rotMatrix[4];
 
  405    kRot[0][2] = rotMatrix[8];
 
  409      kRot[1][0] = -rotMatrix[1];
 
  410      kRot[1][1] = -rotMatrix[5];
 
  411      kRot[1][2] = -rotMatrix[9];
 
  415      kRot[1][0] = rotMatrix[1];
 
  416      kRot[1][1] = rotMatrix[5];
 
  417      kRot[1][2] = rotMatrix[9];
 
  420    kRot[2][0] = rotMatrix[2];
 
  421    kRot[2][1] = rotMatrix[6];
 
  422    kRot[2][2] = rotMatrix[10];
 
  426    double fTrace = kRot[0][0] + kRot[1][1] + kRot[2][2];
 
  432      fRoot = sqrt(fTrace + 1.0);  
 
  433      mQuat.
w = 
static_cast<float>(0.5*fRoot);
 
  435      mQuat.
x = 
static_cast<float>((kRot[2][1] - kRot[1][2])*fRoot);
 
  436      mQuat.
y = 
static_cast<float>((kRot[0][2] - kRot[2][0])*fRoot);
 
  437      mQuat.
z = 
static_cast<float>((kRot[1][0] - kRot[0][1])*fRoot);
 
  442      static size_t s_iNext[3] = { 1, 2, 0 };
 
  444      if (kRot[1][1] > kRot[0][0])
 
  446      if (kRot[2][2] > kRot[i][i])
 
  449      size_t j = s_iNext[i];
 
  450      size_t k = s_iNext[j];
 
  452      fRoot = sqrt(kRot[i][i] - kRot[j][j] - kRot[k][k] + 1.0);
 
  453      float* apkQuat[3] = { &mQuat.
x, &mQuat.
y, &mQuat.
z };
 
  454      *apkQuat[i] = 
static_cast<float>(0.5*fRoot);
 
  456      mQuat.
w = 
static_cast<float>((kRot[k][j] - kRot[j][k])*fRoot);
 
  457      *apkQuat[j] = 
static_cast<float>((kRot[j][i] + kRot[i][j])*fRoot);
 
  458      *apkQuat[k] = 
static_cast<float>((kRot[k][i] + kRot[i][k])*fRoot);
 
  468    float len = mQuat.
w*mQuat.
w + mQuat.
x*mQuat.
x + mQuat.
y*mQuat.
y + mQuat.
z*mQuat.
z;
 
  469    float factor = 1.0f / sqrt(len);
 
 
  480    return w * rkQ.
w + 
x * rkQ.
x + 
y * rkQ.
y + 
z * rkQ.
z;
 
 
  488    *
this = 1.0f / len * *
this;
 
 
  495    float fCos = rkP.
Dot(rkQ);
 
  499    if (fCos < 0.0f && shortestPath)
 
  509    if (fabs(fCos) < 1 - 1e-03)
 
  512      float fSin = sqrt(1 - (fCos * fCos));
 
  513      float fAngle = std::atan2(fSin, fCos);
 
  514      float fInvSin = 1.0f / fSin;
 
  515      float fCoeff0 = std::sin((1.0f - fT) * fAngle) * fInvSin;
 
  516      float fCoeff1 = std::sin(fT * fAngle) * fInvSin;
 
  517      return fCoeff0 * rkP + fCoeff1 * rkT;
 
 
 
  551  std::vector<cv::Point2f> m_filteredPoints;
 
  552  std::vector<cv::Point2f> m_prevPoints;
 
  553  std::vector<cv::Point2f> m_prevfilteredPoints;
 
  554  std::vector<cv::Point2f> m_pointsTrend;
 
  558  float m_fJitterRadius;
 
  559  float m_fMaxDeviationRadius;
 
  566  void Init(
float fSmoothing = 0.25f, 
float fCorrection = 0.25f, 
float fPrediction = 0.25f, 
float fJitterRadius = 0.03f, 
float fMaxDeviationRadius = 0.05f)
 
  568    m_fMaxDeviationRadius = fMaxDeviationRadius; 
 
  569    m_fSmoothing = fSmoothing;                   
 
  570    m_fCorrection = fCorrection;                 
 
  571    m_fPrediction = fPrediction;                 
 
  572    m_fJitterRadius = fJitterRadius;             
 
 
  584    m_filteredPoints.clear();
 
  585    m_prevPoints.clear();
 
  586    m_prevfilteredPoints.clear();
 
  587    m_pointsTrend.clear();
 
  588    for (
unsigned int k = 0; k < 4; ++k)
 
  590      m_filteredPoints.push_back(cv::Point2f(0.0f, 0.0f));
 
  591      m_prevPoints.push_back(cv::Point2f(0.0f, 0.0f));
 
  592      m_prevfilteredPoints.push_back(cv::Point2f(0.0f, 0.0f));
 
  593      m_pointsTrend.push_back(cv::Point2f(0.0f, 0.0f));
 
 
  597  void Update(std::vector<cv::Point2f> points)
 
  599    cv::Point2f diffPoints;
 
  600    std::vector<cv::Point2f> trendPoints;
 
  603    for (
unsigned int k = 0; k < 4; ++k)
 
  605      trendPoints.push_back(cv::Point2f(0.0f, 0.0f));
 
  609    if (m_dwFrameCount == 0)
 
  611      for (
unsigned int i = 0; i < 4; i++)
 
  613        m_filteredPoints[i] = points[i];
 
  617    else if (m_dwFrameCount == 1)
 
  619      for (
unsigned int i = 0; i < 4; i++)
 
  621        m_filteredPoints[i] = points[i] + m_prevPoints[i];
 
  622        m_filteredPoints[i].x *= 0.5f;
 
  623        m_filteredPoints[i].y *= 0.5f;
 
  625        diffPoints = m_filteredPoints[i] - m_prevfilteredPoints[i];
 
  627        cv::Point2f cdiff = diffPoints;
 
  628        cdiff.x *= m_fCorrection;
 
  629        cdiff.y *= m_fCorrection;
 
  631        cv::Point2f pdiff = m_pointsTrend[i];
 
  632        pdiff.x *= 1.0f - m_fCorrection;
 
  633        pdiff.y *= 1.0f - m_fCorrection;
 
  634        trendPoints[i] = cdiff + pdiff;
 
  641      for (
unsigned int i = 0; i < 4; i++)
 
  643        diffPoints = points[i] - m_prevfilteredPoints[i];
 
  645        fDiff = fabs(sqrt(diffPoints.x * diffPoints.x + diffPoints.y * diffPoints.y));
 
  646        if (fDiff <= m_fJitterRadius)
 
  648          diffPoints = m_filteredPoints[i] - m_prevfilteredPoints[i];
 
  650          cv::Point2f cVal = points[i];
 
  651          cVal.x *= fDiff / m_fJitterRadius;
 
  652          cVal.y *= fDiff / m_fJitterRadius;
 
  654          cv::Point2f pVal = m_prevfilteredPoints[i];
 
  655          pVal.x *= 1.0f - fDiff / m_fJitterRadius;
 
  656          pVal.y *= 1.0f - fDiff / m_fJitterRadius;
 
  657          m_filteredPoints[i] = cVal + pVal;
 
  661          m_filteredPoints[i] = points[i];
 
  665        cv::Point2f cVal = m_filteredPoints[i];
 
  666        cVal.x *= 1.0f - m_fSmoothing;
 
  667        cVal.y *= 1.0f - m_fSmoothing;
 
  669        cv::Point2f pVal = m_prevfilteredPoints[i] + m_pointsTrend[i];
 
  670        pVal.x *= m_fSmoothing;
 
  671        pVal.y *= m_fSmoothing;
 
  672        m_filteredPoints[i] = cVal + pVal;
 
  674        diffPoints = m_filteredPoints[i] - m_prevfilteredPoints[i];
 
  677        cVal.x *= m_fCorrection;
 
  678        cVal.y *= m_fCorrection;
 
  680        pVal = m_pointsTrend[i];
 
  681        pVal.x *= 1.0f - m_fCorrection;
 
  682        pVal.y *= 1.0f - m_fCorrection;
 
  684        trendPoints[i] = cVal + pVal;
 
  687        cVal = trendPoints[i];
 
  688        cVal.x *= m_fPrediction;
 
  689        cVal.y *= m_fPrediction;
 
  691        cv::Point2f predictedPoint = m_filteredPoints[i] + cVal;
 
  694        diffPoints = predictedPoint - points[i];
 
  695        fDiff = fabs(sqrt(diffPoints.x * diffPoints.x + diffPoints.y * diffPoints.y));
 
  697        if (fDiff > m_fMaxDeviationRadius)
 
  699          cVal = predictedPoint;
 
  700          cVal.x *= m_fMaxDeviationRadius / fDiff;
 
  701          cVal.y *= m_fMaxDeviationRadius / fDiff;
 
  704          pVal.x *= 1.0f - m_fMaxDeviationRadius / fDiff;
 
  705          pVal.y *= 1.0f - m_fMaxDeviationRadius / fDiff;
 
  707          predictedPoint = cVal + pVal;
 
  710        m_filteredPoints[i] = predictedPoint;
 
  714    for (
unsigned int i = 0; i < 4; i++)
 
  716      m_prevfilteredPoints[i] = m_filteredPoints[i];
 
  717      m_prevPoints[i] = points[i];
 
  718      m_pointsTrend[i] = trendPoints[i];
 
 
  724    return m_filteredPoints;
 
 
 
  732  std::vector<double> m_filteredPoints;
 
  733  std::vector<double> m_prevPoints;
 
  734  std::vector<double> m_prevfilteredPoints;
 
  735  std::vector<double> m_pointsTrend;
 
  739  float m_fJitterRadius;
 
  740  float m_fMaxDeviationRadius;
 
  747  void Init(
float fSmoothing = 0.25f, 
float fCorrection = 0.25f, 
float fPrediction = 0.25f, 
float fJitterRadius = 0.03f, 
float fMaxDeviationRadius = 0.05f)
 
  749    m_fMaxDeviationRadius = fMaxDeviationRadius; 
 
  750    m_fSmoothing = fSmoothing;                   
 
  751    m_fCorrection = fCorrection;                 
 
  752    m_fPrediction = fPrediction;                 
 
  753    m_fJitterRadius = fJitterRadius;             
 
 
  765    m_filteredPoints.clear();
 
  766    m_prevPoints.clear();
 
  767    m_prevfilteredPoints.clear();
 
  768    m_pointsTrend.clear();
 
 
  774    std::vector<double> trendPoints;
 
  777    if (points.size() != m_filteredPoints.size())
 
  779      m_filteredPoints = points;
 
  780      m_prevPoints = points;
 
  781      m_prevfilteredPoints = points;
 
  782      m_pointsTrend = points;
 
  793    for (
unsigned int k = 0; k < points.size(); ++k)
 
  795      trendPoints.push_back(0.0);
 
  799    if (m_dwFrameCount == 0)
 
  801      for (
unsigned int i = 0; i < points.size(); i++)
 
  803        m_filteredPoints[i] = points[i];
 
  807    else if (m_dwFrameCount == 1)
 
  809      for (
unsigned int i = 0; i < points.size(); i++)
 
  811        m_filteredPoints[i] = points[i] + m_prevPoints[i];
 
  812        m_filteredPoints[i] *= 0.5f;
 
  814        diffPoints = m_filteredPoints[i] - m_prevfilteredPoints[i];
 
  816        double cdiff = diffPoints;
 
  817        cdiff *= m_fCorrection;
 
  819        double pdiff = m_pointsTrend[i];
 
  820        pdiff *= 1.0f - m_fCorrection;
 
  821        trendPoints[i] = cdiff + pdiff;
 
  828      for (
unsigned int i = 0; i < points.size(); i++)
 
  830        diffPoints = points[i] - m_prevfilteredPoints[i];
 
  832        fDiff = fabs(sqrt(diffPoints * diffPoints));
 
  833        if (fDiff <= m_fJitterRadius)
 
  835          diffPoints = m_filteredPoints[i] - m_prevfilteredPoints[i];
 
  837          double cVal = points[i];
 
  838          cVal *= fDiff / m_fJitterRadius;
 
  840          double pVal = m_prevfilteredPoints[i];
 
  841          pVal *= 1.0f - fDiff / m_fJitterRadius;
 
  842          m_filteredPoints[i] = cVal + pVal;
 
  846          m_filteredPoints[i] = points[i];
 
  850        double cVal = m_filteredPoints[i];
 
  851        cVal *= 1.0f - m_fSmoothing;
 
  853        double pVal = m_prevfilteredPoints[i] + m_pointsTrend[i];
 
  854        pVal *= m_fSmoothing;
 
  856        m_filteredPoints[i] = cVal + pVal;
 
  858        diffPoints = m_filteredPoints[i] - m_prevfilteredPoints[i];
 
  861        cVal *= m_fCorrection;
 
  863        pVal = m_pointsTrend[i];
 
  864        pVal *= 1.0f - m_fCorrection;
 
  866        trendPoints[i] = cVal + pVal;
 
  869        cVal = trendPoints[i];
 
  870        cVal *= m_fPrediction;
 
  872        double predictedPoint = m_filteredPoints[i] + cVal;
 
  875        diffPoints = predictedPoint - points[i];
 
  876        fDiff = fabs(sqrt(diffPoints * diffPoints));
 
  878        if (fDiff > m_fMaxDeviationRadius)
 
  880          cVal = predictedPoint;
 
  881          cVal *= m_fMaxDeviationRadius / fDiff;
 
  884          pVal *= 1.0f - m_fMaxDeviationRadius / fDiff;
 
  886          predictedPoint = cVal + pVal;
 
  889        m_filteredPoints[i] = predictedPoint;
 
  893    for (
unsigned int i = 0; i < points.size(); i++)
 
  895      m_prevfilteredPoints[i] = m_filteredPoints[i];
 
  896      m_prevPoints[i] = points[i];
 
  897      m_pointsTrend[i] = trendPoints[i];
 
 
  903    return m_filteredPoints;
 
 
 
  911  std::vector<cv::Point3f> m_filteredPoints;
 
  912  std::vector<cv::Point3f> m_prevPoints;
 
  913  std::vector<cv::Point3f> m_prevfilteredPoints;
 
  914  std::vector<cv::Point3f> m_pointsTrend;
 
  918  float m_fJitterRadius;
 
  919  float m_fMaxDeviationRadius;
 
  926  void Init(
float fSmoothing = 0.25f, 
float fCorrection = 0.25f, 
float fPrediction = 0.25f, 
float fJitterRadius = 0.03f, 
float fMaxDeviationRadius = 0.05f)
 
  928    m_fMaxDeviationRadius = fMaxDeviationRadius; 
 
  929    m_fSmoothing = fSmoothing;                   
 
  930    m_fCorrection = fCorrection;                 
 
  931    m_fPrediction = fPrediction;                 
 
  932    m_fJitterRadius = fJitterRadius;             
 
 
  944    m_filteredPoints.clear();
 
  945    m_prevPoints.clear();
 
  946    m_prevfilteredPoints.clear();
 
  947    m_pointsTrend.clear();
 
  948    for (
unsigned int k = 0; k < 2; ++k)
 
  950      m_filteredPoints.push_back(cv::Point3f(0.0f, 0.0f, 0.0f));
 
  951      m_prevPoints.push_back(cv::Point3f(0.0f, 0.0f, 0.0f));
 
  952      m_prevfilteredPoints.push_back(cv::Point3f(0.0f, 0.0f, 0.0f));
 
  953      m_pointsTrend.push_back(cv::Point3f(0.0f, 0.0f, 0.0f));
 
 
  957  void Update(std::vector<cv::Point3f> points)
 
  959    cv::Point3f diffPoints;
 
  960    std::vector<cv::Point3f> trendPoints;
 
  963    for (
unsigned int k = 0; k < 2; ++k)
 
  965      trendPoints.push_back(cv::Point3f(0.0f, 0.0f, 0.0f));
 
  969    if (m_dwFrameCount == 0)
 
  971      for (
unsigned int i = 0; i < 2; i++)
 
  973        m_filteredPoints[i] = points[i];
 
  977    else if (m_dwFrameCount == 1)
 
  979      for (
unsigned int i = 0; i < 2; i++)
 
  981        m_filteredPoints[i] = points[i] + m_prevPoints[i];
 
  982        m_filteredPoints[i].x *= 0.5f;
 
  983        m_filteredPoints[i].y *= 0.5f;
 
  984        m_filteredPoints[i].z *= 0.5f;
 
  986        diffPoints = m_filteredPoints[i] - m_prevfilteredPoints[i];
 
  988        cv::Point3f cdiff = diffPoints;
 
  989        cdiff.x *= m_fCorrection;
 
  990        cdiff.y *= m_fCorrection;
 
  991        cdiff.z *= m_fCorrection;
 
  993        cv::Point3f pdiff = m_pointsTrend[i];
 
  994        pdiff.x *= 1.0f - m_fCorrection;
 
  995        pdiff.y *= 1.0f - m_fCorrection;
 
  996        pdiff.z *= 1.0f - m_fCorrection;
 
  997        trendPoints[i] = cdiff + pdiff;
 
 1004      for (
unsigned int i = 0; i < 2; i++)
 
 1006        diffPoints = points[i] - m_prevfilteredPoints[i];
 
 1008        fDiff = fabs(sqrt(diffPoints.x * diffPoints.x + diffPoints.y * diffPoints.y));
 
 1009        if (fDiff <= m_fJitterRadius)
 
 1011          diffPoints = m_filteredPoints[i] - m_prevfilteredPoints[i];
 
 1013          cv::Point3f cVal = points[i];
 
 1014          cVal.x *= fDiff / m_fJitterRadius;
 
 1015          cVal.y *= fDiff / m_fJitterRadius;
 
 1016          cVal.z *= fDiff / m_fJitterRadius;
 
 1018          cv::Point3f pVal = m_prevfilteredPoints[i];
 
 1019          pVal.x *= 1.0f - fDiff / m_fJitterRadius;
 
 1020          pVal.y *= 1.0f - fDiff / m_fJitterRadius;
 
 1021          pVal.z *= 1.0f - fDiff / m_fJitterRadius;
 
 1022          m_filteredPoints[i] = cVal + pVal;
 
 1026          m_filteredPoints[i] = points[i];
 
 1030        cv::Point3f cVal = m_filteredPoints[i];
 
 1031        cVal.x *= 1.0f - m_fSmoothing;
 
 1032        cVal.y *= 1.0f - m_fSmoothing;
 
 1033        cVal.z *= 1.0f - m_fSmoothing;
 
 1035        cv::Point3f pVal = m_prevfilteredPoints[i] + m_pointsTrend[i];
 
 1036        pVal.x *= m_fSmoothing;
 
 1037        pVal.y *= m_fSmoothing;
 
 1038        pVal.z *= m_fSmoothing;
 
 1039        m_filteredPoints[i] = cVal + pVal;
 
 1041        diffPoints = m_filteredPoints[i] - m_prevfilteredPoints[i];
 
 1044        cVal.x *= m_fCorrection;
 
 1045        cVal.y *= m_fCorrection;
 
 1046        cVal.z *= m_fCorrection;
 
 1048        pVal = m_pointsTrend[i];
 
 1049        pVal.x *= 1.0f - m_fCorrection;
 
 1050        pVal.y *= 1.0f - m_fCorrection;
 
 1051        pVal.z *= 1.0f - m_fCorrection;
 
 1053        trendPoints[i] = cVal + pVal;
 
 1056        cVal = trendPoints[i];
 
 1057        cVal.x *= m_fPrediction;
 
 1058        cVal.y *= m_fPrediction;
 
 1059        cVal.z *= m_fPrediction;
 
 1061        cv::Point3f predictedPoint = m_filteredPoints[i] + cVal;
 
 1064        diffPoints = predictedPoint - points[i];
 
 1065        fDiff = fabs(sqrt(diffPoints.x * diffPoints.x + diffPoints.y * diffPoints.y));
 
 1067        if (fDiff > m_fMaxDeviationRadius)
 
 1069          cVal = predictedPoint;
 
 1070          cVal.x *= m_fMaxDeviationRadius / fDiff;
 
 1071          cVal.y *= m_fMaxDeviationRadius / fDiff;
 
 1072          cVal.z *= m_fMaxDeviationRadius / fDiff;
 
 1075          pVal.x *= 1.0f - m_fMaxDeviationRadius / fDiff;
 
 1076          pVal.y *= 1.0f - m_fMaxDeviationRadius / fDiff;
 
 1077          pVal.z *= 1.0f - m_fMaxDeviationRadius / fDiff;
 
 1079          predictedPoint = cVal + pVal;
 
 1082        m_filteredPoints[i] = predictedPoint;
 
 1086    for (
unsigned int i = 0; i < 2; i++)
 
 1088      m_prevfilteredPoints[i] = m_filteredPoints[i];
 
 1089      m_prevPoints[i] = points[i];
 
 1090      m_pointsTrend[i] = trendPoints[i];
 
 
 1096    return m_filteredPoints;
 
 
 
 1100static bool NiceHomography(cv::Mat Homography)
 
 1103  Homography.convertTo(H, CV_32FC1);
 
 1105  const float det = H.at<
float>(0, 0) * H.at<
float>(1, 1) - H.at<
float>(1, 0) * H.at<
float>(0, 1);
 
 1109  const float N1 = sqrt(H.at<
float>(0, 0) * H.at<
float>(0, 0) + H.at<
float>(1, 0) * H.at<
float>(1, 0));
 
 1110  if (N1 > 4 || N1 < 0.1)
 
 1113  const float N2 = sqrt(H.at<
float>(0, 1) * H.at<
float>(0, 1) + H.at<
float>(1, 1) * H.at<
float>(1, 1));
 
 1114  if (N2 > 4 || N2 < 0.1)
 
 1117  const float N3 = sqrt(H.at<
float>(2, 0) * H.at<
float>(2, 0) + H.at<
float>(2, 1) * H.at<
float>(2, 1));
 
 1125static cv::Point3f rot2euler(
const cv::Mat &rotationMatrix)
 
 1129  double m00 = rotationMatrix.at<
float>(0, 0);
 
 1130  double m02 = rotationMatrix.at<
float>(0, 2);
 
 1131  double m10 = rotationMatrix.at<
float>(1, 0);
 
 1132  double m11 = rotationMatrix.at<
float>(1, 1);
 
 1133  double m12 = rotationMatrix.at<
float>(1, 2);
 
 1134  double m20 = rotationMatrix.at<
float>(2, 0);
 
 1135  double m22 = rotationMatrix.at<
float>(2, 2);
 
 1143    z = atan2(m02, m22);
 
 1145  else if (m10 < -0.998f) { 
 
 1148    z = atan2(m02, m22);
 
 1152    x = atan2(-m12, m11);
 
 1154    z = atan2(-m20, m00);
 
 1165static cv::Mat euler2rot(
const cv::Point3f &euler)
 
 1167  cv::Mat rotationMatrix(3, 3, CV_32F);
 
 1181  float m00, m01, m02, m10, m11, m12, m20, m21, m22;
 
 1184  m01 = sh * sb - ch * sa*cb;
 
 1185  m02 = ch * sa*sb + sh * cb;
 
 1190  m21 = sh * sa*cb + ch * sb;
 
 1191  m22 = -sh * sa*sb + ch * cb;
 
 1193  rotationMatrix.at<
float>(0, 0) = m00;
 
 1194  rotationMatrix.at<
float>(0, 1) = m01;
 
 1195  rotationMatrix.at<
float>(0, 2) = m02;
 
 1196  rotationMatrix.at<
float>(1, 0) = m10;
 
 1197  rotationMatrix.at<
float>(1, 1) = m11;
 
 1198  rotationMatrix.at<
float>(1, 2) = m12;
 
 1199  rotationMatrix.at<
float>(2, 0) = m20;
 
 1200  rotationMatrix.at<
float>(2, 1) = m21;
 
 1201  rotationMatrix.at<
float>(2, 2) = m22;
 
 1203  return rotationMatrix;
 
 1207#include <android/log.h> 
 1208#define LOG_TAG "ScolApp" 
 1209#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) 
 1210#define LOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__) 
 1211#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__) 
 1213#include <sys/stat.h> 
 1214static std::string CopyFileFromAssetToSD(std::string source, std::string dest, std::string destdir)
 
 1216  std::string out = 
"";
 
 1217  char*(*GetUserPath)() = (
char*(__cdecl *)()) SCgetExtra("GetUserPath");
 
 1225    std::string userPath = GetUserPath();
 
 1226    LOGI(
"CopyFileFromAssetToSD > User path : %s", userPath.c_str());
 
 1228    source = 
"apk/" + source;
 
 1230    dest = userPath + 
"/" + destdir + 
"/" + dest;
 
 1231    destdir = userPath + 
"/" + destdir;
 
 1233    if (boost::filesystem::exists(dest))
 
 1235      LOGI(
"CopyFileFromAssetToSD > source already exist : %s", dest.c_str());
 
 1240    LOGI(
"CopyFileFromAssetToSD > make dir : %s", destdir.c_str());
 
 1241    boost::filesystem::create_directories(destdir.c_str());
 
 1243    r = fopen(source.c_str(), 
"rb");
 
 1246      LOGI(
"CopyFileFromAssetToSD > source not found : %s", source.c_str());
 
 1250    w = fopen(dest.c_str(), 
"wb");
 
 1255        n = fread(buf, 1, 1024, r);
 
 1256        if (n > 0) fwrite(buf, 1, n, w);
 
 1257      } 
while (n == 1024);
 
 1263      LOGI(
"CopyFileFromAssetToSD > dest failed to create : %s", dest.c_str());
 
 1268    LOGI(
"CopyFileFromAssetToSD > succeed : %s", dest.c_str());
 
 1276static std::string cloneFileFromAssetToSD(std::string source)
 
 1278  if (source.compare(0, 4, 
"APK/") == 0)
 
 1279    source = source.substr(4, source.length());
 
 1281  boost::filesystem::path file(source);
 
 1282  return CopyFileFromAssetToSD(source, file.filename().generic_string(), file.parent_path().generic_string());
 
 1286static cv::Mat createMask(cv::Size img_size, std::vector<cv::Point2f>& pts)
 
 1288  cv::Mat mask(img_size, CV_8UC1);
 
 1296  a[0] = pts[3].y - pts[0].y;
 
 1297  a[1] = pts[2].y - pts[1].y;
 
 1298  a[2] = pts[1].y - pts[0].y;
 
 1299  a[3] = pts[2].y - pts[3].y;
 
 1301  b[0] = pts[0].x - pts[3].x;
 
 1302  b[1] = pts[1].x - pts[2].x;
 
 1303  b[2] = pts[0].x - pts[1].x;
 
 1304  b[3] = pts[3].x - pts[2].x;
 
 1306  c[0] = pts[0].y * pts[3].x - pts[3].y * pts[0].x;
 
 1307  c[1] = pts[1].y * pts[2].x - pts[2].y * pts[1].x;
 
 1308  c[2] = pts[0].y * pts[1].x - pts[1].y * pts[0].x;
 
 1309  c[3] = pts[3].y * pts[2].x - pts[2].y * pts[3].x;
 
 1311  float max_x, min_x, max_y, min_y;
 
 1313  min_x = img_size.width;
 
 1315  min_y = img_size.height;
 
 1318  for (i = 0; i < 4; i++)
 
 1320    if (pts[i].x > max_x)
 
 1322    if (pts[i].x < min_x)
 
 1324    if (pts[i].y > max_y)
 
 1326    if (pts[i].y < min_y)
 
 1330  if (max_x >= img_size.width)
 
 1331    max_x = img_size.width - 1;
 
 1332  if (max_y >= img_size.height)
 
 1333    max_y = img_size.height - 1;
 
 1339  unsigned char *ptr = mask.data;
 
 1343  for (y = min_y; y <= max_y; y++)
 
 1345    offset = y * img_size.width;
 
 1346    for (x = min_x; x <= max_x; x++)
 
 1348      for (i = 0; i < 4; i++)
 
 1350        val[i] = a[i] * x + b[i] * y + c[i];
 
 1353      if (val[0] * val[1] <= 0 && val[2] * val[3] <= 0)
 
 1354        *(ptr + offset + x) = 255;
 
 1361static std::vector<cv::Point2f> scalePoints(std::vector<cv::Point2f>& point_vec, 
double scale)
 
 1363  std::vector<cv::Point2f> ret_vec;
 
 1365  std::vector<cv::Point2f>::iterator itr = point_vec.begin();
 
 1366  while (itr != point_vec.end())
 
 1368    ret_vec.push_back(*itr * scale);
 
 1376static bool checkRectShape(std::vector<cv::Point2f>& rectPts)
 
 1378  if (rectPts.size() != 4)
 
 1385  vec[0][0] = rectPts[1].x - rectPts[0].x;
 
 1386  vec[0][1] = rectPts[1].y - rectPts[0].y;
 
 1387  vec[1][0] = rectPts[2].x - rectPts[1].x;
 
 1388  vec[1][1] = rectPts[2].y - rectPts[1].y;
 
 1389  vec[2][0] = rectPts[3].x - rectPts[2].x;
 
 1390  vec[2][1] = rectPts[3].y - rectPts[2].y;
 
 1391  vec[3][0] = rectPts[0].x - rectPts[3].x;
 
 1392  vec[3][1] = rectPts[0].y - rectPts[3].y;
 
 1395  float val = vec[3][0] * vec[0][1] - vec[3][1] * vec[0][0];
 
 1401  for (i = 0; i < 3; i++)
 
 1403    val = vec[i][0] * vec[i + 1][1] - vec[i][1] * vec[i + 1][0];
 
 1410  if (cv::isContourConvex(cv::Mat(rectPts)))
 
 1412    for (j = 0; j < rectPts.size(); j++)
 
 1414      int pdx = j == 0 ? rectPts.size() - 1 : j - 1; 
 
 1415      int sdx = j == rectPts.size() - 1 ? 0 : j + 1; 
 
 1417      cv::Point v1 = rectPts[sdx] - rectPts[j];
 
 1418      cv::Point v2 = rectPts[pdx] - rectPts[j];
 
 1421      double angle = acos(
static_cast<double>(v1.x*v2.x + v1.y*v2.y) / (cv::norm(v1) * cv::norm(v2)));
 
 1433static void rotateImage(
const cv::Mat &input, cv::Mat &output, cv::Mat &outwarp, 
double alpha, 
double beta, 
double gamma, 
double dx, 
double dy, 
double dz, 
double f)
 
 1435  alpha = alpha * (CV_PI / 180.);
 
 1436  beta = beta * (CV_PI / 180.);
 
 1437  gamma = gamma * (CV_PI / 180.);
 
 1440  double w = (double)input.cols;
 
 1441  double h = (double)input.rows;
 
 1444  cv::Mat A1 = (cv::Mat_<double>(4, 3) <<
 
 1451  cv::Mat RX = (cv::Mat_<double>(4, 4) <<
 
 1453    0, cos(alpha), -sin(alpha), 0,
 
 1454    0, sin(alpha), cos(alpha), 0,
 
 1457  cv::Mat RY = (cv::Mat_<double>(4, 4) <<
 
 1458    cos(beta), 0, -sin(beta), 0,
 
 1460    sin(beta), 0, cos(beta), 0,
 
 1463  cv::Mat RZ = (cv::Mat_<double>(4, 4) <<
 
 1464    cos(gamma), -sin(gamma), 0, 0,
 
 1465    sin(gamma), cos(gamma), 0, 0,
 
 1470  cv::Mat R = RX * RY * RZ;
 
 1473  cv::Mat T = (cv::Mat_<double>(4, 4) <<
 
 1479  cv::Mat A2 = (cv::Mat_<double>(3, 4) <<
 
 1485  outwarp = A2 * (T * (R * A1));
 
 1488  warpPerspective(input, output, outwarp, input.size(), cv::INTER_LANCZOS4);
 
cbmachine ww
Scol machine declaration for MM macros.
 
int LoadArToolkit(mmachine m)
 
Vector2 operator/(Vector2 u, float scalar)
 
int UnloadMediaPlayerToolkit()
 
int LoadMathToolkit(mmachine m)
Load the packages in Scol virtual machine.
 
Vector2 operator*(Vector2 u, float scalar)
 
int LoadCaptureToolkit(mmachine m)
 
Vector2 operator+(Vector2 u, Vector2 v)
 
Vector2 operator-(Vector2 u, Vector2 v)
 
int Random(int mi, int mx)
 
int LoadMediaPlayerToolkit(mmachine m)
 
int WM_ML_TRAINING_FINISHED
 
int LoadMlToolkit(mmachine m)
 
int LoadBitmapToolKit(mmachine m)
 
static BtQuaternion Slerp(float fT, const BtQuaternion &rkP, const BtQuaternion &rkQ, bool shortestPath)
 
BtQuaternion Inverse() const
 
BtQuaternion(float quat[4])
 
BtQuaternion operator*(float s) const
 
bool operator!=(const BtQuaternion &rhs) const
 
BtQuaternion(float ww, float xx, float yy, float zz)
 
BtQuaternion operator+(const BtQuaternion &rkQ) const
 
BtQuaternion(double quat[4])
 
BtQuaternion operator-() const
 
static const BtQuaternion IDENTITY
 
friend BtQuaternion operator*(float s, const BtQuaternion &q)
 
float Dot(const BtQuaternion &rkQ) const
 
bool operator==(const BtQuaternion &rhs) const
 
static BtQuaternion FromRotationMatrix(double rotMatrix[16], bool reverseX=false, bool reverseY=true)
 
void Update(std::vector< cv::Point3f > points)
 
FilterDoubleExponential3f()
 
std::vector< cv::Point3f > GetFilteredPoints()
 
~FilterDoubleExponential3f()
 
void Init(float fSmoothing=0.25f, float fCorrection=0.25f, float fPrediction=0.25f, float fJitterRadius=0.03f, float fMaxDeviationRadius=0.05f)
 
~FilterDoubleExponentialDouble()
 
FilterDoubleExponentialDouble()
 
std::vector< double > GetFilteredPoints()
 
void Init(float fSmoothing=0.25f, float fCorrection=0.25f, float fPrediction=0.25f, float fJitterRadius=0.03f, float fMaxDeviationRadius=0.05f)
 
void Update(std::vector< double > points)
 
std::vector< cv::Point2f > GetFilteredPoints()
 
void Update(std::vector< cv::Point2f > points)
 
void Init(float fSmoothing=0.25f, float fCorrection=0.25f, float fPrediction=0.25f, float fJitterRadius=0.03f, float fMaxDeviationRadius=0.05f)
 
~FilterDoubleExponential()
 
FilterDoubleExponential()
 
Vector2 operator*=(float scalar)
 
float DotProduct(Vector2 other)
 
Vector2 operator+=(Vector2 other)
 
Vector2 operator/=(float scalar)
 
Vector2 GetNoramlizedVector()
 
Vector2(float xx, float yy)
 
Vector2 operator-=(Vector2 other)
 
Vector3(float xx, float yy, float zz)
 
Vector3 operator*=(float scalar)
 
float DotProduct(Vector3 other)
 
Vector3 operator/=(float scalar)
 
Vector3 CrossProduct(Vector3 other)
 
Vector3 operator+=(Vector3 other)
 
Vector3 operator-=(Vector3 other)
 
Vector3 GetNoramlizedVector()