我想在 OpenCV 中制作一个稀疏矩阵。
我怎样才能对这个矩阵进行基本操作,例如:
从矩阵元素中放置或访问数据。
干杯
最佳答案
使用 C++ 接口(interface)可能更合适。请注意,文档 [1] 中的示例代码缺少模运算,因此失败。
const int dims = 2;
int size[] = {3, 20}; // rows and columns if in two dimensions
SparseMat sparse_mat(dims, size, CV_32F);
for(int i = 0; i < 1000; i++) {
// create a random index in dims dimensions
int idx[dims];
for(int k = 0; k < dims; k++)
idx[k] = rand() % size[k];
sparse_mat.ref<float>(idx) += 1.f;
}
cout << "bottom right element @ (2,19) = " << sparse_mat.ref<float>(2,19) << "\n";
Mat dense;
sparse_mat.convertTo(dense, CV_32F);
cout << dense;
给出以下输出
bottom right element @ (2,19) = 19
[9, 23, 13, 26, 18, 13, 18, 15, 13, 17, 13, 18, 19, 6, 20, 20, 12, 15, 15, 15;
17, 17, 14, 16, 12, 14, 17, 15, 15, 18, 24, 18, 13, 22, 18, 11, 18, 22, 17, 15;
19, 16, 14, 10, 18, 19, 10, 17, 18, 15, 24, 22, 18, 18, 18, 23, 21, 16, 14, 19]
[1] OpenCV 引用手册。版本 2.4.3。 2012,页。 46.
关于c - 我如何使用 Opencv SparseMatrix,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10323201/