使用MATLAB编码器将代码从注册估算器应用程序导出到C++
嗨我试图导出使用MATLAB Coder
工具从MATLAB中的注册估算器应用程序自动生成的“默认代码”为C++。使用MATLAB编码器将代码从注册估算器应用程序导出到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
在编码器工具,我收到了几个问题,例如该编码器需要declare the extrinsic。到现在为止还挺好。我加了例如:coder.extrinsic('imregconfig');
和coder.extrinsic('optimizer');
。但我仍然收到如下错误:
尝试从'mxArray'中提取字段'GradientMagnitudeTolerance'。
尝试从'mxArray'中提取字段'MinimumStepLength'。
尝试从'mxArray'中提取字段'MaximumStepLength'。
...
指着符合optimizer.GradientMagnitudeTolerance = 1.00000e-04;
(和下文)。
我发现通常变量的初始化是缺失的。但我不知道如何在高级中初始化属性optimizer.GradientMagnitudeTolerance
。谁能帮我这个?
PS:我使用MATLAB R2017a
和Microsoft 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,即类似的外部函数。从编码器的角度来看,这些是不透明的类型,因此也是有关尝试从mxArray中提取字段的错误。
嗯这是不好的:/你建议任何其他方式来使用这个代码“outsite”的matlab IDE? – user1234
你可以试试MATLAB编译器来编译这段代码并创建一个独立的可执行文件。那会在MATLAB之外运行。 – Navan