zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    数据结构与算法——优先队列类的C++实现(二叉堆)
    Effective C++--经验条款
    [精]读览天下免费阅读平台
    团队现状与用人标准——揭秘万达电商(6)
    稀疏向量计算优化小结
    漫谈雪崩
    Git起步
    Solr 配置文件之schema.xml
    Shader toy (顺手写两个Gyro)(纯代码写3D)
    Tomcat服务器安装
  • 原文地址:https://www.cnblogs.com/Anita9002/p/8474308.html
Copyright © 2011-2022 走看看