实用脚本二:C++读取dat文件并保存为图像

主要涉及到的内容有批量读取文件夹中以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);
    }

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值