需要帮助在Matlab中矢量化循环吗 我的大脑与C++思维模式是紧密相连的。需要帮助矢量化以下循环 此代码试图生成一个C++标题,该数组包含一个将失真图像的每个像素位置映射到未失真坐标的数组。
FYI cameraParams和imginTrinsic先前已由estimateFisheyeParameters函数和undistortFisheyeImage生成需要帮助在Matlab中矢量化循环吗 我的大脑与C++思维模式是紧密相连的。需要帮助矢量化以下循环 此代码试图生成一个C++标题,该数组包含一个将失真图像的每个像素位置映射到未失真坐标的数组。,matlab,vectorization,Matlab,Vectorization,FYI cameraParams和imginTrinsic先前已由estimateFisheyeParameters函数和undistortFisheyeImage生成 fileID = fopen('undistorted.h', 'w'); fprintf(fileID, '#ifndef UNDISTORTED_H\n#define UNDISTORTED_H\n\n'); fprintf(fileID, 'const float distortionFix[%d][%d][2] = {
fileID = fopen('undistorted.h', 'w');
fprintf(fileID, '#ifndef UNDISTORTED_H\n#define UNDISTORTED_H\n\n');
fprintf(fileID, 'const float distortionFix[%d][%d][2] = {', mrows, ncols);
for y = 1:mrows
fprintf(fileID, '{');
for x = 1:ncols
undistortedPoint = undistortFisheyePoints([x y], cameraParams.Intrinsics);
undistortedPoint = undistortedPoint - imgIntrinsics.PrincipalPoint;
fprintf(fileID, '{%f, %f}', undistortedPoint);
if x < ncols
fprintf(fileID, ', ');
end
end
if (y < mrows)
fprintf(fileID, '},\n');
end
end
fprintf(fileID, '}};\n\n#endif');
最好的起点是认识到它可以接受坐标点矩阵,因此使用矩阵输入调用它一次可能比在循环中重复调用它更有效。您只需创建点矩阵,该矩阵可以使用和来完成,获得未变形点的矩阵,然后使用或从每行中减去imgIntrinsics.PrincipalPoint。这一切都可以在循环之外完成,然后您只需要一个循环即可将其全部打印出来:
fileID = fopen('undistorted.h', 'w');
fprintf(fileID, '#ifndef UNDISTORTED_H\n#define UNDISTORTED_H\n\n');
fprintf(fileID, 'const float distortionFix[%d][%d][2] = {', mrows, ncols);
points = [repmat((1:ncols).', mrows, 1) repelem((1:mrows).', ncols, 1)];
undistortedPoints = undistortFisheyePoints(points, cameraParams.Intrinsics);
undistortedPoints = bsxfun(@minus, undistortedPoints, imgIntrinsics.PrincipalPoint);
for y = 1:mrows
fprintf(fileID, '{');
index = ((y-1)*ncols+1):(y*ncols-1);
fprintf(fileID, '{%f, %f},', undistortedPoints(index, :).');
fprintf(fileID, '{%f, %f}', undistortedPoints(y*ncols, :));
if (y < mrows)
fprintf(fileID, '},\n');
end
end
fprintf(fileID, '}};\n\n#endif');
为什么这里需要矢量化?这段代码的执行时间比编译生成的头文件要长得多吗?@CrisLuengo这是一个很好的练习,可以进入matlab思维模式,而且我讨厌等待。这项任务本身确实对时间不敏感