c++读取文件到vector

读取一个pts文件到一个vector<Point2f>里面。

其中pts文件如下:

version: 1
n_points: 68
{
266 246
264 279
269 312
274 345
283 378
302 407
328 429
365 447
399 452
434 445
466 432
494 410
512 383
522 352
525 322
530 287
536 256
288 215
304 199
327 191
349 195
370 202
415 199
436 193
460 191
481 194
496 209
395 239
395 260
396 278
396 296
367 320
378 322
394 323
410 321
421 319
311 242
328 230
349 234
360 246
348 245
331 245
433 244
445 231
465 229
484 241
464 243
448 244
342 356
362 344
383 338
399 338
412 339
432 344
449 356
434 373
414 384
398 388
381 386
362 376
359 359
383 355
397 356
412 355
436 359
413 365
397 368
383 367
}

 

 

#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <fstream>
#include<iostream>
#include <sstream>
#include <vector>
#include <string>
#include <fstream>
#include <iostream>

#ifdef __APPLE__
#include <sys/uio.h>
#else
#include <sys/io.h>
#endif



using namespace cv;
using namespace std;




vector<Point2f> readpts(){
    ifstream myfile("1.pts");
    string temp;
    if (!myfile.is_open())
    {
        cout << "未成功打开文件" << endl;
    }
    vector<Point2f> vp2f;
    int index=0;
    while(getline(myfile,temp)) {
         if((index>=3)&&(index<71)){
             Point2f p1;
             int a[2]={0};
             istringstream iss;//istringstream提供读 string 的功能
             iss.str(temp);//将 string 类型的 test 复制给 iss,返回 void
             string s;
             int i=0;
             while (iss >> s){
                  int x = stoi(s);
//                  cout << x << endl;//按空格读取string
                 a[i]=x;
                 i++;
             }
             p1.x=a[0];
             p1.y=a[1];
             vp2f.push_back(p1);
         }
        index++;
       
    }
    
    return vp2f;
}

int main(){

    Mat img=imread("1.jpg");

  vector<Point2f> landmark= readpts();
   
    for (int idx = 0; idx < landmark.size(); idx++) {
        cv::circle(img, landmark[idx], 1, cv::Scalar(0, 255, 0), -1);
    }
    imshow("img",img);
    cvWaitKey(0);
    return 1;
}

 

posted @ 2018-02-26 17:25  Anita-ff  阅读(4521)  评论(0编辑  收藏  举报