txt内数据格式:
0.jpg -0.262107 -0.100013 0.125654 0.289338 -0.003868 -0.196953 0.175709 -0.185209 -0.157402 -0.143893 -0.164551 0.071440 0.179504 0.197782
1.jpg -0.407406 -0.245312 -0.019645 0.144038 -0.149167 -0.342252 0.030410 -0.257859 -0.230051 -0.216543 -0.237201 -0.001209 0.106855 0.125133
读取代码
/*
1. 关键点检测部分使用
2. 用于可视化变换后的图像
3. 也可以用于保存变换后的图像
4. 时间:2019.2.13
*/
#include<iostream>
#include<io.h>
#include<fstream>
#include<string>
#include<opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
const string root_dir = "G:/FacePoints/Transformed_image/"; // 变换后的图像存放路径
const string label_dir = "G:/FacePoints/train_trans.txt"; // 变换后的文件名及标签存放路径
const string saved_dir = "G:/FacePoints/DisplayFacePoint/"; // 保存画上了关键点的图像
// 处理过程:读图->读标签->实时显示图/保存图
fstream fe;
fe.open(label_dir, ios::in);
char Index[6000];
int dataNum = 0;
int pointNum = 7;
while (fe.eof() == 0)
{
memset(Index, 0, 6000);
fe.getline(Index, 6000);
if (Index == "")
continue;
char filename[200] = { 0 };
char points_str[5000] = { 0 };
sscanf(Index, "%[^ ] %[^\n]", filename, points_str);
// 读取关键点坐标(以图象左上角为原点)
vector<float> points;
char *pnext = NULL;
char* token = strtok_s(points_str, " ", &pnext);
while (token != NULL)
{
char* newToken = new char[strlen(token) + 1];
strcpy_s(newToken, strlen(token) + 1, token);
float tempvalue = atof(newToken);
tempvalue = int(tempvalue * 40 + 20); // 将关键点数据变换到变换后的图像上
points.push_back(tempvalue);
token = strtok_s(NULL, " ", &pnext);
delete[]newToken;
newToken = NULL;
}
// 读取图像文件
string full_path = "";
full_path = root_dir + filename;
Mat image = imread(full_path);
// 显示并保存图像
for (int i = 0; i < pointNum; i++)
{
Point temp;
temp.x = points[i];
temp.y = points[i + pointNum];
circle(image, temp, 0.5, Scalar(0, 0, 255), 0.5);
}
imshow("facePoints", image);
waitKey(1);
resize(image, image, Size(100, 100));
string save_path = "";
save_path = saved_dir + filename;
imwrite(save_path, image);
dataNum++;
}
fe.close();
return 0;
}