Chris Su
Chris Su

Reputation: 543

How to zero elements of 3D Matrix with opencv Library?

I'm struggling with zeroing elements of 3D matrix with opencv. I can zero all elements in 2D matrix like following way:

meta = new Mat(Mat::zeros(cluster,3,CV_32S));

I try to use the similar way to initialize elements with 0 in 3D matrix, it fails.

block = new Mat(Mat::zeros(3,dim,CV_32F));

Error message:

1>MatrixOp.obj : error LNK2019: unresolved external symbol "public: static class cv::MatExpr __cdecl cv::Mat::zeros(int,int const *,int)" (?zeros@Mat@cv@@SA?AVMatExpr@2@HPBHH@Z) referenced in function "public: __thiscall MatrixOp::MatrixOp(char *)" (??0MatrixOp@@QAE@PAD@Z)

I got one last way to initialize matrix. Traverse the matrix and set element value 0. But it seems labor-some.

for(int i=0;i<value_num;i++)
    for(int j=0;j<frame_no;j++)
        for(int k=0;k<cluster;k++)
            block->at<float>(i,j,k) = 0;

Can anyone throw me a better ideas? Thanks.

Upvotes: 1

Views: 8078

Answers (1)

Michele mpp Marostica
Michele mpp Marostica

Reputation: 2472

I remind you the documentation, you can use this constructor:

C++: Mat::Mat(int ndims, const int* sizes, int type, const Scalar& s)

passing cv::Scalar(0) should be good for your purposes.

int sizes[] = { 100, 100, 100 };
cv::Mat *matrix = new cv::Mat(3, sizes, CV_32FC1, cv::Scalar(0));

This should make a 3D matrix, a sort of cube with edge 100.

Upvotes: 6

Related Questions