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;
}
Привет всем, я немного смущен этой функцией define. Я предполагаю, что 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;
}
Это Main
#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);
};
Возможно, [это] (http://www.tutorialspoint.com/cplusplus/cpp_class_member_functions.htm) учебник может дает некоторое представление. – Recker
Кажется, вам нужна [приличная книга] (http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo
'Dataset :: _ load_frames' фактически является именем функции (области действия) функции-члена в struct' Dataset'. –