用c++语言在opencv中将数组存入txt文件
时间: 2023-12-31 17:06:57 浏览: 33
以下是将数组存入txt文件的C代码示例,使用了OpenCV库中的FileStorage类:
```c
#include "opencv2/opencv.hpp"
#include <iostream>
#include <fstream>
using namespace cv;
using namespace std;
int main()
{
// 定义数组
int arr[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
// 创建FileStorage对象,将数组写入txt文件
FileStorage fs("arr.txt", FileStorage::WRITE);
fs << "arr" << arr;
fs.release();
// 读取txt文件中的数组
FileStorage fs2("arr.txt", FileStorage::READ);
int arr2[3][4];
fs2["arr"] >> arr2;
// 输出读取的数组
for(int i=0; i<3; i++) {
for(int j=0; j<4; j++) {
cout << arr2[i][j] << " ";
}
cout << endl;
}
return 0;
}
```
在这个示例中,我们首先定义了一个3x4的整数数组,然后使用OpenCV的FileStorage类将其写入名为“arr.txt”的文件中。我们还使用FileStorage类读取该文件并将数据存储在一个新的数组中,并输出该数组以确保正确读取。