-1

ファイルからタイプ CvPoint* のポイントを読み取ることに興味がありますが、標準の表記法 (x,y) を試しました。出力を確認しようとすると、誤った値が返されます。ファイル内の CvPoint を読み取るための形式は何ですか。

ポイント.txt

(1,1)

main.cpp

points  = (CvPoint*)malloc(length*sizeof(CvPoint*));
points1 = (CvPoint*)malloc(length*sizeof(CvPoint*));
points2 = (CvPoint*)malloc(length*sizeof(CvPoint*));
fp = fopen(points.txt, "r");
fscanf(fp, "%d", &(length));
printf("%d  \n", length);
i = 1;
while(i <= length)
{
  fscanf(fp, "%d", &(points[i].x));
  fscanf(fp, "%d", &(points[i].y));
  printf("%d  %d \n",points[i].x, points[i].y);
  i++;
}

それは印刷します:

1


12  0
4

1 に答える 1

0

テキスト ファイルに同じ形式を使用した別のアプローチを次に示します。

#include <iostream>
#include <fstream>
#include <opencv2/core/core.hpp>

using namespace std;
using namespace cv;

int main(int argc, char* argv[]) {
    ifstream file("points.txt");
    string line;
    size_t start, end;
    Point2f point;
    while (getline(file, line)) {
         start = line.find_first_of("(");
     end = line.find_first_of(",");
     point.x = atoi(line.substr(start + 1, end).c_str());
     start = end;
     end = line.find_first_of(")");
     point.y = atoi(line.substr(start + 1, end - 1).c_str());
     cout << "x, y: " << point.x << ", " << point.y << endl;
    }
    return 0;
}
于 2013-02-13T21:40:05.360 に答える