简体   繁体   English

在MATLAB Coder中将mxarrays与step函数一起使用时遇到的问题

[英]Problems with using mxarrays with the step function in MATLAB Coder

I'm trying to convert my image processing code on MATLAB into C by using MATLAB coder. 我正在尝试通过使用MATLAB编码器将我在MATLAB上的图像处理代码转换为C。 using imread needs a coder.entrinsic declaration. 使用imread需要coder.entrinsic声明。 However, this means that the output to imread will be an mxArray . 然而,这意味着输出imread将是一个mxArray。 This is a problem as I cannot use this with the step function. 这是一个问题,因为我不能将其与step函数一起使用。 The error report from code generation is shown below: 代码生成的错误报告如下所示:

Does anyone know a way around this? 有谁知道解决这个问题的方法吗?

When using coder.extrinsic, functions declared as extrinsic will return mxArray types. 使用coder.extrinsic时,声明为extrinsic的函数将返回mxArray类型。 If you pass these to other Matlab functions, Coder will resolve everything well, but if you use them with your own functions or in any way try to manipulate them, you need to help Coder resolve them into a known type. 如果将它们传递给其他Matlab函数,Coder会很好地解决所有问题,但是,如果将它们与自己的函数一起使用或以任何方式尝试对其进行操作,则需要帮助Coder将它们解析为已知类型。 You do this by pre-defining a variable and copying the mxArray into it, which will allow Coder to correctly convert to a standard data type. 您可以通过预定义变量并将mxArray复制到其中来完成此操作,这将使Coder可以正确转换为标准数据类型。 If you know the exact size beforehand, you can preallocate the variable before the call and skip the copy, but in this case it may be a bit trickier. 如果您事先知道确切的大小,则可以在调用之前预分配变量,然后跳过副本,但是在这种情况下,它可能会有些棘手。

In the case of your function, I assume you have a call somewhere that looks like: 对于您的函数,我假设您在某个地方有一个调用,如下所示:

I = imread([some paramaters]);

We need to get the mxArray type from the call to imread, then determine its dimensions so that another variable can be allocated in a native type. 我们需要从要读取的调用中获取mxArray类型,然后确定其尺寸,以便可以在本机类型中分配另一个变量。 The determination of the mxArray dimensions using the size function itself needs to have a preallocated variable so that size does not return a mxArray also. 使用size函数本身确定mxArray的尺寸需要预先分配一个变量,以便size也不会返回mxArray。 Here are the steps: 步骤如下:

coder.extrinsic('imread');
Itemp = imread([some paramaters]);
idims = zeros(1,3); %Need to preallocate idims so it does not become an mxArray
idims = size(Itemp)
I = coder.nullcopy(zeros(idims, 'uint8'));  % Allocate but do not initialize an array of uint8 values of the same size as Itemp
I = Itemp; % Copy the data from the mxArray into the final variable.

If you know the exact size of the image before the call to imread, you can skip the copy and the second variable and simply preallocate the variable I to the correct size, but this is not typically the case for an image read. 如果在调用读取之前知道图像的确切大小,则可以跳过副本和第二个变量,而只是将变量I预分配给正确的大小,但是读取图像通常不是这种情况。

You can see a little more information on how to do this in the following help document from Mathworks: 您可以在Mathworks的以下帮助文档中找到有关如何执行此操作的更多信息:

http://www.mathworks.com/help/simulink/ug/calling-matlab-functions.html#bq1h2z9-47 http://www.mathworks.com/help/simulink/ug/calling-matlab-functions.html#bq1h2z9-47

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM