Skip to content

Instantly share code, notes, and snippets.

@denkiwakame
Created September 16, 2014 14:07
Show Gist options
  • Save denkiwakame/7bfd1eeadb2439601722 to your computer and use it in GitHub Desktop.
Save denkiwakame/7bfd1eeadb2439601722 to your computer and use it in GitHub Desktop.
やっつけMatパーサ
// data format
// # camnum
// # setnum
// # sets
// # data xd yd x0 y0 ... xN yN
// 0
// 4
// 1 2 3 4
// xxx xxx xxx xxx xxx xxx xxx xxx
// xxx ...
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <regex>
#include <boost/program_options.hpp>
#include <boost/foreach.hpp>
#include <boost/regex.hpp>
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <opencv/cxcore.h>
namespace dc_parser {
double toDouble(std::string s) { double r = 0; std::istringstream ss(s); ss >> r; return r; }
std::vector <std::string> split(const std::string _s, const std::string del)
{
std::vector <std::string> ret;
std::string s = _s;
while (!s.empty())
{
size_t pos = s.find(del);
std::string sub = "";
sub = s.substr(0, pos);
ret.push_back(sub);
if (pos != std::string::npos)
pos += del.size();
s.erase(0, pos);
}
return ret;
}
int loadFromLine(std::ifstream& file, std::string& line, const std::string& escape_char){
if ( !std::getline(file, line) ) return -1; // failed to load
// escape sequence
// excape character regexp
boost::regex pattern("^\\s*" + escape_char);
if ( regex_search(line, pattern) ) return 0; // skip the line
return 1; // success
}
cv::Mat lineToMat(const std::string& line) {
cv::Mat row;
// load as string array
std::vector<std::string> tmp = split(line, " ");
for (const auto& str : tmp) row.push_back(toDouble(str));
return row;
}
std::vector<double> lineToVector(const std::string& line) {
// load as string array
std::vector<std::string> tmp = split(line, " ");
// to double convertion
std::vector<double> args;
for (const auto& str : tmp) args.push_back(toDouble(str));
return args;
}
void loadFromDat(std::string& filename, int& cam_num, int& set_num, std::vector<double>& sets, cv::Mat& data) {
std::ifstream file(filename.c_str());
std::string line;
// comment section
while (true) {
int status = dc_parser::loadFromLine(file, line, "#");
if (status == 1) break;
assert(status >=0 && "load failed");
}
// camera
cam_num = dc_parser::lineToVector(line)[0];
int status = dc_parser::loadFromLine(file, line, "#");
// set-num
assert(status >=0 && "load failed");
set_num = dc_parser::lineToVector(line)[0];
status = dc_parser::loadFromLine(file, line, "#");
// sets
assert(status >=0 && "load failed");
sets = dc_parser::lineToVector(line);
// data
// rows: set_points (xd yd x0 y0 x1 y1 x2 y2 ... )
// cols: points_num
cv::Mat data_row;
while (dc_parser::loadFromLine(file, line, "#") > 0) {
data_row = dc_parser::lineToMat(line);
// 1つ目
if (data.empty()) {
data = data_row;
continue;
}
cv::Mat concat_mat[2];
concat_mat[0] = data;
concat_mat[1] = data_row;
cv::hconcat(concat_mat, 2, data);
}
}
}
int main(int argc, char const* argv[])
{
boost::program_options::options_description cmdline("Command line options");
cmdline.add_options()
("help,h", "show help messages")
("camidx,c", boost::program_options::value<int>(), "camera idx")
("inparam,i", boost::program_options::value<std::string>(), "intrinsic matrix param .xml")
("filename,f", boost::program_options::value<std::string>(), "file name cam0-x-x-x-x-x.dat");
boost::program_options::variables_map variables_map;
try {
// parse_command_line
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, cmdline), variables_map);
} catch (std::exception &e) {
std::cerr << e.what() << std::endl;
return -1;
}
if (argc==1 || variables_map.count("help")){
std::cerr << " USAGE: $ ./excalib --camidx 0 --inparam camera0.xml --filename camera0-1-2-3-4.dat " << std::endl;
std::cerr << cmdline << std::endl;
return 0;
}
std::string filename = variables_map["filename"].as<std::string>();
int cam_num, set_num;
std::vector<double> sets;
cv::Mat points;
dc_parser::loadFromDat(filename, cam_num, set_num, sets, points);
std::cout << points << std::endl;
std::cout << points.rows << " " << points.cols << std::endl;
return 0;
}
@denkiwakame
Copy link
Author

cv::Mat は痒い所にイマイチ手が届かないんだよなあ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment