2017-06-21 103 views
0

嗨我試圖導出使用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 R2017aMicrosoft Visual C++ 2017 (C)編譯

回答

0

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中提取字段的錯誤。

+0

嗯這是不好的:/你建議任何其他方式來使用這個代碼「outsite」的matlab IDE? – user1234

+0

你可以試試MATLAB編譯器來編譯這段代碼並創建一個獨立的可執行文件。那會在MATLAB之外運行。 – Navan

相關問題