#include <stdio.h>
#include "opencv2/highgui/highgui.hpp" 
#include "opencv2/imgproc/imgproc.hpp"
using namespace std;

void main()
{
    int size[] = { 10,10 };
    cv::SparseMat sm(2, size, CV_32F);
    for (int i = 0; i < 10; i++)
    {
        int idx[2];
        idx[0] = size[0] * rand();
        idx[1] = size[1] * rand();
        //Fill the array
        sm.ref<float>(idx) += 1.0f;
        //Print out the nonzero elements
        //
    }
    cv::SparseMatConstIterator_<float>it = sm.begin<float>();
    cv::SparseMatConstIterator_<float>it_end = sm.end<float>();
    for (; it != it_end; ++it)
    {
        const cv::SparseMat::Node* node = it.node();
        printf("(%3d,%3d)%f\n", node->idx[0], node->idx[1], *it);
    }
    return;
}

结果打印:

Opencv 8 (打印一个稀疏矩阵中的所有非0元素)-LMLPHP

12-23 20:48