校准:
我在Matlab中使用这个视觉工具箱校准了相机.我使用棋盘图像来做到这一点.校准后,我得到cameraParams,其中包含:
Camera Extrinsics RotationMatrices: [3x3x18 double] TranslationVectors: [18x3 double]
和
Camera Intrinsics IntrinsicMatrix: [3x3 double] FocalLength: [1.0446e+03 1.0428e+03] PrincipalPoint: [604.1474 359.7477] Skew: 3.5436
目的: 我使用这台相机记录了一些运动物体的轨迹.每个对象对应于帧中的单个点.现在,我想要投射点,以便我得到一个顶视图.
请注意我希望转换的所有这些点都在同一个平面上.
例如:[xcor_i,ycor_i]
-101.7000 -77.4040 -102.4200 -77.4040
关键点:此平面垂直于用于校准的棋盘图像之一.对于那张图片(下图),我知道棋盘的起点高度(193.040厘米).投影点的平面与地面平行并垂直于该图像.
代码 (参考: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
%% 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
从OReilly Learning OpenCV Pg 412引用文本:"一旦我们按照我们的意愿设置单应矩阵和高度参数,我们就可以移除棋盘并推动推车,制作路径的鸟瞰视频...... "这就是我基本上希望实现的目标.