将3D MatND拆分为2D Mat opencv的向量 [英] Split 3D MatND into vector of 2D Mat opencv

查看:238
本文介绍了将3D MatND拆分为2D Mat opencv的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有可能从opencv中存储为MatND的3D数据多维数据集获取2D Mat对象吗?基本上,我使用mexopencv传递一个3D矩阵到MexFile。我使用MxArray(prhs [0])。toMatND()将矩阵转换为MatND对象。现在我想把这个数据立方体沿着第三维分解成一个cv :: Mat矩阵的向量。我需要对这些2D矩阵进行操作,因此在第三维上进行迭代。
是否有根据需要拆分数据立方体的功能?或者也许是一个指向3D数据立方体的2D子矩阵的方法?



编辑:这是我的代码,它使用mexopencv将Matlab输入参数转换为MatND数组。我实现@ chappjc的方法,将3D数据代码拆分为2D矩阵的向量。除了x和y尺寸被切换的事实,一切都很好。

  #includemexopencv.hpp
#include< iostream>

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

// 1)将MxArray转换为cv :: Mat
cv :: MatND matnd = MxArray(prhs [0])。toMatND();

//从矩阵中提取平面
int dims [] = {matnd.size [0],matnd.size [1],matnd.size [2]};
std :: vector< cv :: Mat> matVec;
for(int p = 0; p double * ind =(double *)matnd.data + p * dims [0] * dims [1 ]; //子矩阵指针
matVec.push_back(cv :: Mat(2,dims,CV_64F,ind).clone()); // clone if mnd go away
}

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

//这里我将使用matVec的二维子矩阵做一些事情
// ...


// 2)在这里我想将3D矩阵传递回Matlab
//我只知道如何使用mexopencv将cv :: Mat转换回mxArray *:
plhs [0] = MxArray(matnd);
}

事实上,尺寸在matVec中切换的事实是很讨厌的。有没有人有更好的解决方案?



这是一个小[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
/ pre>

解决方案

一个聪明的法师曾经说过:不要尝试拆分 / code>。这不可能。相反...只是试图实现真相。没有 MatND






MatND 已过时,现在 typedef 'd到 Mat 。在opencv2 / core / core.hpp中:

  typedef Mat MatND; 

这意味着你可以像一个 Mat 并手动切割。我相信 ptr 方法不能像dims> 2的预期工作,所以你可以抓住 Mat :: data 指针并计算子矩阵的位置。有一个 ptr(int i0,int i1,int i2)方法,但我没有太多运气,因为 step [] 对于多维数组很奇怪。



示例 pre> //使用元素索引作为内容创建3D矩阵
int dims [] = {5,5,3};
cv :: Mat mnd(3,dims,CV_64F);
for(int i = 0; i *((double *)mnd.data + i)=(double)i;

//从矩阵中提取平面
std :: vector< cv :: Mat> matVec;
for(int p = 0; p double * ind =(double *)mnd.data + p * dims [0] * dims [1 ]; //子矩阵指针
matVec.push_back(cv :: Mat(2,dims,CV_64F,ind).clone()); // clone if mnd go away
}

std :: cout<< Size of matVec:<< matVec.size()< std :: endl;
std :: cout<< 第一Mat的大小:< matVec [0] .size()<< std :: endl;

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

输出

  matVec的大小:3 
第一个Mat的大小:[5 x 5]

matVec [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]


Is it possible to get a 2D Mat object from a 3D data cube stored as MatND in opencv? Basically I'm passing a 3D matrix to a MexFile using "mexopencv". I convert the matrix to a MatND object by using MxArray(prhs[0]).toMatND(). Now I want to split up this datacube along the third dimension into a vector of cv::Mat matrices. I need to make operations on these 2D matrices an 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 the 2D sub matrices of the 3D data cube?

Edit: This is my code which uses mexopencv to convert the Matlab input arguments to MatND arrays. I implemented @chappjc's method of splitting up the 3D data code into a vector of 2D matrices. Apart from the fact that x and y dimensions are switched 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. Actually the fact that the dimensions are switched in "matVec" is pretty annoying. Does anyone have a better solution?

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

>> 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

解决方案

A wise mage once said: Do not try to split the MatND. That's impossible. Instead... only try to realize the truth. There is no MatND.


MatND is obsolete and it's now typedef'd to Mat. In opencv2/core/core.hpp:

 typedef Mat MatND;

This means you can just treat it just like a Mat and cut it up manually. I believe the at and ptr methods don't work as expected for dims>2, so you can just grab the Mat::data pointer and compute the location of the sub-matrix. There is a ptr(int i0, int i1, int i2) method, but I have not had much luck with it because the step[] for multi-dimensional arrays is strange.

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]

这篇关于将3D MatND拆分为2D Mat opencv的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