您的位置:首页 > 编程语言 > C语言/C++

c语言使用小技巧之读取图片路径和label

2016-12-25 19:05 225 查看
主要是我记性不好 记下这些常用的以免自己忘记。

1. 每个图片路径后面跟一个label 



   那么读取方式为:

 fstream fin;

fin.open("ss.txt", ios::in | ios::app);

string stemp, int n;

fin>>stemp>>in;

#include <iostream>
#include <opencv2/opencv.hpp>
#include <vector>
#include <fstream>
#include <time.h>
#include "../FaceGenderV.h"
using namespace cv;
using namespace std;
#define MAXFACECOUNT 50

string gender[2] = { "female", "male" };

int main()
{
void * handle = FGV_Init("GPU");
if (handle == NULL)
{
printf("Init failed, exit!");
exit(0);
}

string imagefolder_path="//home//ggj//ggj//data//2015workshop";
FILE *fid=NULL;
fid=fopen("2015workshop_bbox_1221_val.txt","w");
fstream fin;
fin.open("gender_val.txt",ios::in|ios::app);
string stemp;
int n;

char imgpath[260];
int imagenum=0;

while(!fin.eof())
{

fin>>stemp>>n;
cout<<"in "<<n<<endl;
cout<<"stemp "<<stemp<<endl;
cout<<"imagenum "<<imagenum<<endl;
imagenum=imagenum+1;

sprintf(imgpath, "%s/%s",imagefolder_path.c_str(),stemp.c_str());
cv::Mat img = imread(imgpath);

FR_Rect *rect = new FR_Rect[MAXFACECOUNT];
clock_t begin = clock();
int facecount = FGV_Detect(handle, img.data, img.cols, img.rows, rect); //
clock_t end = clock();
printf("detect time cost: %f ms \n", double(end - begin));
printf("facecount = %d\n", facecount);
if (facecount == 0)

continue;
else
{
for (int i = 0; i < facecount; i++)
{

Rect m_rect(rect[i].left, rect[i].top,rect[i].width,rect[i].height);
Mat ROI;
cout<<"rect[i].left "<<rect[i].left<<endl;
cout<<"rect[i].top "<<rect[i].top<<endl;
cout<<"img.rows "<<img.rows<<endl;
cout<<"img.cols "<<img.cols<<endl;
cout<<"rect[i].width "<<rect[i].width<<endl;
cout<<"rect[i].height "<<rect[i].height<<endl;
if(rect[i].width>img.rows)
continue;
if(rect[i].height>img.cols)
continue;
if(rect[i].left>img.rows)
continue;
if(rect[i].top>img.cols)
continue;
if((rect[i].top+rect[i].height)>=img.cols)
continue;
if((rect[i].left+rect[i].width)>=img.rows)
continue;
img(m_rect).copyTo(ROI);
fprintf(fid,"%s ",stemp.c_str());
fprintf(fid,"%d %d %d %d ",rect[i].left,rect[i].top,rect[i].width,rect[i].height);
fprintf(fid,"%d\n",n);
}

}
delete[] rect;
rect = NULL;
}

FGV_Destroy(handle);

fclose(fid);
return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: