热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

C++提取文件中信息的方法

这篇文章主要为大家详细介绍了C++提取文件中信息的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

对于文件比较复杂的时候,为了获取文件中的信息,需要一些比较特殊的函数,比如,getline()、replace()、atoi,atof等

例子一,读取以下文件中的数据,并保存进一个类里面。

首先,类的定义如下,感觉是struct,但是按照struct的处理,我这段代码出错了,不知道什么问题,暂时po出来吧,有空看看。

struct ImageLabel{
  std::string imagePath;//save图片路径名
  int faceBox[4];//输入点集的最外层包络矩形rect四个参数
  int landmarkPos[2*LandmarkPointsNum];//输入点集

private:
  friend class cereal::access;
  /**
   * Serialises this class using cereal.
   *
   * @param[in] ar The archive to serialise to (or to serialise from).
   */
  template
  void serialize(Archive& ar)
  {
    ar(imagePath, faceBox, landmarkPos);
  }
};

然后读入一系列如下文件,每个文件的数据保存格式相同,因此定义了一个vector,来顺序存储每一个文件对应的类

文件如下:*.pts文件

version: 1
n_points: 68
{
446.000 91.000
449.459 119.344
450.957 150.614
460.552 176.986
471.486 202.157
488.087 226.842
506.016 246.438
524.662 263.865
553.315 271.435
578.732 266.260
599.361 248.966
615.947 220.651
627.439 197.999
635.375 179.064
642.063 156.371
647.302 124.753
646.518 92.944
470.271 117.870
486.218 109.415
503.097 114.454
519.714 120.090
533.680 127.609
571.937 123.590
585.702 117.155
602.344 109.070
620.077 103.951
633.964 111.236
554.931 145.072
554.589 161.106
554.658 177.570
554.777 194.295
532.717 197.930
543.637 202.841
555.652 205.483
565.441 202.069
576.368 197.061
487.474 136.436
499.184 132.337
513.781 133.589
527.594 143.047
513.422 144.769
499.117 144.737
579.876 140.815
590.901 130.008
605.648 128.376
618.343 132.671
606.771 140.525
593.466 141.419
519.040 229.040
536.292 221.978
}

所有pts文件名都保存在一个TXT文件中,对应的图像名也保存在一个TXT文件中,我们需要把图片的文件路径加文件名保存到类里面,为了处理方便我们用了replace函数,直接替换pts文件名的的后三位,得到新的文件名。

