std::vector<Frame> Dataset::_load_frames(const std::string& basepath)
{
std::vector<Frame> frames;
fs::directory_iterator dit(basepath), dend;
for (; dit != dend; dit++) {
if (dit->path().extension() != ".jpg") continue;
if (dit->path().string() == "") continue;
frames.push_back(Frame(dit->path().string()));
}
return frames;
}
大家好,我对这个函数定义有点困惑。我在猜std::vector<Frame>
是返回值类型(如int
..)?但是之后的Dataset::_load_frames
是什么? Dataset
是前一个块中定义的结构。我有点困惑,这部分假设不是函数名吗?为什么它属于其他一些类?或者如果我对一些基本概念完全错了?
请,任何建议将不胜感激,谢谢!
这是Dataset.cpp
//This is the Dataset.cpp
#include "Dataset.h"
#include <map>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>
#include <fstream>
namespace fs = boost::filesystem;
Frame::Frame(const std::string& _image_filename):image_filename(_image_filename){
int length = image_filename.size();
camera_id = std::stoi(image_filename.substr(length-14,2));
tracklet_id = std::stoi(image_filename.substr(length-11,2));
time = std::stoi(image_filename.substr(length-8,4));
}
#include <iostream>
Dataset::Dataset(const std::string& basepath):
network((fs::path(basepath) / "network.dot").string()){
frames = _load_frames(basepath);
groundtruth = _load_groundtruth((fs::path(basepath) / "groundtruth.txt").string());
}
std::vector<int> Dataset::_load_groundtruth(const std::string& gtfile){
std::ifstream ifs(gtfile);
std::map<std::string, int> data;
while(true){
std::string tmp;
ifs >> tmp;
if(ifs.eof() || tmp=="") break;
std::vector<std::string> tokens;
boost::split(tokens, tmp, boost::is_any_of(","));
data[tokens[0]] = std::stoi(tokens[1]);
}
std::vector<int> gt;
for(auto f: frames){
int length = f.image_filename.size();
std::string fn = f.image_filename.substr(length - 14);
gt.push_back(data[fn]);
}
return gt;
}
std::vector<Frame> Dataset::_load_frames(const std::string& basepath){
std::vector<Frame> frames;
fs::directory_iterator dit(basepath), dend;
for(; dit!=dend; dit++){
if(dit->path().extension()!=".jpg") continue;
if(dit->path().string() == "") continue;
frames.push_back(Frame(dit->path().string()));
}
std::cout << frames[1];
return frames;
}
这是主要的
#include <iostream>
#include <fstream>
#include <opencv2/opencv.hpp>
#include "Dataset.h"
#include "user_function.h"
#include "evaluation.h"
void save(const std::vector<Frame>& frames, const std::vector<int>& results, const std::string& resultfile);
int main(int argc, char **argv){
if(argc < 4){
std::cerr << "usage: " << argv[0] << " level datasetpath resultfile" << std::endl;
exit(EXIT_FAILURE);
}
int level = std::stoi(argv[1]);
std::string datasetpath = argv[2];
std::string resultfile = argv[3];
// データセットの読み込み
Dataset dataset(datasetpath);
// ラベル割当処理(この関数を書き換えます)
std::vector<int> results = compute(level, dataset.frames, dataset.network);
// 評価
evaluate(dataset.groundtruth, results);
// 保存
save(dataset.frames, results, resultfile);
}
void save(const std::vector<Frame>& frames, const std::vector<int>& results, const std::string& resultfile){
std::ofstream ofs(resultfile);
for(int i=0; i<frames.size(); i++){
std::string tmp = frames[i].image_filename;
std::string name = tmp.substr(tmp.size() - 14);
ofs << name << "," << results[i] << std::endl;
}
}
这是Dataset.h
#pragma once
#include <string>
#include <vector>
#include "CameraNetwork.h"
struct Frame{
Frame(const std::string& image_filename);
std::string image_filename;
int camera_id;
int tracklet_id;
int time;
};
struct Dataset{
Dataset(const std::string& basepath);
std::vector<Frame> frames;
CameraNetwork network;
std::vector<int> groundtruth;
private:
std::vector<int> _load_groundtruth(const std::string& gtfile);
std::vector<Frame> _load_frames(const std::string& basepath);
};
答案 0 :(得分:1)
_load_frames是struct DataSet的成员函数。
我认为,鉴于领先的下划线,它是一种私人方法。
struct DataSet
{
private:
//function declaration
std::vector<Frame> _load_frames(const std::string& basepath);
};
我建议你读一本书或者上一些关于c ++的课程,因为这是一个相当基本的概念。
http://www.tutorialspoint.com/cplusplus/cpp_class_member_functions.htm https://www.amazon.co.uk/Jumping-into-C-Alex-Allain-ebook/dp/B00F9311YC https://www.amazon.co.uk/C-Programming-Language-Bjarne-Stroustrup-ebook/dp/B00DUW4BMS