Codegen for Bundle Adjustment
Show older comments
I am facing two issues when using codegen on my code. I have attached images of my code for reference.
1) The error report recognises cameraPoses as a struct even though it is a 2x2 table when I run the code. When I take the values in cameraPoses and make camPoses (as shown in the image), that is recognised as a table.
2) Once I use camPoses in Bundle Adjustment, I get a codegen error that says 'This function does not support one-dimensional indexing. I am not sure what that means because my variable dimensions match the documentation. I have also attached images of my variables and their dimensions.
Answers (1)
The issue is not your runtime dimensions. Instead, tt is a codegen representation/type issue.
In MATLAB R2026a, the codegen implementation of imageviewset/poses returns a struct:
struct('ViewId', viewsId, 'AbsolutePose', absPoses)
So the report calling "cameraPoses" a struct is expected, even though normal MATLAB execution shows a 2x2 table.
cameraPoses = poses(vSetKeyFrames);
viewIds = uint32(cameraPoses.ViewId(:));
numViews = numel(viewIds);
absolutePose = cell(numViews, 1);
for k = 1:numViews
absolutePose{k} = cameraPoses.AbsolutePose(k);
end
camPoses = table(viewIds, absolutePose, ...
'VariableNames', {'ViewId', 'AbsolutePose'});
[refinedPoints, refinedAbsPoses] = bundleAdjustment( ...
xyzWorldPoints, tracks, camPoses, intrinsics, ...
'FixedViewIDs', uint32(1), BAParams.full{:});
Additionally, I recommend avoiding "1:height(cameraPoses)" for codegen here. If "cameraPoses" is the codegen struct, "height(cameraPoses)" is not the number of poses. Use "numel(cameraPoses.ViewId)" and preserve the actual ViewId values so they still match "tracks.ViewIds".
“One-dimensional indexing” means codegen is seeing an object array that only supports linear indexing like "pose(k)", but something in the generated validation path is trying table/row-style indexing. Making "AbsolutePose" a cell column avoids that path failing.
Hope this resolves the issue.
Regards,
Matt.
Categories
Find more on Computer Vision with Simulink in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!