gpt4 book ai didi

c++ - OpenCV:如何检查 SparseMat 中是否存在元素

转载 作者:行者123 更新时间:2023-11-28 07:41:30 27 4
gpt4 key购买 nike

我正在使用 CV_32FC3SparseMat 并尝试执行以下操作:

int dims = 2;
int size[] = {10,10};
SparseMat m = SparseMat(dims, size, CV_32FC3);
m.ref<Vec3f>(2,5)[0] = 0.5;
m.ref<Vec3f>(2,5)[0] = 0.5;
m.ref<Vec3f>(2,5)[0] = 0.5;

if(m.find(2,6) == m.end()){
cout << " could not find 2,6" << endl;
}
if(m.find(2,5) != m.end()){
cout << "found 2, 5" << m.ref<Vec3f>(2,5)[0] << ", " << .ref<Vec3f>(2,5)[1] << ", " << .ref<Vec3f>(2,5)[2] << endl;
}

但是,查找部分不正确,我如何使用它来检查该索引处的元素是否已添加到稀疏垫中?

谢谢

最佳答案

来自SparseMat::ptr的定义:

C++: uchar* SparseMat::ptr(int i0, int i1, bool createMissing, size_t* hashval=0)

Return pointer to the matrix element. If the element is there (it is non-zero), the pointer to it is returned. If it is not there and createMissing=false, NULL pointer is returned. If it is not there and createMissing=true, the new elementis created and initialized with 0. Pointer to it is returned. If the optional hashval pointer is not NULL, the element hash value is not computed but hashval is taken instead.

所以在你的代码中

m.find(2,6) == m.end()
m.find(2,5) != m.end()

应该是

m.ptr(2,6,false) == NULL
m.ptr(2,5,false) != NULL

关于c++ - OpenCV:如何检查 SparseMat 中是否存在元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15762174/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com