主要涉及到的内容有批量读取文件夹中以dat为后缀的文件,获取文件名,读取datshuju,并最后赋图保存
//获取所有的文件名
void GetAllFiles(string path, vector<string>& files)
{
long hFile = 0;
//文件信息
struct _finddata_t fileinfo;
string p;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
{
do
{
if ((fileinfo.attrib & _A_SUBDIR))
{
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
GetAllFiles(p.assign(path).append("\\").append(fileinfo.name), files);
}
}
else
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}
//获取特定格式的文件名
void GetAllFormatFiles(string path, vector<string>& files, string format)
{
//文件句柄
long long hFile = 0;
//文件信息
struct _finddata_t fileinfo;
string p;
if ((hFile = _findfirst(p.assign(path).append("\\*" + format).c_str(), &fileinfo)) != -1)
{
do
{
if ((fileinfo.attrib & _A_SUBDIR))
{
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
{
//files.push_back(p.assign(path).append("\\").append(fileinfo.name) );
GetAllFormatFiles(p.assign(path).append("\\").append(fileinfo.name), files, format);
}
}
else
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}
void readFile(const char* path)
{
string save_path = "";
string filename = path;
size_t found = filename.find_last_of('\\');
string savename = filename.substr(found+1, filename.length());
size_t found1 = savename.find_last_of('.');
string savename1 = savename.substr(0, found1);
string savepath = save_path + savename1 + ".png";
//打开文件
FILE* pFile = nullptr;
pFile = fopen(path, "rb");
//初始化
MV3D_LP_DEPTH_DATA data;
memset(&data, 0, sizeof(MV3D_LP_DEPTH_DATA));
//获取结构体数据
fread(&data, sizeof(MV3D_LP_DEPTH_DATA), 1, pFile);
//获取深度数据
int16_t* pDpData = new int16_t[data.nDataLen];
fread(pDpData, data.nDataLen, 1, pFile);
int dWidth = data.nWidth;
int dHeight = data.nHeight;
cv::Mat res(dHeight, dWidth, CV_8UC3, cv::Scalar(0, 0, 0));
int max = -32768;
int min = 0;
int64_t mid = 0;
int count = 0;
int count1 = 0;
for (int row = 0; row < dHeight; row++)
{
for (int col = 0; col < dWidth; col++)
{
count1++;
int16_t pd = pDpData[row*dWidth+col];
if (pd != (-32768))
{
count++;
mid = mid + pd;
if (pd > max)
max = pd;
if (pd < min)
min = pd;
}
}
}
mid = mid / count;
for (int row = 0; row < dHeight; row++)
{
for (int col = 0; col < dWidth; col++)
{
int16_t pd = pDpData[row * dWidth + col];
if (pd == (-32768)) {
res.at<Vec3b>(row, col)[0] = 0; //B
res.at<Vec3b>(row, col)[1] = 0; //G
res.at<Vec3b>(row, col)[2] = 0; //R
}
else {
int color = 255 * (pd - min) / (max - min);
res.at<Vec3b>(row, col)[0] = color; //B
res.at<Vec3b>(row, col)[1] = color; //G
res.at<Vec3b>(row, col)[2] = color; //R
}
}
}
//for (int row = 0; row < dHeight; row++)
//{
// for (int col = 0; col < dWidth; col++)
// {
// int16_t pd = pDpData[row * dWidth + col];
// if (pd == (-32768)) {
// res.at<Vec3b>(row, col)[0] = 0; //B
// res.at<Vec3b>(row, col)[1] = 0; //G
// res.at<Vec3b>(row, col)[2] = 0; //R
// }
// else {
// if (mid - pd > 150) {
// res.at<Vec3b>(row, col)[0] = 255; //B
// res.at<Vec3b>(row, col)[1] = 255; //G
// res.at<Vec3b>(row, col)[2] = 255; //R
// }
// else {
// res.at<Vec3b>(row, col)[0] = 0; //B
// res.at<Vec3b>(row, col)[1] = 0; //G
// res.at<Vec3b>(row, col)[2] = 0; //R
// }
// }
// }
//}
cv::imwrite(savepath, res);
int main(int argc, char** argv)
{
string filePath = "";
vector<string> files;
//读取所有格式为dat的文件
string format = ".dat";
GetAllFormatFiles(filePath, files, format);
int len = files.size();
for (int i = 0; i < len; i++) {
const char* p = files[i].data();
readFile(p);
}
}