Divide 3D MatND by vector 2D Mat opencv

Is it possible to get a 2D Mat object from a 3D data cube saved as a MatND in opencv? Basically I am passing a 3D matrix to MexFile using "mexopencv". I am converting a matrix to a MatND object using MxArray (prhs [0]). ToMatND (). Now I want to split this datacube by third dimension into matrix vector cv :: Mat. I need to perform operations on these 2-dimensional matrices and therefore iterate over the third dimension. Is there a function to split the data cube as needed? Or maybe a way to get a pointer to 2D submatrices of a 3D data cube?

Edit: This is my code that uses mexopencv to convert Matlab input arguments to MatND arrays. I used @chappjc's method to split the 3D code into a vector of 2D matrices. Apart from the fact that the x and y sizes are toggled, everything is fine.

#include "mexopencv.hpp"
#include <iostream>

void mexFunction(int nlhs, mxArray *plhs[],
                 int nrhs, const mxArray *prhs[])
{
    // Check arguments
    if (nlhs!=1 || nrhs!=1)
        mexErrMsgIdAndTxt("myfunc:invalidArgs", "Wrong number of arguments");

    // 1) Convert MxArray to cv::Mat
    cv::MatND matnd = MxArray(prhs[0]).toMatND();

    // Extract planes from matrix
    int dims[] = { matnd.size[0],matnd.size[1],matnd.size[2]};
    std::vector<cv::Mat> matVec;
    for (int p = 0; p < dims[2]; ++p) {
        double *ind = (double*)matnd.data + p * dims[0] * dims[1]; // sub-matrix pointer
        matVec.push_back(cv::Mat(2, dims, CV_64F, ind).clone()); // clone if mnd goes away
    }

    std::cout << "\nmatVec[0]:\n" << matVec[0] << std::endl;
    std::cout << "\nmatVec[1]:\n" << matVec[1] << std::endl;

    // Here I will do some stuff with the 2D submatrices from matVec
    // ...


    // 2) Here I want to pass the 3D matrix back to Matlab
    // I only know how to convert cv::Mat back to mxArray* using mexopencv:
    plhs[0] = MxArray(matnd);
}

      

2nd Edit. In fact, the fact that the sizes are switched in "matVec" is rather annoying. Anyone have a better solution?

This is the result of a small example [5 x 4 x 2]:

>> b

b(:,:,1) =

     1     6    11    16
     2     7    12    17
     3     8    13    18
     4     9    14    19
     5    10    15    20


b(:,:,2) =

   101   106   111   116
   102   107   112   117
   103   108   113   118
   104   109   114   119
   105   110   115   120

>> c = cv.myFunc(b)

matVec[0]:
[1, 2, 3, 4, 5;
  6, 7, 8, 9, 10;
  11, 12, 13, 14, 15;
  16, 17, 18, 19, 20]

matVec[1]:
[101, 102, 103, 104, 105;
  106, 107, 108, 109, 110;
  111, 112, 113, 114, 115;
  116, 117, 118, 119, 120]

c(:,:,1) =

     1     6    11    16
     2     7    12    17
     3     8    13    18
     4     9    14    19
     5    10    15    20


c(:,:,2) =

   101   106   111   116
   102   107   112   117
   103   108   113   118
   104   109   114   119
   105   110   115   120

      

+3


source to share


1 answer


A wise magician once said: don't try to divide MatND

. It's impossible. Instead ... just try to understand the truth. No MatND

.


MatND

deprecated and now typedef

'd before Mat

. In opencv2 / core / core.hpp:

 typedef Mat MatND;

      

This means you can simply process it the same way Mat

and cut it off by hand. I believe the methods at

and ptr

do not work as expected for dims> 2, so you can just grab the pointer Mat::data

and calculate the location of the submatrix. There is a method ptr(int i0, int i1, int i2)

, but I had no luck with it because it is step[]

strange for multidimensional arrays.



Example

// create 3D matrix with element index as content
int dims[] = { 5, 5, 3 };
cv::Mat mnd(3, dims, CV_64F);
for (int i = 0; i < mnd.total(); ++i)
    *((double*)mnd.data+i) = (double)i;

// extract planes from matrix
std::vector<cv::Mat> matVec;
for (int p = 0; p < dims[2]; ++p) {
    double *ind = (double*)mnd.data + p * dims[0] * dims[1]; // sub-matrix pointer
    matVec.push_back(cv::Mat(2, dims, CV_64F, ind).clone()); // clone if mnd goes away
}

std::cout << "Size of matVec: " << matVec.size() << std::endl;
std::cout << "Size of first Mat: " << matVec[0].size() << std::endl;

std::cout << "\nmatVec[0]:\n" << matVec[0] << std::endl;
std::cout << "\nmatVec[1]:\n" << matVec[1] << std::endl;
std::cout << "\nmatVec[2]:\n" << matVec[2] << std::endl;

      

Output

Size of matVec: 3
Size of first Mat: [5 x 5]

matVec[0]:
[0, 1, 2, 3, 4;
  5, 6, 7, 8, 9;
  10, 11, 12, 13, 14;
  15, 16, 17, 18, 19;
  20, 21, 22, 23, 24]

matVec[1]:
[25, 26, 27, 28, 29;
  30, 31, 32, 33, 34;
  35, 36, 37, 38, 39;
  40, 41, 42, 43, 44;
  45, 46, 47, 48, 49]

matVec[2]:
[50, 51, 52, 53, 54;
  55, 56, 57, 58, 59;
  60, 61, 62, 63, 64;
  65, 66, 67, 68, 69;
  70, 71, 72, 73, 74]

      

+6


source







All Articles