具体实现过程如下:
1.从不同角度用摄像机对立方体盒子进行拍摄,得到一幅图像。如下:
2.在立方体盒子上选取至少6个点,获取这6个点的图像坐标和空间点坐标(世界坐标系自己设定)。如:
7 %点个数
0 0 0%空间点的世界坐标
0 0 48
50 0 0
0 59 0
0 59 48
50 0 48
50 59 48
1538 1619%空间点对应的图像坐标
1533 1012
1749 1244
814 1414
730 855
1771 707
1077 603
3.编程计算摄像机矩阵CM,完成标定。(Matlab实现的)
代码如下:
clc
% 打开文件
fid_l=fopen('C:\Documents and Settings\duan\桌面\我的工作\inputdata_l.txt','r');
% 点的数目
tem_l =fscanf(fid_l,'%i',1);
var_l =tem_l;
% 空间点集
WPoint_l = zeros(var_l,3);
% 图像点集
CPoint_l = zeros(var_l,2);
A_l = zeros(2*var_l,11);
for i = 1:1:var_l
[A_l,count_l]=fscanf(fid_l,'%f %f %f',[1,3]);
WPoint_l(i,1)=A_l(1);
WPoint_l(i,2)=A_l(2);
WPoint_l(i,3)=A_l(3);
end
for i = 1:1:var_l
[A_l,count_l]=fscanf(fid_l,'%f %f',[1,2]);
CPoint_l(i,1)=A_l(1);
CPoint_l(i,2)=A_l(2);
end
fclose(fid_l);
WPoint_l;
CPoint_l;
for i =1:1:var_l
m = 2*(i-1)+1;
n = 2*i;
A_l(m,1) = WPoint_l(i,1);
A_l(m,2) = WPoint_l(i,2);
A_l(m,3) = WPoint_l(i,3);
A_l(m,4) = 1;
A_l(m,5) = 0;
A_l(m,6) = 0;
A_l(m,7) = 0;
A_l(m,8) = 0;
A_l(m,9) = -WPoint_l(i,1)*CPoint_l(i,1);
A_l(m,10) = -WPoint_l(i,2)*CPoint_l(i,1);
A_l(m,11) = -WPoint_l(i,3)*CPoint_l(i,1);
A_l(n,1) = 0;
A_l(n,2) = 0;
A_l(n,3) = 0;
A_l(n,4) = 0;
A_l(n,5) = WPoint_l(i,1);
A_l(n,6) = WPoint_l(i,2);
A_l(n,7) = WPoint_l(i,3);
A_l(n,8) = 1;
A_l(n,9) = -WPoint_l(i,1)*CPoint_l(i,2);
A_l(n,10) = -WPoint_l(i,2)*CPoint_l(i,2);
A_l(n,11) = -WPoint_l(i,3)*CPoint_l(i,2);
end
B_l = zeros(2*var_l,1);
for i = 1:1:var_l
B_l(2*(i-1) + 1) = CPoint_l(i,1);
B_l(2*i) = CPoint_l(i,2);
end
% A_l
% B_l
p = A_l\B_l;
p1 = [p;1.0];
p1 = reshape(p1,4,3);
CM_l = p1';
%CM_l 即为要求的左摄像机矩阵
CM_l
%这段代码的作用是验证所求的摄像机矩阵的正确性
% c =[30;-30;0;1];
% t =CM_l*c
% t(1)=t(1)/t(3);
% t(2)=t(2)/t(3);
% t(3)=t(3)/t(3);
% t
% 打开文件
fid_r = fopen('C:\Documents and Settings\duan\桌面\我的工作\inputdata_r.txt','r');
% 点的数目
tem_r = fscanf(fid_r,'%i',1);
var_r = tem_r;
% 空间点集
WPoint_r = zeros(var_r,3);
% 图像点集
CPoint_r = zeros(var_r,2);
A_r = zeros(2*var_r,11);
for i = 1:1:var_r
[A_r,count_r] = fscanf(fid_r,'%f %f %f',[1,3]);
WPoint_r(i,1) = A_r(1);
WPoint_r(i,2) = A_r(2);
WPoint_r(i,3) = A_r(3);
end
for i = 1:1:var_r
[A_r,count_r] = fscanf(fid_r,'%f %f',[1,2]);
CPoint_r(i,1) = A_r(1);
CPoint_r(i,2) = A_r(2);
end
fclose(fid_r);
WPoint_r;
CPoint_r;
for i =1:1:var_r
m = 2*(i-1)+1;
n = 2*i;
A_r(m,1) = WPoint_r(i,1);
A_r(m,2) = WPoint_r(i,2);
A_r(m,3) = WPoint_r(i,3);
A_r(m,4) = 1;
A_r(m,5) = 0;
A_r(m,6) = 0;
A_r(m,7) = 0;
A_r(m,8) = 0;
A_r(m,9) = -WPoint_r(i,1)*CPoint_r(i,1);
A_r(m,10) = -WPoint_r(i,2)*CPoint_r(i,1);
A_r(m,11) = -WPoint_r(i,3)*CPoint_r(i,1);
A_r(n,1) = 0;
A_r(n,2) = 0;
A_r(n,3) = 0;
A_r(n,4) = 0;
A_r(n,5) = WPoint_r(i,1);
A_r(n,6) = WPoint_r(i,2);
A_r(n,7) = WPoint_r(i,3);
A_r(n,8) = 1;
A_r(n,9) = -WPoint_r(i,1)*CPoint_r(i,2);
A_r(n,10) = -WPoint_r(i,2)*CPoint_r(i,2);
A_r(n,11) = -WPoint_r(i,3)*CPoint_r(i,2);
end
B_r = zeros(2*var_r,1);
for i = 1:1:var_r
B_r(2*(i-1) + 1) = CPoint_r(i,1);
B_r(2*i) = CPoint_r(i,2);
end
% A_r
% B_r
p = A_r\B_r;
p1 = [p;1.0];
p1 = reshape(p1,4,3);
CM_r = p1';
%CM_r 即为要求的右摄像机矩阵
CM_r
%这段代码的作用是验证所求的摄像机矩阵的正确性
c =[30;0;0;1];
t =CM_r*c;
t(1)=t(1)/t(3);
t(2)=t(2)/t(3);
t(3)=t(3)/t(3);
t
转载于:https://blog.51cto.com/19870404/388148