如何在不分配内存的情况下将openv映射到具有对齐支持的特征
How to map opencv to eigen with alignment support without allocate the memory?
我使用opencv从一些数据集(28*28行,200000 cols)读取大数据,并希望将其映射到具有对齐支持的特征矩阵,而无需分配另一个大缓冲区。
cv::Mat big_data(28*28, 200000, CV_64F);
//...read data and preprocess
EMatrix map_big_data;
//cv2Eigen will allocate a new, big buffer
cv::cv2Eigen(big_data, map_big_data);
是否有可能映射矩阵而不分配另一个大内存?这是可以调整cv::Mat的大小,我想避免两个大的缓冲区同时存在(可能抛出bad_alloc)
cv::Mat big_data(28*28, 200000, CV_64F);
//read data and preprocess....
using EMatrix = Eigen::Matrix<double, Eigen::Dynamic,
Eigen::Dynamic, Eigen::RowMajor>;
using EMap = Eigen::Map<EMatrix, Eigen::Aligned>;
//resize the cv::Mat to meet the alignment requirement(16bytes aligned),is this possible?How could I get the align_cols?
big_data.resize(big_data.rows, align_cols);
//this may or may not crash
EMap map_big_data(reinterpret_cast<double*>(big_data.data),
big_data.rows,
big_data.cols);
编辑:使用Eigen::aligned_allocator来分配对齐内存
Eigen::aligned_allocator<double> alloc;
int const Rows = 28*28;
int const Cols = 200000;
auto buffer = alloc.allocate(Rows*Cols);
cv::Mat big_data(Rows, Cols, CV_64F, buffer, Cols * sizeof(double));
//read data set and do some preprocess....
using EMatrix = Eigen::Matrix<double, Eigen::Dynamic,
Eigen::Dynamic, Eigen::RowMajor>;
using EMap = Eigen::Map<EMatrix, Eigen::Aligned>;
EMap map_big_data(reinterpret_cast<double*>(big_data.data),
big_data.rows,
big_data.cols);
//some jobs.....
alloc.deallocate(buffer, 0);
这样可以吗?
为什么要直接使用特征分配器呢?使用Eigen::MatrixXd(或类似的),并将指向数据的指针传递给cvMat构造函数。使用这种方式,cvMat"包装"数据而不复制它(与Eigen::Map相同)。
int Rows = 28*28;
int Cols = 200000;
Eigen::Matrix<double, Eigen::Dynamic,
Eigen::Dynamic, Eigen::RowMajor> eigenMat(Rows, Cols);
cv::Mat big_data(Rows, Cols, CV_64F, eigenMat.data(), Cols * sizeof(double));
//read data set and do some preprocess....
你唯一不应该做的是尝试调整big_data
的大小,但eigenMat
如果必要的话。
来自OpenCV文档:
指向用户数据的指针。接受数据和步进参数的矩阵构造函数不分配矩阵数据。相反,它们只是初始化指向指定数据的矩阵头,这意味着不会复制任何数据。这个操作非常高效,可以使用OpenCV函数来处理外部数据。外部数据不会自动释放,所以您应该注意它。
这样就没有映射,不需要尝试确定是否有额外的填充,也不需要记住解除分配。同时还获得了特征矩阵的所有优点
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 将函数类成员映射到类本身内部
- 如何在 C# 中映射双 C 结构指针?
- 如何在C++中使用结构生成映射
- 使用std::函数映射对象方法
- 如何加载(或映射)文件部分的最大大小,但适合在Windows上的RAM
- C++映射分割错误(核心转储)
- 内联映射初始化的动态atexit析构函数崩溃
- 如何将高维数据映射到特征类型?
- 具有 2 个分量的数组的特征映射到 3 的向量
- 特征:将数组映射到矩阵大小未知的矩阵
- 将 BGR OpenCV 垫映射到特征张量
- 在OpenMP约简中使用特征映射
- 将数组映射回现有特征矩阵
- 使用特征映射复杂矩阵
- 映射特征矩阵Ref对象与块不一致
- 将c++数组映射到特征矩阵
- Rcpp特征映射错误与MatrixXf
- 如何在不分配内存的情况下将openv映射到具有对齐支持的特征