OpenCV的Sample分析:real_time_tracking(1)

OpenCV的Sample分析:real_time_tracking (1)

今天来分析opencv自带例程,real time tracking

首先来配置一下qmake,

INCLUDEPATH += /usr/local/include \
                /usr/local/include/opencv \
                /usr/local/include/opencv2 \

LIBS += /usr/local/lib/libopencv_highgui.so \
        /usr/local/lib/libopencv_core.so    \
        /usr/local/lib/libopencv_imgproc.so \
        /usr/local/lib/libopencv_calib3d.so \
        /usr/local/lib/libopencv_video.so   \
        /usr/local/lib/libopencv_flann.so  \
        /usr/local/lib/libopencv_features2d.so.3.2 \
        /usr/local/lib/libopencv_videoio.so 

在qt下,配置qmake要比编写cmake要简单得多,只要注意格式,找到文件即可

那就开始分析程序了,先来看全局变量,

string tutorial_path = "/home/anpei/Desktop/task/test_openCV/"; // path to tutorial

string video_read_path = tutorial_path + "Data/box.mp4";       // recorded video
string yml_read_path = tutorial_path + "Data/cookies_ORB.yml"; // 3dpts + descriptors
string ply_read_path = tutorial_path + "Data/box.ply";         // mesh

// Intrinsic camera parameters: UVC WEBCAM
double f = 55;                           // focal length in mm
double sx = 22.3, sy = 14.9;             // sensor size
double width = 640, height = 480;        // image size

double params_WEBCAM[] = { width*f/sx,   // fx
                           height*f/sy,  // fy
                           width/2,      // cx
                           height/2};    // cy

// Some basic colors
Scalar red(0, 0, 255);
Scalar green(0,255,0);
Scalar blue(255,0,0);
Scalar yellow(0,255,255);

// Robust Matcher parameters
int numKeyPoints = 2000;      // number of detected keypoints
float ratioTest = 0.70f;          // ratio test
bool fast_match = true;       // fastRobustMatch() or robustMatch()

// RANSAC parameters
int iterationsCount = 500;      // number of Ransac iterations.
float reprojectionError = 2.0;  // maximum allowed distance to consider it an inlier.
double confidence = 0.95;        // ransac successful confidence.

// Kalman Filter parameters
int minInliersKalman = 30;    // Kalman threshold updating

// PnP parameters
int pnpMethod = SOLVEPNP_ITERATIVE;

这里定义一些全局变量,比如相机的内参数,匹配点的参数,RANSAC参数,以及kalman参数,PnP参数。

以及三个事后会认真分析的函数,这些函数是关于kalman滤波的,值得深入地了解

void initKalmanFilter( KalmanFilter &KF, int nStates, int nMeasurements, int nInputs, double dt);
void updateKalmanFilter( KalmanFilter &KF, Mat &measurements,
                         Mat &translation_estimated, Mat &rotation_estimated );
void fillMeasurements( Mat &measurements,
                       const Mat &translation_measured, const Mat &rotation_measured);

程序首先导入两个文件,

  Model model;               // instantiate Model object
  model.load(yml_read_path); // load a 3D textured object model

  Mesh mesh;                 // instantiate Mesh object
  mesh.load(ply_read_path);  // load an object mesh

需要了解一下,类Model和类Mesh的作用,

打开Model.h头文件,可以一览类Model的接口以及成员变量,(函数的功能以及变量的含义都可以从命名了解,主要用来管理特征点)

class Model
{
public:
  Model();
  virtual ~Model();

  std::vector<cv::Point2f> get_points2d_in() const { return list_points2d_in_; }
  std::vector<cv::Point2f> get_points2d_out() const { return list_points2d_out_; }
  std::vector<cv::Point3f> get_points3d() const { return list_points3d_in_; }
  std::vector<cv::KeyPoint> get_keypoints() const { return list_keypoints_; }
  cv::Mat get_descriptors() const { return descriptors_; }
  int get_numDescriptors() const { return descriptors_.rows; }


  void add_correspondence(const cv::Point2f &point2d, const cv::Point3f &point3d);
  void add_outlier(const cv::Point2f &point2d);
  void add_descriptor(const cv::Mat &descriptor);
  void add_keypoint(const cv::KeyPoint &kp);


  void save(const std::string path);
  void load(const std::string path);


private:
  /** The current number of correspondecnes */
  int n_correspondences_;
  /** The list of 2D points on the model surface */
  std::vector<cv::KeyPoint> list_keypoints_;
  /** The list of 2D points on the model surface */
  std::vector<cv::Point2f> list_points2d_in_;
  /** The list of 2D points outside the model surface */
  std::vector<cv::Point2f> list_points2d_out_;
  /** The list of 3D points on the model surface */
  std::vector<cv::Point3f> list_points3d_in_;
  /** The list of 2D points descriptors */
  cv::Mat descriptors_;
};

对于类Model,它的load()函数是,

/** Load a YAML file using OpenCv functions **/
void Model::load(const std::string path)
{
  cv::Mat points3d_mat;

  cv::FileStorage storage(path, cv::FileStorage::READ);
  storage["points_3d"] >> points3d_mat;
  storage["descriptors"] >> descriptors_;

  points3d_mat.copyTo(list_points3d_in_);

  storage.release();

}

再来看Mesh类,打开Mesh.h后发现,(提取和管理三角形顶点,多边形顶点)

class Mesh
{
public:

  Mesh();
  virtual ~Mesh();

  std::vector<std::vector<int> > getTrianglesList() const { return list_triangles_; }
  cv::Point3f getVertex(int pos) const { return list_vertex_[pos]; }
  int getNumVertices() const { return num_vertexs_; }

  void load(const std::string path_file);

private:
  /** The identification number of the mesh */
  int id_;
  /** The current number of vertices in the mesh */
  int num_vertexs_;
  /** The current number of triangles in the mesh */
  int num_triangles_;
  /* The list of triangles of the mesh */
  std::vector<cv::Point3f> list_vertex_;
  /* The list of triangles of the mesh */
  std::vector<std::vector<int> > list_triangles_;
};

再来看类Mesh的load()函数,

/** Load a CSV with *.ply format **/
void Mesh::load(const std::string path)
{

  // Create the reader
  CsvReader csvReader(path);

  // Clear previous data
  list_vertex_.clear();
  list_triangles_.clear();

  // Read from .ply file
  csvReader.readPLY(list_vertex_, list_triangles_);

  // Update mesh attributes
  num_vertexs_ = (int)list_vertex_.size();
  num_triangles_ = (int)list_triangles_.size();

}

出于好奇,可以看一下类csvReader的readPLY()函数,这里就不叙述啦
















  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值