OpenCVの"const std::vectorcv::Point2f"という宣言で,
座標情報を格納する変数をtxtファイルから読み込んだものから作りたいです.
自分の思うように書いてうまくいかなかったものを次に示します.
c++
1#include <iostream> 2#include <fstream> 3#include <vector> 4#include <cmath> 5#include <opencv2/opencv.hpp> 6#include <stdlib.h> 7 8std::vector<cv::Point2f> read_data(const std::string &fname) 9{ 10 // ファイルの存在チェック 11 std::ifstream inFile(fname); 12 if (static_cast<bool>(inFile) == false) { 13 std::cerr << fname << "が存在しません\n"; 14 exit(0); 15 } 16 17 // データの読み込み 18 std::vector<cv::Point2f> data; 19 while (inFile.good()) { 20 cv::Point pt; 21 inFile >> pt.x >> pt.y; 22 if (inFile.eof()) break; 23 data.emplace_back(pt); 24 } 25 26 return data; 27} 28 29int main(int argc, char *argv[]){ 30 31 if(argc != 5){ 32 std::cout << "\nERROR: too few parameters\n"; 33 //help(argv); 34 return -1; 35 } 36 37const std::vector<cv::Point2f> data1 = read_data("../ryoiki/1xy.txt"); 38 39return 1; 40 41}
また,txtファイルの中身を一部示します.
いくつかの座標が縦に並べられていて,左の列がx座標,右の列がy座標になっています.
1xy.txt
1512 529 2491 562 3433 470 4435 470 5468 491 6503 557 761 88 865 88 9364 422 1085 88 11402 425 12486 482
どなたかご教授ください.
どのように「うまくいかなかった」のかがハッキリとしなければ原因は分かりません。
例えば数字が意図通りに読み込まれないとすれば、プログラムが実際にどのような挙動をしたのかを示すべきですし、どのような環境でコンパイル・実行しているのかや、テキストファイルのエンコーディングなども併せて明記しておくと原因究明に役立つと思われます。
あなたの回答
tips
プレビュー