从文件中读取CVPoint

时间:2013-02-13 03:37:10

标签: c opencv

我对从文件中读取CvPoint *类型的点感兴趣,但我尝试过标准符号(x,y)。当我尝试验证输出时,它给出了不正确的值。在文件中读取CvPoint的格式是什么。

point.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

1 个答案:

答案 0 :(得分: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;
}