How to access slices of a 3d matrix in OpenCV

I would like to store 592 47x47 arrays in a 47x47x592 matrix. I created a 3D matrix as follows:

int sizes[] = {47,47,592};
Mat 3dmat(3, sizes, CV_32FC1);

Then I thought that I could access it using a set of ranges, as shown below.

Range ranges[3];
ranges[0] = Range::all();
ranges[1] = Range::all();
ranges[2] = Range(x,x+1) //within a for loop.
Mat 2dmat = 3dmat(ranges);

However, when I try to use the copyTo function to enter an existing dataset, it does not work.

data.copyTo(2dmat); //data is my 47x47 matrix

The 3D matrix does not update when I do this.

Any information is appreciated! Thank!

edit: I store 592 matrices in this three-dimensional matrix so that I can then later access each of the individual 47x47 matrices in a different loop. So I will do something similar later:

2dmat = 3dmat(ranges);
2dmat.copyTo(data);

Therefore, I would perform some operations using this data matrix. And in the next iteration of the loop, I will use the next saved data matrix.

+4
2

, :

std::vector<cv::Mat> mat(592, cv::Mat(47, 47, CV_32FC1)); // allocates 592 matrices sized 47 by 47
for(auto &m: mat) {
    // do your processsing here
    data.copyTo(m);
}
+4

, :

cv::Mat slice = mat3D(ranges).clone();
cv::Mat mat2D;
mat2D.create(2, &(mat3D.size[0]), mat3D.type());
slice.copySize(mat2D);

2D-.

+1

Source: https://habr.com/ru/post/1542952/


All Articles