qt使用opencv将二维数据转为二进制数据存入mat中
时间: 2024-05-12 09:14:45 浏览: 265
可以使用 `cv::Mat` 的构造函数来将二维数据转换为 `cv::Mat` 对象,然后使用 `cv::imencode()` 函数将 `cv::Mat` 对象编码为二进制数据。
以下是示例代码:
```cpp
#include <opencv2/opencv.hpp>
#include <vector>
int main()
{
// 二维数据
std::vector<std::vector<int>> data = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
// 转换为 cv::Mat 对象
cv::Mat mat(data.size(), data[0].size(), CV_8UC1);
for (int i = 0; i < mat.rows; ++i)
{
for (int j = 0; j < mat.cols; ++j)
{
mat.at<uint8_t>(i, j) = data[i][j];
}
}
// 编码为二进制数据
std::vector<uint8_t> binaryData;
cv::imencode(".jpg", mat, binaryData);
return 0;
}
```
在上述代码中,`data` 是一个二维整数向量,`mat` 是将 `data` 转化为的 `cv::Mat` 对象,`binaryData` 是存储编码后的二进制数据的向量。注意,`cv::imencode()` 函数可以指定编码格式,这里使用了 JPG 格式。
阅读全文
相关推荐















