如何在 OpenCV 中将 Float Mat 写入文件

2021-12-10 00:00:00 opencv floating-point file-io c++ mat

我有一个矩阵

Mat B(480,640,CV_32FC1);

包含浮动值..我想将此矩阵写入一个文件,该文件可以在记事本或 Ms word 或 Excel 中打开以查看内部和存储的值....imwrite 函数只能保存8位或16位图像..

containing floating values..I want to write this matrix to a file which could be opened in notepad or Ms word or Excel to see the values inside and for storage....imwrite function can save 8-bit or 16-bit image only..

如果可以做到,请提出您的建议?如果是,如何??

Drop in your suggestions if this could be done?? if yes, how ??

推荐答案

使用纯 OpenCV API 调用:

Using pure OpenCV API calls:

// Declare what you need
cv::FileStorage file("some_name.ext", cv::FileStorage::WRITE);
cv::Mat someMatrixOfAnyType;

// Write to file!
file << "matName" << someMatrixOfAnyType;

文件扩展名可以是 xml 或 yml.在这两种情况下,您都会获得一个可以轻松删除/解析的小标题,然后您就可以访问浮点格式的数据.我成功地使用了这种方法(使用 yml 文件)将数据导入 Matlab 和 Matplotlib

The file extension can be xml or yml. In both cases you get a small header that you can easily remove/parse, then you have access to the data in a floating point format. I used this approach successfully (with yml files) to get data into Matlab and Matplotlib

获取数据:

  1. 用任何编辑器打开文件
  2. 然后抑制除数据标签内容(即像素值)之外的所有文本和数字.
  3. 完成后,使用 txt 或 csv 扩展名保存文件,然后使用 matlab(拖放操作)打开它.

瞧.如果没有正确猜测图像大小,您可能需要在 matlab 命令行中对结果矩阵进行整形.

Voilà. You may have to reshape the resulting matrix in matlab command line if it didn't guess correctly the image size.

相关文章