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;
}
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;
}
相关文章推荐
- 玩转Google开源C++单元测试框架Google Test系列(gtest)之一 - 初识gtest
- C/C++-技巧-宏
- C语言与汇编语言混合编程
- C++(18):STL之vector初步
- 蓝桥 1的个数
- 从c++中理解java的继承关系
- C控制语句:循环
- 关于C++里面的函数中,按值传递与按引用传递的区别
- 算法提高 9-2 文本加密
- C语言实现顺序存储结构的循环链表
- 算法提高 逆序排列
- C++内存管理
- c++ Primer 中文版 第五版 编译
- 使用C++来求解二维卷积
- C语言 内存管理的细枝末节
- 算法 数字三角形问题
- C++的const
- C语言指针的几点作用
- C语言数组与指针总结
- C++(17):STL之string初步