OpenCV 4.12.0
开源计算机视觉
加载中...
搜索中...
无匹配项
相机运动估计

目标

在本教程中,您将学习如何使用重建 API 进行相机运动估计。

  • 加载包含跟踪的 2D 点的文件,并构建所有帧的容器。
  • 运行 libmv 重建流程。
  • 使用 Viz 显示获得的结果。

代码

#include <opencv2/core.hpp>
#include <opencv2/sfm.hpp>
#include <opencv2/viz.hpp>
#include <iostream>
#include <fstream>
using namespace std;
using namespace cv;
using namespace cv::sfm;
static void help() {
cout
<< "\n------------------------------------------------------------------\n"
<< " 这个程序展示了 OpenCV 运动结构 (SFM) 模块中的相机轨迹重建能力。\n"
<< " \n"
<< " \n"
<< " example_sfm_scene_reconstruction <文件路径> <f> <cx> <cy>\n"
<< " example_sfm_trajectory_reconstruction <tracks_file_path> <f> <cx> <cy>\n"
<< " 其中:是轨道文件在您系统中的绝对路径。\n"
<< " \n"
<< " 该文件必须具有以下格式:\n"
<< " row1 : track 1 的 x1 y1 x2 y2 ... x36 y36\n"
<< " row2 : track 2 的 x1 y1 x2 y2 ... x36 y36\n"
<< " 等等\n"
<< " \n"
<< " 即,一行给出某个点在帧 1 到 36 中被跟踪时测量的 2D 位置。如果在一个视图中没有找到匹配项,则 x\n"
<< " 和 y 是 -1。\n"
<< " 每行对应于一个不同的点。\n"
<< " \n"
<< " f 是以像素为单位的焦距。\n"
<< " \n"
<< " cx 是图像主点的 x 坐标,以像素为单位。\n"
<< " cy 是图像主点的 y 坐标,以像素为单位。\n"
<< "------------------------------------------------------------------\n\n"
/* 构建以下结构数据
<< endl;
}
* frame1 frame2 frameN
*
* track1 | (x11,y11) | -> | (x12,y12) | -> | (x1N,y1N) |
* track2 | (x21,y11) | -> | (x22,y22) | -> | (x2N,y2N) |
* trackN | (xN1,yN1) | -> | (xN2,yN2) | -> | (xNN,yNN) |
* 如果标记 (x,y) 没有出现在帧中,则它的
*
*
* 值将为 (-1,-1)。
static void parser_2D_tracks(const String &_filename, std::vector<Mat> &points2d )
*/
if (!myfile.is_open())
{
if (!myfile.is_open()) {
cout << "无法读取文件:" << _filename << endl;
{
double x, y;
exit(0);
} else {
int n_frames = 0, n_tracks = 0;
files.push_back(line_str);
// 从文本文件中提取数据
vector<vector<Vec2d> > tracks;
for ( ; getline(myfile,line_str); ++n_tracks)
istringstream line(line_str);
{
vector<Vec2d> track;
for ( n_frames = 0; line >> x >> y; ++n_frames)
if ( x > 0 && y > 0)
{
track.push_back(Vec2d(x,y));
track.push_back(Vec2d(-1));
else
tracks.push_back(track);
}
// 将数据嵌入重建 API 格式
}
for (int i = 0; i < n_frames; ++i)
Mat_<double> frame(2, n_tracks);
{
for (int j = 0; j < n_tracks; ++j)
frame(0,j) = tracks[j][i][0];
{
frame(1,j) = tracks[j][i][1];
points2d.push_back(Mat(frame));
}
myfile.close();
}
/* 键盘回调来控制 3D 可视化
}
}
bool camera_pov = false;
*/
static void keyboard_callback(const viz::KeyboardEvent &event, void* cookie)
if ( event.action == 0 &&!event.symbol.compare("s") )
{
camera_pov = !camera_pov;
/* 示例主代码
}
int main(int argc, char** argv)
*/
// 从文本文件中读取 2D 点
{
if ( argc != 5 )
// 解析图像路径
{
help();
exit(0);
}
std::vector<Mat> points2d;
parser_2D_tracks( argv[1], points2d );
// 设置相机校准矩阵
const double f = atof(argv[2]),
Matx33d K = Matx33d( f, 0, cx,
Matx33d K = Matx33d( f, 0, cx,
bool is_projective = true;
bool is_projective = true;
0, 0, 1);
reconstruct(points2d, Rs_est, ts_est, K, points3d_estimated, is_projective);
reconstruct(images_paths, Rs_est, ts_est, K, points3d_estimated, is_projective);
cout << "\n----------------------------\n" << endl;
cout << "\n----------------------------\n" << endl;
cout << "重建:" << endl;
cout << "============================" << endl;
cout << "估计的 3D 点:" << points3d_estimated.size() << endl;
cout << "估计的相机:" << Rs_est.size() << endl;
cout << "精炼的内参:" << endl << K << endl << endl;
cout << "3D 可视化:" << endl;
viz::Viz3d window_est("估计坐标系");
cout << "估计的 3D 点:" << points3d_estimated.size() << endl;
window_est.setBackgroundColor(); // 默认为黑色
window_est.registerKeyboardCallback(&keyboard_callback);
cout << "恢复点 ... ";
cout << "正在恢复点 ... ";
for (int i = 0; i < points3d_estimated.size(); ++i)
vector<Vec3f> point_cloud_est;
for (int i = 0; i < points3d_estimated.size(); ++i)
point_cloud_est.push_back(Vec3f(points3d_estimated[i]));
cout << "[完成]" << endl;
cout << "恢复相机 ... ";
vector<Affine3d> path_est;
for (size_t i = 0; i < Rs_est.size(); ++i)
path_est.push_back(Affine3d(Rs_est[i],ts_est[i]));
cout << "渲染轨迹 ... ";
cout << "恢复相机 ... ";
cout << endl << "按:" << endl;
cout << " 's' 切换相机视角" << endl;
cout << " 'q' 关闭窗口" << endl;
if ( path_est.size() > 0 )
// 动画轨迹
{
int idx = 0, forw = -1, n = static_cast<int>(path_est.size());
while(!window_est.wasStopped())
for (size_t i = 0; i < point_cloud_est.size(); ++i)
{
Vec3d point = point_cloud_est[i];
{
Affine3d point_pose(Mat::eye(3,3,CV_64F), point);
char buffer[50];
sprintf (buffer, "%d", static_cast<int>(i));
viz::WCube cube_widget(Point3f(0.1,0.1,0.0), Point3f(0.0,0.0,-0.1), true, viz::Color::blue());
cube_widget.setRenderingProperty(viz::LINE_WIDTH, 2.0);
window_est.showWidget("Cube"+String(buffer), cube_widget, point_pose);
Affine3d cam_pose = path_est[idx];
}
viz::WCameraPosition cpw(0.25); // 坐标轴
viz::WCameraPosition cpw_frustum(K, 0.3, viz::Color::yellow()); // 相机视锥体
if ( camera_pov )
window_est.setViewerPose(cam_pose);
// 渲染完整的轨迹
else
{
window_est.showWidget("cameras_frames_and_lines_est", viz::WTrajectory(path_est, viz::WTrajectory::PATH, 1.0, viz::Color::green()));
window_est.showWidget("CPW", cpw, cam_pose);
window_est.showWidget("CPW_FRUSTUM", cpw_frustum, cam_pose);
// 更新轨迹索引(弹簧效果)
}
forw *= (idx==n || idx==0) ? -1: 1; idx += forw;
// 帧率 1s
window_est.spinOnce(1, true);
window_est.removeAllWidgets();
指定其坐标 x、y 和 z 的 3D 点的模板类。
}
}
return 0;
}
定义 affine.hpp:127
cv::Matx< double, 3, 3 >
从 Mat 派生的模板矩阵类。
定义 mat.hpp:2257
n 维密集数组类
定义 mat.hpp:830
cv::Vec< double, 2 >
定义 types.hpp:255
此类表示键盘事件。
cv::viz::KeyboardEvent::symbol
定义 types.hpp:303
cv::viz::KeyboardEvent::action
定义 types.hpp:302
Viz3d 类表示 3D 可视化窗口。此类是隐式共享的。
这个 3D 小部件通过其轴或视锥体表示场景中的相机位置....
定义 viz3d.hpp:68
void reconstruct(InputArrayOfArrays points2d, OutputArray Ps, OutputArray points3d, InputOutputArray K, bool is_projective=false)
定义 widgets.hpp:544
此 3D 小部件定义一个立方体。
定义 widgets.hpp:373
轨迹。
cv::sfm::reconstruct
std::string String
定义 cvstd.hpp:151
#define CV_64F
Definition interface.h:79
int main(int argc, char *argv[])
定义 highgui_qt.cpp:3
解释
定义 core.hpp:107
STL 命名空间。

首先,我们需要加载包含在所有帧上跟踪的 2D 点的文件,并构建容器以提供给重建 API。在这种情况下,跟踪的 2D 点将具有以下结构,一个 2D 点数组的向量,其中每个内部数组代表一个不同的帧。每帧由一个 2D 点列表组成,例如,帧 1 中的第一个点与帧 2 中的同一点。如果在帧中没有点,则分配的值将为 (-1,-1)

for (int i = 0; i < n_frames; ++i)

* frame1 frame2 frameN
*
* track1 | (x11,y11) | -> | (x12,y12) | -> | (x1N,y1N) |
* track2 | (x21,y11) | -> | (x22,y22) | -> | (x2N,y2N) |
* trackN | (xN1,yN1) | -> | (xN2,yN2) | -> | (xNN,yNN) |
* 如果标记 (x,y) 没有出现在帧中,则它的
*
*
* 值将为 (-1,-1)。
static void parser_2D_tracks(const String &_filename, std::vector<Mat> &points2d )
*/
...
其次,构建的容器将用于提供给重建 API。重要的是概述估计结果必须存储在 vector<Mat> 中
{
for (int j = 0; j < n_tracks; ++j)
frame(0,j) = tracks[j][i][0];
{
frame(1,j) = tracks[j][i][1];
points2d.push_back(Mat(frame));
}
myfile.close();
}

reconstruct(points2d, Rs_est, ts_est, K, points3d_estimated, is_projective);

reconstruct(points2d, Rs_est, ts_est, K, points3d_estimated, is_projective);
reconstruct(images_paths, Rs_est, ts_est, K, points3d_estimated, is_projective);
最后,获得的结果将在 Viz 中显示,在这种情况下,重现带有振荡效果的相机。
cout << "\n----------------------------\n" << endl;
cout << "重建:" << endl;
cout << "============================" << endl;
cout << "估计的 3D 点:" << points3d_estimated.size() << endl;
cout << "估计的相机:" << Rs_est.size() << endl;
cout << "精炼的内参:" << endl << K << endl << endl;
cout << "3D 可视化:" << endl;

用法和结果

为了运行此示例,我们需要指定跟踪点文件的路径,相机的焦距以及中心投影坐标(以像素为单位)。您可以在 samples/data/desktop_trakcks.txt 中找到示例文件

./example_sfm_trajectory_reconstruction desktop_tracks.txt 1914 640 360

下图显示了从跟踪的 2D 点获得的相机运动

生成于 2025 年 7 月 3 日星期四 12:14:36,适用于 OpenCV,作者:doxygen 1.12.0