qt使用opencv3将mat格式压缩成python的scipy.io生成的mat的格式
时间: 2024-05-13 14:17:29 浏览: 15
首先,将 OpenCV 中的 Mat 转换为 NumPy 数组:
```python
import numpy as np
import cv2
# 读取图像
img = cv2.imread('example.jpg')
# 将 OpenCV 中的 Mat 转换为 NumPy 数组
img_np = np.array(img)
```
然后,使用 SciPy 库中的 `scipy.io.savemat()` 函数将 NumPy 数组保存为 mat 文件:
```python
from scipy.io import savemat
# 保存 NumPy 数组为 mat 文件
savemat('example.mat', {'img': img_np})
```
这里将 NumPy 数组保存为名为 `example.mat` 的 mat 文件,并将其命名为 `img`。你可以根据需要更改这些名称。
相关问题
qt使用opencv将mat数据保存为python的scipy.io.savemat的格式
可以使用OpenCV和SciPy中的函数来实现将Mat数据保存为Python的Scipy.io.savemat格式。下面是一个简单的示例代码:
```c++
#include <opencv2/opencv.hpp>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <cstdlib>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <boost/filesystem.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/algorithm/string.hpp>
#include "json.hpp"
using namespace std;
using namespace cv;
namespace pt = boost::property_tree;
using json = nlohmann::json;
int main(int argc, char** argv) {
// 读取Mat数据
Mat img = imread("test.jpg");
// 将Mat数据转换为数组形式
vector<int> img_data;
for(int i=0;i<img.rows;i++) {
for(int j=0;j<img.cols;j++) {
Vec3b pixel = img.at<Vec3b>(i, j);
img_data.push_back(pixel[0]);
img_data.push_back(pixel[1]);
img_data.push_back(pixel[2]);
}
}
// 定义保存数据的json对象
json data;
data["img"] = img_data;
// 将json对象保存为文件
ofstream out_file("test.mat");
out_file << data;
out_file.close();
return 0;
}
```
这个示例程序将OpenCV读取的图像数据转换为一个vector<int>形式的数组,然后将这个数组保存到一个json对象中,最后使用ofstream将这个json对象保存到文件中。由于Scipy.io.savemat格式也是以JSON格式保存数据的,所以这个程序可以将Mat数据保存为Scipy.io.savemat格式。
qt使用opencv3将mat格式压缩成matlab的格式
您可以使用OpenCV的FileStorage类来将Mat格式数据压缩为Matlab格式数据。以下是一个示例代码:
```c++
#include <opencv2/core.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat mat = imread("example.jpg", IMREAD_GRAYSCALE);
// 将Mat格式数据压缩为Matlab格式数据
FileStorage fs("example.yml", FileStorage::WRITE);
fs << "data" << mat;
fs.release();
return 0;
}
```
上面的代码将读取名为“example.jpg”的灰度图像,将其存储为Mat格式数据,然后使用FileStorage类将其压缩为Matlab格式数据,并将其保存到名为“example.yml”的文件中。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)