ORB-SLAM2单目增加读取视频的功能,不通过ros(附源码)

通过cap.isOpened()这个函数来判断读取视频还是图片。使用方式如下:

cerr << endl << "Usage: ./mono_tum path_to_vocabulary path_to_settings path_to_sequence" << endl;
cerr << endl << "or ./mono_tum path_to_vocabulary path_to_settings path_to_video" << endl;
        

读取视频会生成DataforORBSLAM文件,该文件格式是和ORB-SLAM2对应数据集一般。会保存所有图像帧及图像名称信息,生成的数据集也可以通过读取图像数据集的命令来运行。WriteVideoInfo()函数仅是通过帧数来生成图像名称信息文件。

下面是完整的mono_tum.cc代码,复制替换即可:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
enum Datatype{videodata=0, imagesdata=1};
int datatype =0; 

using namespace std;
void LoadImages(const string &strFile, vector &vstrImageFilenames, vector &vTimestamps);
void writeVideoInfo(vector &vstrImageFilenames, vector &vTimestamps, int TotalFrameNumber);

int main(int argc, char **argv)
{
    if(argc != 4)
    {
        cerr << endl << "Usage: ./mono_tum path_to_vocabulary path_to_settings path_to_sequence" << endl;
        cerr << endl << "or ./mono_tum path_to_vocabulary path_to_settings path_to_video" << endl;
        return 1;
    }

    // Retrieve paths to images
    vector vstrImageFilenames;
    vector vTimestamps;
    std::string dataname = argv[3];
    cv::VideoCapture cap(dataname);

    //choose datatype by isOpened()
    if(cap.isOpened())
    {    
        datatype = Datatype::videodata;
    int TotalFrameNumber = cap.get(CV_CAP_PROP_FRAME_COUNT);
    writeVideoInfo(vstrImageFilenames, vTimestamps, TotalFrameNumber);
    }
    else
    {
        datatype = Datatype::imagesdata;
    std::string strFile = string(argv[3])+"/rgb.txt";
    LoadImages(strFile, vstrImageFilenames, vTimestamps);
    }
    int nImages = vstrImageFilenames.size();

    // Create SLAM system. It initializes all system threads and gets ready to process frames.
    ORB_SLAM2::System SLAM(argv[1],argv[2],ORB_SLAM2::System::MONOCULAR,true);

    // Vector for tracking time statistics
    vector vTimesTrack;
    vTimesTrack.resize(nImages);

    cout << endl << "-------" << endl;
    cout << "Start processing sequence ..." << endl;
    cout << "Images in the sequence: " << nImages << endl << endl;

    // Main loop
    cv::Mat im;
    for(int ni=0; ni >(t2 - t1).count();

        vTimesTrack[ni]=ttrack;

        // Wait to load the next frame
        double T=0;
        if(ni0)
            T = tframe-vTimestamps[ni-1];

        if(ttrack &vstrImageFilenames, vector &vTimestamps)
{
    ifstream f;
    f.open(strFile.c_str());

    // skip first three lines
    // 前三行是注释,跳过
    string s0;
    getline(f,s0);
    getline(f,s0);
    getline(f,s0);

    while(!f.eof())
    {
        string s;
        getline(f,s);
        if(!s.empty())
        {
            stringstream ss;
            ss << s;
            double t;
            string sRGB;
            ss >> t;
            vTimestamps.push_back(t);
            ss >> sRGB;
            vstrImageFilenames.push_back(sRGB);
        }
    }
}

//to generate video info file
void writeVideoInfo(vector &vstrImageFilenames, vector &vTimestamps, int TotalFrameNumber)
{
    
    system("mkdir -p DataforORBSLAM/rgb/");
    std::ofstream outtofile;
    std::string FilePath = "DataforORBSLAM/rgb.txt";
    outtofile.open(FilePath); 
    double beginname = 6269.100000;
    std::string filename;
    for(int i=0; i

代码写的不好,有问题私我或者评论。

你可能感兴趣的:(计算机视觉重建,ubuntu,linux,3d)