读取MEX文件中的4D阵列 [英] Read 4D Array in MEX File

查看:70
本文介绍了读取MEX文件中的4D阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MATLAB中有一个4维数组;我正在尝试在MEX函数中访问数组.下面创建"testmatrix",这是一个4维矩阵,已知数据为uint8类型.

I have a 4 dimensional array in MATLAB; I am trying to access the array in a MEX function. The following creates 'testmatrix', a matrix of 4 dimensions with known data of uint8 type.

%Create a 4D array 2x,2y, rgb(3), framenumber(from 1 to 5)
%Framenumber from 1 to 5

testmatrix(1,1,1,1) = 0;
testmatrix(1,1,1,2) = 1;
testmatrix(1,1,1,3) = 2;
testmatrix(1,1,1,4) = 3;
testmatrix(1,1,1,5) = 4;

testmatrix(1,1,2,1) = 5;
testmatrix(1,1,2,2) = 6;
testmatrix(1,1,2,3) = 7;
testmatrix(1,1,2,4) = 8;
testmatrix(1,1,2,5) = 9;

testmatrix(1,1,3,1) = 10;
testmatrix(1,1,3,2) = 11;
testmatrix(1,1,3,3) = 12;
testmatrix(1,1,3,4) = 13;
testmatrix(1,1,3,5) = 14;

testmatrix(1,2,1,1) = 15;
testmatrix(1,2,1,2) = 16;
testmatrix(1,2,1,3) = 17;
testmatrix(1,2,1,4) = 18;
testmatrix(1,2,1,5) = 19;

testmatrix(1,2,2,1) = 20;
testmatrix(1,2,2,2) = 21;
testmatrix(1,2,2,3) = 22;
testmatrix(1,2,2,4) = 23;
testmatrix(1,2,2,5) = 24;

testmatrix(1,2,3,1) = 25;
testmatrix(1,2,3,2) = 26;
testmatrix(1,2,3,3) = 27;
testmatrix(1,2,3,4) = 28;
testmatrix(1,2,3,5) = 29;

testmatrix(2,1,1,1) = 30;
testmatrix(2,1,1,2) = 31;
testmatrix(2,1,1,3) = 32;
testmatrix(2,1,1,4) = 33;
testmatrix(2,1,1,5) = 34;

testmatrix(2,1,2,1) = 35;
testmatrix(2,1,2,2) = 36;
testmatrix(2,1,2,3) = 37;
testmatrix(2,1,2,4) = 38;
testmatrix(2,1,2,5) = 39;

testmatrix(2,1,3,1) = 40;
testmatrix(2,1,3,2) = 41;
testmatrix(2,1,3,3) = 42;
testmatrix(2,1,3,4) = 43;
testmatrix(2,1,3,5) = 44;

testmatrix(2,2,1,1) = 45;
testmatrix(2,2,1,2) = 46;
testmatrix(2,2,1,3) = 47;
testmatrix(2,2,1,4) = 48;
testmatrix(2,2,1,5) = 49;

testmatrix(2,2,2,1) = 50;
testmatrix(2,2,2,2) = 51;
testmatrix(2,2,2,3) = 52;
testmatrix(2,2,2,4) = 53;
testmatrix(2,2,2,5) = 54;

testmatrix(2,2,3,1) = 55;
testmatrix(2,2,3,2) = 56;
testmatrix(2,2,3,3) = 57;
testmatrix(2,2,3,4) = 58;
testmatrix(2,2,3,5) = 59;

testmatrix = uint8(testmatrix);

但是,当我尝试使用mxGetNumberOfDimensions时,我得到的尺寸不正确,如下所示:

However, I am getting incorrect dimensions when I try to use mxGetNumberOfDimensions my code is as follows:

#include "mex.h"

void
mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
//Check - expect 1 input, expect 1 outputs
    if (nrhs != 1) {
        mexErrMsgIdAndTxt( "MATLAB:24bit_pixel_from_uint8_rgb:invalidNumInputs",
                "One input argument required.");
    }
    if (nlhs > 1) {
        mexErrMsgIdAndTxt( "MATLAB:24bit_pixel_from_uint8_rgb:maxlhs",
                "Too many output arguments.");
    }    

//declare variables
    mxArray *a_in_m, *b_out_m;
    double *a, *b, *c, *d;
    int dimx, dimy, numdims;
    int dima, dimb, dimc, dimd;
    int i,j,M,N,L,W;
    //Comment
    mwSize *dim_array, dims;
    dims      = mxGetNumberOfDimensions(prhs[0]);
    dim_array = mxGetDimensions(prhs[0]);
    M            = dim_array[0];
    N            = dim_array[1];
    L            = dim_array[2];
    W            = dim_array[3];
    mexPrintf("dims: %d\n",dims);
    mexPrintf("M: %d\n",M);
    mexPrintf("N: %d\n",N);
    mexPrintf("L: %d\n",L);
    mexPrintf("W: %d\n",W);

}

此cx文件在testarray上的结果如下:

The result of this mex file on testarray follows:

>> rgb_to_uint8 testmatrix
dims: 2
M: 1
N: 10
L: 1634887535
W: 1766203501

dims对于第一个元素是正确的,但其他所有内容都不对-我必须做一些根本上不正确的事情.任何想法或朝着正确方向的指针将不胜感激;谢谢大家!

dims is correct for the first element, but everything else is wrong - I must be doing something fundamentally incorrect. Any thoughts or a pointer in the right direction would be appreciated; thanks all!

推荐答案

调用

rgb_to_uint8 testmatrix

等效于这样称呼

rgb_to_uint8('testmatrix')

与此相同:

c = 'testmatrix'
rgb_to_uint8(c)

当然是大小为1x10(2D)的字符数组.您需要像函数一样调用:

which of course is a character array of size 1x10 (2D). You need to call like a function:

rgb_to_uint8(testmatrix)

这篇关于读取MEX文件中的4D阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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