我正在使用opencv将一些matlab代码翻译成c.我想获得满足条件的Matrix的值.我为此创建了一个蒙版,当我将它应用到原始矩阵时,我得到的原始矩阵大小相同,但是掩码中没有0值.但我的问题是如何才能得到矩阵中非零的值并将其分配给不同的矩阵.
我的matlab代码是:
for i= 1:size(no,1) mask= labels==i; op = orig(mask,:); //op only contains the values from the orig matrix which are in the mask. So orig size and op size is not the same ..... end
我现在的c翻译是:
for (int i=0; i<n.rows; i++) { Mat mask; compare(labels,i,mask,CMP_EQ); Mat op; orig.copyTo(op,mask); //Here the orig size and the op size is always same but values which are not in the mask are 0 }
那么,我怎样才能创建一个只有掩码满足的值的矩阵?
解决方法
您可能会尝试使用cv :: SparseMat(
http://docs.opencv.org/modules/core/doc/basic_structures.html#sparsemat),它只在散列中保留非零值.
将常规cv :: Mat分配给cv :: SparseMat时,它会自动捕获非零值.从那时起,您可以迭代非零值并根据需要对其进行操作.
希望我有正确的问题,这有帮助!