这个例子展示了如何在用校准的立体摄像机拍摄的视频中检测人,并确定他们与摄像机的距离。
加载stereoParameters
对象,这是使用的相机校准的结果stereoCameraCalibrator
应用程序或estimateCameraParameters
函数。
加载立体测量器对象。负载(“handshakeStereoParams.mat”);可视化摄像机外物理。showExtrinsics (stereoParams);
创建用于读取和显示视频的系统对象。
videoFileLeft =“handshake_left.avi”;videoFileRight =“handshake_right.avi”;readerLeft = VideoReader (videoFileLeft);readerRight = VideoReader (videoFileRight);球员= vision.VideoPlayer (“位置”560年[20200740]);
为了计算视差和重建三维场景,必须对左右两帧进行校正。经校正的图像具有水平极线,并行对齐。通过将匹配点的搜索空间缩小到一维,简化了视差的计算。矫正后的图像也可以组合成浮雕,可以用立体红青色眼镜观看,以看到3d效果。
frameLeft = readFrame (readerLeft);frameRight = readFrame (readerRight);[frameLeftRect, frameRightRect] =…rectifyStereoImages (frameLeft frameRight stereoParams);图;imshow (stereoAnaglyph (frameLeftRect frameRightRect));标题(纠正视频帧的);
在经校正的立体图像中,任何一对对应点都位于同一像素行上。对于左边图像中的每个像素,计算到右边图像中相应像素的距离。这个距离称为视差,它与相应的世界点到摄像机的距离成比例。
frameLeftGray = rgb2gray (frameLeftRect);frameRightGray = rgb2gray (frameRightRect);(frameLeftGray, frameRightGray);图;, 64年imshow (disparityMap [0]);标题(“差距地图”);colormap飞机colorbar
从视差图中重建每个像素对应的点的三维世界坐标。
points3D = reconstructScene(disitymap, stereoParams);%转换为米并创建一个pointCloud对象points3D = points3D ./ 1000;ptCloud = pointCloud (points3D,“颜色”, frameLeftRect);%创建流点云查看器([-3, 3], [-3, 3], [0, 8],“VerticalAxis”,“y”,…“VerticalAxisDir”,“下来”);可视化点云视图(player3D ptCloud);
使用vision.PeopleDetector
系统目标检测人。
%创建人员检测对象。限制最小对象大小%的速度。peopleDetector = vision.PeopleDetector (“MinSize”83年[166]);%检测人。bboxes = peopleDetector.step (frameLeftGray);
求出每个被检测的人的质心的三维世界坐标,计算质心到摄像机的距离,单位为米。
找到被检测到的人的中心。质心= [round(bboxes(:, 1) + bboxes(:, 3) / 2),…圆形(bboxes(:, 2) + bboxes(:, 4) / 2)];找出质心的三维世界坐标。centroidsIdx = sub2ind(大小(差异图),centroids(:, 2), centroids(:, 1));X = points3D(:,:, 1);Y = points3D(:,:, 2);Z = points3D(:,:, 3);centroids3D = [X (centroidsIdx) ';Y (centroidsIdx) ';Z (centroidsIdx)];找出与相机的距离,单位为米。=√(sum(centroids3D .^ 2));%显示检测到的人员及其距离。= cell(1, numel(dists));为i = 1:numel(dists)标签{i} = sprintf(“% 0.2 f米”距离(我));结束图;imshow (insertObjectAnnotation (frameLeftRect,“矩形”、bboxes标签);标题(检测到人的);
应用上面描述的步骤来检测人,并在每一帧视频中测量他们到摄像机的距离。
而hasFrame (readerLeft) & & hasFrame (readerRight)读取帧。frameLeft = readFrame (readerLeft);frameRight = readFrame (readerRight);校正帧。[frameLeftRect, frameRightRect] =…rectifyStereoImages (frameLeft frameRight stereoParams);%转换为灰度。frameLeftGray = rgb2gray (frameLeftRect);frameRightGray = rgb2gray (frameRightRect);%计算差异。(frameLeftGray, frameRightGray);重建三维场景。points3D = reconstructScene(disitymap, stereoParams);points3D = points3D ./ 1000;ptCloud = pointCloud (points3D,“颜色”, frameLeftRect);视图(player3D ptCloud);%检测人。bboxes = peopleDetector.step (frameLeftGray);如果~ isempty (bboxes)找到被检测到的人的中心。质心= [round(bboxes(:, 1) + bboxes(:, 3) / 2),…圆形(bboxes(:, 2) + bboxes(:, 4) / 2)];找出质心的三维世界坐标。centroidsIdx = sub2ind(大小(差异图),centroids(:, 2), centroids(:, 1));X = points3D(:,:, 1);Y = points3D(:,:, 2);Z = points3D(:,:, 3);中心体3d = [X(中心体),Y(中心体),Z(中心体)];找出与相机的距离,单位为米。dist =√(centroids3D .^ 2,2));显示检测人员及其距离。= cell(1, numel(dists));为i = 1:numel(dists)标签{i} = sprintf(“% 0.2 f米”距离(我));结束dispFrame = insertObjectAnnotation (frameLeftRect,“矩形”bboxes,…标签);其他的dispFrame = frameLeftRect;结束%显示帧。步骤(球员,dispFrame);结束
%清理释放(球员);
这个例子展示了如何使用一个校准过的立体摄像机在3-D中定位行人。
王建民,“基于OpenCV库的计算机视觉研究”,国立台湾大学出版社,2003年。
用于人类检测的定向梯度直方图。CVPR 2005。