相机校准模式的转换

时间:2016-06-08 13:15:58

标签: matlab camera-calibration

我在matlab中使用相机校准来检测

之后的一些棋盘图案
figure; showExtrinsics(cameraParams, 'CameraCentric');

enter image description here

现在,我想围绕x轴旋转棋盘图案,使得它们在相机框架中的y坐标几乎相同。

方法: 我得到相机框架中所有图案的位置。然后我做优化,其中目标函数是最小化y的方差,变量是关于x的旋转范围从0到360.

问题: 但是当我绘制变换的y坐标时,它们甚至几乎在一条线上。

代码:

获取checkerboad积分:

%% Get rotation and translation matrices for each image; 
T_cw=cell(num_imgs,1); % stores camera to world rotation and translation for each image
pixel_coordinates=zeros(num_imgs,2); % stores the pixel coordinates of each checkerboard origin
for ii=1:num_imgs,
    % Calibrate the camera
    im=imread(list_imgs_path{ii}); 
    [imagePoints, boardSize] = detectCheckerboardPoints(im);
    [r_wc, t_wc] = extrinsics(imagePoints, worldPoints, cameraParams);
    T_wc=[r_wc,t_wc';0 0 0 1];
    % World to camera matrix
    T_cw{ii} = inv(T_wc); 
    t_cw{ii}=T_cw{ii}(1:3,4); % x,y,z coordinates in camera's frame
end

数据(num_imgs = 10):

t_cw
[-1072.01388542262;1312.20387622761;-1853.34408157349]  
[-1052.07856598756;1269.03455126794;-1826.73576892251]  
[-1091.85978641218;1351.08261414473;-1668.88197803184]
[-1337.56358084648;1373.78548638383;-1396.87603554914]  
[-1555.19509876309;1261.60428874489;-1174.63047408086]
[-1592.39596647158;1066.82210015055;-1165.34417772659]  
[-1523.84307918660;963.781819272748;-1207.27444716506]  
[-1614.00792252030;893.962075837621;-1114.73528985018]
[-1781.83112607964;708.973204727939;-797.185326205240]
[-1781.83112607964;708.973204727939;-797.185326205240]

主要代码(优化和转换):

%% Get theta for rotation
f_obj = @(x)var_ycors(x,t_cw);
opt_theta = fminbnd(f_obj,0,360);
%% Plotting (rotate ycor and check to fix theta)
y_rotated=zeros(1,num_imgs);
for ii=1:num_imgs,
    y_rotated(ii)=rotate_cor(opt_theta,t_cw{ii});
end
plot(1:numel(y_rotated),y_rotated);


function var_computed=var_ycors(theta,t_cw)
ycor=zeros(1,numel(t_cw));
for ii =1:numel(t_cw),
    ycor(ii)=rotate_cor(theta,t_cw{ii});
end
var_computed=var(ycor);
end

function ycor=rotate_cor(theta,mat)
r_x=[1 0 0; 0 cosd(theta) -sind(theta); 0 sind(theta) cosd(theta)];
rotate_mat=mat'*r_x;
ycor=rotate_mat(2);
end

1 个答案:

答案 0 :(得分:1)

这是一个明确的特征向量问题!

拿你的质心:

t_cw=[-1072.01388542262;1312.20387622761;-1853.34408157349  
-1052.07856598756;1269.03455126794;-1826.73576892251 
-1091.85978641218;1351.08261414473;-1668.88197803184
-1337.56358084648;1373.78548638383;-1396.87603554914  
-1555.19509876309;1261.60428874489;-1174.63047408086
-1592.39596647158;1066.82210015055;-1165.34417772659  
-1523.84307918660;963.781819272748;-1207.27444716506  
-1614.00792252030;893.962075837621;-1114.73528985018
-1781.83112607964;708.973204727939;-797.185326205240
-1781.83112607964;708.973204727939;-797.185326205240];
t_cw=reshape(t_cw,[3,10])';

计算PCA,所以我们知道主要的成分:

[R]=pca(t_cw);

而且......就是这样! R现在是原始点和旋转坐标系之间的变换矩阵。举个例子,我会用红色绘制旧点,用蓝色绘制新点:

hold on

plot3(t_cw(:,1),t_cw(:,2),t_cw(:,3),'ro')
trans=t_cw*R;
plot3(trans(:,1),trans(:,2),trans(:,3),'bo')

enter image description here

你可以看到现在蓝色的那些在一个平面上,最适合X方向。如果你想要它们在Y方向,只需在Z中旋转90度(我相信你可以用2分钟的Google来弄清楚如何做到这一点;))。

注意:这在数学上是最合适的。我知道他们不是连续的#34;正如人们所愿,但这是因为数据,这实际上是最好的拟合,因为这就是特征向量!