void readpic(std::vector &Imagelabels){
   cout<<"test readpic"<imagePath=filePath+line;//将文件全路径保存在 mImageLabel->imagePath
    mImageLabel.imagePath=filePath+line;//将文件全路径保存在 mImageLabel->imagePath
    cout< vp2f;
    char line11[1024]={0};//存放每行数据
    //将文件逐行读取到string linestr中,然后对行的内容进行判断
    while(LabelsFile.getline(line11, sizeof(line11))){
      //从第四行开始把数据写进landmark数组中
      if((index>=3)&&(index<139)){
        string x = "";
        string y = "";
        std::stringstream word(line11);
        word >> x;
        word >> y;
        cout<

其中,因为GetLine读出来的数据是string类型,因此需要将数据转成int型,看到pts文件中数据是都是float型,因此先要将string转成float,用的是atof函数。

另外如果把 Imagelabels 当做类来处理只需要,最开始,ImageLabel mImageLabel;后面直接push_back,不需要delete。所有的引用都变成:mImageLabel.faceBox,不能用mImageLabel->landmarkPos。 

另外一个升级的例子,处理起来很复杂,这里把ImageLabel当做一个struct来处理,这样需要new,也需要delete,引用的时候也是用的->,这个需要注意。

文件是:labels_ibug_300W.xml

稍微看一部分,了解下它的构成。

<&#63;xml version='1.0' encoding='ISO-8859-1'&#63;>
<&#63;xml-stylesheet type='text/xsl' href='image_metadata_stylesheet.xsl'&#63;>

iBUG face point dataset - All images
This folder contains data downloaded from:
http://ibug.doc.ic.ac.uk/resources/facial-point-annotations/

The dataset is actually a combination of the AFW, HELEN, iBUG, and LFPW
face landmark datasets. But the iBUG people have aggregated it all together
and gave them a consistent set of 68 landmarks across all the images, thereby
turning it into one big dataset.

Note that we have adjusted the coordinates of the points from the MATLAB convention
of 1 being the first index to 0 being the first index. So the coordinates in this
file are in the normal C 0-indexed coordinate system.

We have also added left right flips (i.e. mirrors) of each image and also
appropriately flipped the landmarks. This doubles the size of the dataset.
Each of the mirrored versions of the images has a filename that ends with
_mirror.jpg.

Finally, note that the bounding boxes are from dlib's default face detector. For the
faces the detector failed to detect, we guessed at what the bounding box would have been
had the detector found it and used that.

 
  
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
  
 
 
  
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
  
 
 
  
   
   
   

可以找到一些规律,按照这些规律来得到我们需要的数据,直接上代码:

void ReadLabelsFromFile(std::vector &Imagelabels, std::string Path = "labels_ibug_300W.xml"){
  std::string ParentPath(trainFilePath);
  std::ifstream LabelsFile(ParentPath+Path, std::ios::in);
  if(!LabelsFile.is_open())
    return;
  std::string linestr;
  while(std::getline(LabelsFile, linestr)){
    linestr = trim(linestr);
    linestr = replace(linestr, "", "");
    linestr = replace(linestr, "<", "");
    linestr = replace(linestr, ">", "");
    linestr = replace(linestr, "'", "");
 
    std::vector strNodes = split(linestr, " ");
    static ImageLabel* mImageLabel = NULL;
    switch (strNodes.size()) {
    case 1:
      if(strNodes[0] == "image"){
        Imagelabels.push_back(*mImageLabel);
        delete mImageLabel;
      }
      break;
    case 2:
      if(strNodes[0] == "image"){
        mImageLabel = new ImageLabel();
        mImageLabel->imagePath = ParentPath + split(strNodes[1], "=")[1];
//        std::cout <imagePath <imagePath);
//        cv::imshow("Image", Image);
//        cv::waitKey(0);
      }
      break;
    case 5:
      if(strNodes[0] == "box"){
        mImageLabel->faceBox[0] = atoi(split(strNodes[1], "=")[1].data());
        mImageLabel->faceBox[1] = atoi(split(strNodes[2], "=")[1].data());
        mImageLabel->faceBox[2] = atoi(split(strNodes[3], "=")[1].data());
        mImageLabel->faceBox[3] = atoi(split(strNodes[4], "=")[1].data());
      }
      break;
    case 4:
      if(strNodes[0] == "part"){
        int index = atoi(split(strNodes[1], "=")[1].data());
        mImageLabel->landmarkPos[index] = atoi(split(strNodes[2], "=")[1].data());
        mImageLabel->landmarkPos[index+LandmarkPointsNum] = atoi(split(strNodes[3], "=")[1].data());
      }
      break;
    default:
      break;
    }
  }
  LabelsFile.close();
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


推荐阅读
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • HDFS2.x新特性
    一、集群间数据拷贝scp实现两个远程主机之间的文件复制scp-rhello.txtroothadoop103:useratguiguhello.txt推pushscp-rr ... [详细]
  • 网络请求模块选择——axios框架的基本使用和封装
    本文介绍了选择网络请求模块axios的原因,以及axios框架的基本使用和封装方法。包括发送并发请求的演示,全局配置的设置,创建axios实例的方法,拦截器的使用,以及如何封装和请求响应劫持等内容。 ... [详细]
  • Windows下配置PHP5.6的方法及注意事项
    本文介绍了在Windows系统下配置PHP5.6的步骤及注意事项,包括下载PHP5.6、解压并配置IIS、添加模块映射、测试等。同时提供了一些常见问题的解决方法,如下载缺失的msvcr110.dll文件等。通过本文的指导,读者可以轻松地在Windows系统下配置PHP5.6,并解决一些常见的配置问题。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 本文讨论了在Spring 3.1中,数据源未能自动连接到@Configuration类的错误原因,并提供了解决方法。作者发现了错误的原因,并在代码中手动定义了PersistenceAnnotationBeanPostProcessor。作者删除了该定义后,问题得到解决。此外,作者还指出了默认的PersistenceAnnotationBeanPostProcessor的注册方式,并提供了自定义该bean定义的方法。 ... [详细]
  • eclipse学习(第三章:ssh中的Hibernate)——11.Hibernate的缓存(2级缓存,get和load)
    本文介绍了eclipse学习中的第三章内容,主要讲解了ssh中的Hibernate的缓存,包括2级缓存和get方法、load方法的区别。文章还涉及了项目实践和相关知识点的讲解。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 本文讨论了使用HTML5+JS开发App所需的框架和工具推荐,希望能提供真实案例作为参考。重点考虑框架和工具的文档齐全性以及是否支持二维码扫描、摇一摇等功能。同时提到了H5+框架的适用性。 ... [详细]
  • 本文介绍了一个程序,可以输出1000内能被3整除且个位数为6的所有整数。程序使用了循环和条件判断语句来筛选符合条件的整数,并将其输出。 ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 本文介绍了UVALive6575题目Odd and Even Zeroes的解法,使用了数位dp和找规律的方法。阶乘的定义和性质被介绍,并给出了一些例子。其中,部分阶乘的尾零个数为奇数,部分为偶数。 ... [详细]
author-avatar
李伟祥祥
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有