Abh*_*tia 9 matlab opencv computer-vision camera-calibration matlab-cvst
校准:
我在Matlab中使用这个视觉工具箱校准了相机.我使用棋盘图像来做到这一点.校准后,我得到cameraParams,其中包含:
Camera Extrinsics
RotationMatrices: [3x3x18 double]
TranslationVectors: [18x3 double]
Run Code Online (Sandbox Code Playgroud)
和
Camera Intrinsics
IntrinsicMatrix: [3x3 double]
FocalLength: [1.0446e+03 1.0428e+03]
PrincipalPoint: [604.1474 359.7477]
Skew: 3.5436
Run Code Online (Sandbox Code Playgroud)
目的: 我使用这台相机记录了一些运动物体的轨迹.每个对象对应于帧中的单个点.现在,我想要投射点,以便我得到一个顶视图.
请注意我希望转换的所有这些点都在同一个平面上.
例如:[xcor_i,ycor_i]
-101.7000 -77.4040
-102.4200 -77.4040
Run Code Online (Sandbox Code Playgroud)代码 (参考:https://stackoverflow.com/a/27260492/3646408并在下面以@Dima回答):
function generate_homographic_matrix()
%% Calibrate camera
% Define images to process
path=['.' filesep 'Images' filesep];
list_imgs=dir([path '*.jpg']);
list_imgs_path=strcat(path,{list_imgs.name});
% Detect checkerboards in images
[imagePoints, boardSize, imagesUsed] = detectCheckerboardPoints(list_imgs_path);
imageFileNames = list_imgs_path(imagesUsed);
% Generate world coordinates of the corners of the squares
squareSize = 27; % in units of 'mm'
worldPoints = generateCheckerboardPoints(boardSize, squareSize);
% Calibrate the camera
[cameraParams, imagesUsed, estimationErrors] = estimateCameraParameters(imagePoints, worldPoints, ...
'EstimateSkew', true, 'EstimateTangentialDistortion', true, ...
'NumRadialDistortionCoefficients', 3, 'WorldUnits', 'mm');
%% Compute homography for peripendicular plane to checkerboard
% Detect the checkerboard
im=imread(['.' filesep 'Images' filesep 'exp_19.jpg']); %exp_19.jpg is the checkerboard orthogonal to the floor
[imagePoints, boardSize] = detectCheckerboardPoints(im);
% Compute rotation and translation of the camera.
[Rc, Tc] = extrinsics(imagePoints, worldPoints, cameraParams);
% Rc(rotation of the calibration view w.r.t the camera) = [x y z])
%then the floor has rotation Rf = [z x -y].(Normal vector of the floor goes up.)
Rf=[Rc(:,3),Rc(:,1),Rc(:,2)*-1];
% Translate it to the floor
H=452;%distance btw origin and floor
Fc = Rc * [0; H; 0];
Tc = Tc + Fc';
% Combine rotation and translation into one matrix:
Rf(3, :) = Tc;
% Compute the homography between the checkerboard and the image plane:
H = Rf * cameraParams.IntrinsicMatrix;
save('homographic_matrix.mat','H')
end
Run Code Online (Sandbox Code Playgroud)
%% Transform points
function [x_transf,y_transf] =transform_points(xcor_i,ycor_i)
% creates a projective2D object and then transforms the points forward to
% get a top-view
% xcor_i and ycor_i are 1d vectors comprising of the x-coordinates and
% y-coordinates of trajectories.
data=load('homographic_matrix.mat');
homo_matrix=data.H;
tform=projective2d(inv(homo_matrix));
[x_transf,y_transf] = transformPointsForward(tform,xcor_i,ycor_i);
end
Run Code Online (Sandbox Code Playgroud)
从OReilly Learning OpenCV Pg 412引用文本:"一旦我们按照我们的意愿设置单应矩阵和高度参数,我们就可以移除棋盘并推动推车,制作路径的鸟瞰视频...... "这就是我基本上希望实现的目标.
由于您拥有网格上正方形的大小,然后给定已知由大小为 E(以现实世界单位)的边连接的 2 个点,您可以计算它们的 3D 位置。
利用相机固有矩阵K以及 3D 位置C和相机方向矩阵R,您可以通过执行以下操作来计算到每个点的射线p:
D = R^T * K^-1 * p
Run Code Online (Sandbox Code Playgroud)
每个 3D 点定义为:
P = C + t*D
Run Code Online (Sandbox Code Playgroud)
并且您有一个约束,那 ||P1-P2|| = E
就是求解t1,t2并找到两个点的 3D 位置。
为了创建顶视图,您可以获取 3D 点并使用该顶视图的相机模型对其进行投影,以生成新图像。
如果所有点都在一个平面上,则计算 3 个点的位置就足够了,您可以推断其余的点。
如果您的点位于您知道一个坐标的平面上,则可以简单地对每个点执行此操作。例如,如果您知道相机位于 高度h=C.z,并且您想要找到帧中点的 3D 位置(假定它们位于地板上 (z=0)),那么您所要做的就是计算方向D如上,然后:
t=abs( (h-0)/D.z )
Run Code Online (Sandbox Code Playgroud)
代表0平面的高度。替换为其他飞机的任何其他值。
现在您已经有了 的值t,您可以计算每个点的 3D 位置:P=C+t*D。
然后,要创建顶视图,请创建新的相机位置和旋转以匹配所需的投影,并且您可以将每个点投影到该相机的图像平面上。如果您想要完整的图像,您可以插入位置并填充不存在特征点的空白。
有关更多详细信息,您可以随时阅读: http: //www.robots.ox.ac.uk/~vgg/hzbook/index.html