qt使用opencv的filestorage将mat数据
时间: 2024-04-29 15:21:21 浏览: 177
保存到xml文件中,可以按照以下步骤进行操作:
1. 引入头文件
```cpp
#include <opencv2/opencv.hpp>
```
2. 创建Mat对象并初始化
```cpp
cv::Mat mat = cv::Mat::zeros(3, 3, CV_32FC1);
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
mat.at<float>(i,j) = i+j;
}
}
```
3. 创建FileStorage对象并打开文件
```cpp
cv::FileStorage fs("data.xml", cv::FileStorage::WRITE);
```
4. 将Mat数据写入文件
```cpp
fs << "mat" << mat;
```
5. 关闭文件
```cpp
fs.release();
```
完整代码如下:
```cpp
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat mat = Mat::zeros(3, 3, CV_32FC1);
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
mat.at<float>(i,j) = i+j;
}
}
FileStorage fs("data.xml", FileStorage::WRITE);
fs << "mat" << mat;
fs.release();
return 0;
}
```
运行后,会在当前目录下生成一个名为data.xml的文件,其中保存了Mat对象的数据。
阅读全文
相关推荐

















