gpt4 book ai didi

c++ - 使用 VNL 读取 MatLab (mat) 文件

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:16:09 42 4
gpt4 key购买 nike

我得到了一个 .mat 文件(三维,单精度 float ),我想使用 VXL 读取它. VNL 有几个类/函数可以执行此操作,但我没有找到任何示例来证明它们的用途。我从 the header file 中拼凑了以下内容,但似乎没有数据被读入,当我询问尺寸时,我得到的似乎是未初始化的垃圾(尽管读取了 header 中的一些数据)。

#include <vcl_cstdlib.h>
#include <vcl_iostream.h>
#include <vcl_fstream.h>
#include <vnl/vnl_matlab_read.h>
#include <vnl/vnl_vector.h>

int main(int argc, char ** argv)
{

if (2 != argc)
{
std::cerr << "Provide a file." << std::endl;
return EXIT_FAILURE;
}

const char * fileName = argv[1];

vcl_filebuf fileBuffer;
if (!fileBuffer.open(fileName,vcl_ios::in))
{
std::cerr << "There was a problem opening the file." << std::endl;
return EXIT_FAILURE;
}

vcl_istream stream(&fileBuffer);
vnl_matlab_readhdr read(stream);

vcl_cout << read.name() << vcl_endl; // Platform: PCWIN64, Created on: (date and time)
vcl_cout << read.rows() << vcl_endl; // 1094852661
vcl_cout << read.cols() << vcl_endl; // 774905933

vnl_vector<float> data;
vnl_matlab_read_or_die(stream, data, fileName);
std::cout << data.size() << std::endl; // 0

return EXIT_SUCCESS;

}

我不知道我是否错误地使用了该类,或者 VNL 是否无法处理多维数组?

最佳答案

并不是所有的 MAT 文件都使用相同的格式,它已经改变了好几次。读取垃圾可能是尝试使用不支持压缩的库读取压缩的 MAT 文件。

由于我无法为您选择的库找到任何合理的文档,因此我只能推荐一个替代方案。在 matlab 中保存 MAT 文件时,指定格式 -v7.3 这是最新的格式。这样做,Matlab 将编写一个 gzip 压缩的 HDF5 an API for C++ 附带的文件

如果您无法访问 Matlab,请使用 Octave 将您的 Mat 文件转换为最新格式。这里,save 函数的选项称为 -hdf5 而不是 -7.3

关于c++ - 使用 VNL 读取 MatLab (mat) 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33345417/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com