最近需要利用深度图做一些东西,就在网上下了一些数据(http://eeeweba.ntu.edu.sg/computervision/people/home/renzhou/HandGesture.htm),该数据集包含了彩色图及对应的深度图,但是该数据集没有以图像形式存储深度值,而是用txt文本以行列形式存储真正的深度值(单位为mm),所以并不能直观的看到深度图像,我需要把这些深度值从txt文本提取出来并把它以图像的形式呈现出来,网上没看到现成的解决的方案,所以我只有用现成的轮子自己做一个了。废话不多说,直接上代码(代码很乱,估计也只用这么一次,所以就没怎么注意了。程序的基本流程是:先找到目录及子目录下的所有txt文件,再分别读取txt文件,按行读取之后再进行字符串分割提取其中的深度值,为了便于以图像形式显示,将深度值归一化至0~255存入8位单通道的Mat类型数据中,最后以png图像形式保存至各个目录):
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <io.h>
#include <direct.h>
#include <fstream>
#include <iostream>
using namespace cv;
using namespace std;
// ******************************************************************
// @refer to [C++文件读写操作(二)逐字符读取文本和逐行读取文本](http://blog.csdn.net/wangshihui512/article/details/8921924)
// [字符串分割(C++)](http://www.cnblogs.com/MikeZhang/archive/2012/03/24/MySplitFunCPP.html)
// [C++读取文件夹中所有的文件或者是特定后缀的文件](http://blog.csdn.net/adong76/article/details/39432467)
// [C/C++ 判断文件夹是否存在以及创建、删除文件夹 windows以及linux通用](http://blog.csdn.net/u012005313/article/details/50688257)
// [Split a string in C++?](http://stackoverflow.com/questions/236129/split-a-string-in-c)
// [Kinect开发学习笔记之(六)带游戏者ID的深度数据的提取](http://blog.csdn.net/zouxy09/article/details/8151044)
// [Depth Map Tutorial](http://www.pages.drexel.edu/~nk752/depthMapTut.html)
// ******************************************************************
// ----- 逐个字符读取文件 --------
void testByChar()
{
fstream testByCharFile;
char c;
testByCharFile.open("./test.txt",ios::in);
while(!testByCharFile.eof())
{
testByCharFile>>c;
cout<<c;
}
testByCharFile.close();
}
// -------- 逐行读取文件 -------------------
void testByLine()
{
char buffer[256];
fstream outFile;
outFile.open("./test.txt",ios::in);
while(!outFile.eof())
{
outFile.getline(buffer, 256, '\n');//getline(char *,int,char) 表示该行字符达到256个或遇到换行就结束
cout<<buffer<<endl;
}
outFile.close();
}
// ------- 分割字符串 --------------
void splitString()
{
char buffer[1280];
fstream outFile;
outFile.open("./test.txt",ios::in);
while(!outFile.eof())
{
outFile.getline(