使用 MATLAB 编码器将代码从注册估算器应用程序导出到C++

Using MATLAB coder to export code from Registration Estimator app to C++

本文关键字:应用程序 C++ 编码器 MATLAB 代码 注册 使用      更新时间:2023-10-16

嗨,我正在尝试导出从 MATLAB 中的注册估算器应用程序自动生成的"默认代码",以使用 MATLAB Coder 工具进行C++。

这是我今天生成的示例代码:

function [MOVINGREG] = registerImages(MOVING,FIXED)
%registerImages  Register grayscale images using auto-generated code from Registration Estimator app.
%  [MOVINGREG] = registerImages(MOVING,FIXED) Register grayscale images
%  MOVING and FIXED using auto-generated code from the Registration
%  Estimator App. The values for all registration parameters were set
%  interactively in the App and result in the registered image stored in the
%  structure array MOVINGREG.
% Auto-generated by registrationEstimator app on 21-Jun-2017
%-----------------------------------------------------------

% Convert RGB images to grayscale
FIXED = rgb2gray(FIXED);
MOVING = rgb2gray(MOVING);
% Default spatial referencing objects
fixedRefObj = imref2d(size(FIXED));
movingRefObj = imref2d(size(MOVING));
% Intensity-based registration
[optimizer, metric] = imregconfig('monomodal');
optimizer.GradientMagnitudeTolerance = 1.00000e-04;
optimizer.MinimumStepLength = 1.00000e-05;
optimizer.MaximumStepLength = 6.25000e-02;
optimizer.MaximumIterations = 100;
optimizer.RelaxationFactor = 0.500000;
% Align centers
fixedCenterXWorld = mean(fixedRefObj.XWorldLimits);
fixedCenterYWorld = mean(fixedRefObj.YWorldLimits);
movingCenterXWorld = mean(movingRefObj.XWorldLimits);
movingCenterYWorld = mean(movingRefObj.YWorldLimits);
translationX = fixedCenterXWorld - movingCenterXWorld;
translationY = fixedCenterYWorld - movingCenterYWorld;
% Coarse alignment
initTform = affine2d();
initTform.T(3,1:2) = [translationX, translationY];
% Apply transformation
tform = imregtform(MOVING,movingRefObj,FIXED,fixedRefObj,'similarity',optimizer,metric,'PyramidLevels',3,'InitialTransformation',initTform);
MOVINGREG.Transformation = tform;
MOVINGREG.RegisteredImage = imwarp(MOVING, movingRefObj, tform, 'OutputView', fixedRefObj, 'SmoothEdges', true);
% Store spatial referencing object
MOVINGREG.SpatialRefObj = fixedRefObj;
end

在第 Run-Time Issues 节的编码器工具中,我收到了几个问题,例如,编码人员需要声明外部函数。目前为止,一切都好。例如,我添加了:coder.extrinsic('imregconfig'); coder.extrinsic('optimizer');。但我仍然收到以下错误:

尝试从"mxArray"中提取字段"梯度幅度容差"。

尝试从"mxArray"中提取字段"最小步长"。

尝试从"mxArray"中提取字段"最大步长"。

指向带有 optimizer.GradientMagnitudeTolerance = 1.00000e-04;(及下方(的行。

我发现通常缺少变量的初始化。但是我不知道如何提前初始化属性optimizer.GradientMagnitudeTolerance。谁能帮我解决这个问题?

PS:我正在使用MATLAB R2017aMicrosoft Visual C++ 2017 (C)编译器

根据 https://www.mathworks.com/help/coder/ug/functions-supported-for-code-generation--categorical-list.html#bsl0arh-1 中代码生成支持的函数列表,代码生成不支持 imregconfig。这解释了您首先遇到的问题。添加 coder.extrinsic 意味着 MATLAB Coder 生成的文件将调用 MATLAB 来运行该函数。您只能对需要 MATLAB 运行的 mex 目标执行此操作。imregconfig 不会生成任何 C 代码。使用此代码无法生成从外部应用程序使用的独立 C 代码。

当声明为 coder.extrinsic 的函数调用 MATLAB 时,它们会返回一个 mxArray。代码的其余部分只能通过将其传递给 MATLAB(即类似的外部函数(来处理此 mxArray。从Coder的角度来看,这些是不透明的类型,因此尝试从mxArray中提取字段时会出现错误。