OpenCV 3.3.0 contribのsfmモジュールのサンプルを動かしてみる

NegativeMind
Aug 25, 2017 · 5 min read

数度のトライを経て、OpenCVのバージョン3.3.0でやっとsfmモジュールのビルドが通ったわけです。
https://blog.negativemind.com/2017/08/11/opencv3-3-0-with-sfm-module/

ようやくサンプルを試す段階に来た。参考にしてるQiita記事の後編へやっと進める。↓
http://qiita.com/ChaoticActivity/items/178d23508b92a09e59ea

記事によると、サンプルを動かすためにまたいくつか手を加える必要がある(笑)

ヘッダーファイルの修正

さて、opencv_contrib-3.3.0/modules/sfm/samples以下にあるsfmのサンプルをそのままビルドしようとすると

reconstruct()が定義されていません

的なエラーが出る。
このエラーの理由は、いくつかのヘッダーファイルで

#if CERES_FOUND

という条件マクロが書かれており、Ceres-Solverの有無でincludeするファイルを選択しているから。なので、この条件がtrueになるようにコンパイラにCeres-Solverが存在することを知らせる必要がある。

で、ちょっと行儀が悪いけど、オイラはもうinclude/opencv2/sfm.hppの冒頭の以下の記述を

#ifndef __OPENCV_SFM_HPP__
#define __OPENCV_SFM_HPP__

#include <opencv2/sfm/conditioning.hpp>
#include <opencv2/sfm/fundamental.hpp>
#include <opencv2/sfm/numeric.hpp>
#include <opencv2/sfm/projection.hpp>
#include <opencv2/sfm/triangulation.hpp>
#if CERES_FOUND
#include <opencv2/sfm/reconstruct.hpp>
#include <opencv2/sfm/simple_pipeline.hpp>
#endif

直接以下のように編集しちゃいました。

#ifndef __OPENCV_SFM_HPP__
#define __OPENCV_SFM_HPP__

#include
#include
#include
#include
#include
#include

#define CERES_FOUND 1

#if CERES_FOUND
#include
#include
#endif

サンプルコードの実行

参考記事に倣ってtrajectory_reconstruccion.cppを実行してみる。

おお、特にエラーも無く動作したぞ!

参考記事によると、このサンプルコードにはバグがあるというか、カメラの軌跡を表示する際に使用しているcv::viz::WTrajectoryに渡す引数が間違っているらしい。
以下の部分を

/// Recovering cameras
cout << "Recovering cameras ... ";

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 << "[DONE]" << endl;

逆行列に変えて以下のようにすると正しい座標変換になる。

/// Recovering cameras
cout << "Recovering cameras ... ";

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]).inv());

cout << "[DONE]" << endl;

調子に乗ってscene_reconstruction.cppも動かしてみようと思ったら見事エラー…
http://docs.opencv.org/3.3.0/d4/d18/tutorial_sfm_scene_reconstruction.html

え、正しいコマンドライン引数が良く分からんぞ。。。


Originally published at NegativeMindException.

)
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade