文档

立体声视频深度估计

这个例子展示了如何检测用校准过的立体摄像机拍摄的视频中的人,并确定他们与摄像机的距离。

加载立体相机的参数

加载stereoParameters对象,它是使用stereoCameraCalibratorApp或estimateCameraParameters函数。

加载stereparameters对象。负载(“handshakeStereoParams.mat”);可视化相机外部。showExtrinsics (stereoParams);

创建视频文件阅读器和视频播放器

创建用于读取和显示视频的系统对象。

videoFileLeft =“handshake_left.avi”;videoFileRight =“handshake_right.avi”;readerLeft =视觉。VideoFileReader (videoFileLeft“VideoOutputDataType”“uint8”);readerRight =视觉。VideoFileReader (videoFileRight“VideoOutputDataType”“uint8”);玩家=视野。DeployableVideoPlayer (“位置”, [20,400]);

读取和纠正视频帧

为了计算视差和重建三维场景,必须对左右摄像机的帧进行校正。校正后的图像具有水平极线,并且行对齐。通过将匹配点的搜索空间缩小到一维,简化了视差的计算。校正后的图像也可以组合成一个浮雕,可以使用立体红青色眼镜来观看3d效果。

frameLeft = readerLeft.step();frameRight = readerRight.step();[frameeftrect, frameightrect] =rectifyStereoImages(frameLeft, frameight, stereparams);图;imshow (stereoAnaglyph (frameLeftRect frameRightRect));标题(“校正视频帧”);

计算差异

在整流立体图像中,任意对对应点位于同一像素行上。对于左边图像中的每个像素,计算到右边图像中相应像素的距离。这个距离称为视差,它与相应的世界点到相机的距离成正比。

frameLeftRect = rgb2gray(frameLeftRect);frameRightGray = rgb2gray(frameRightRect);disparityMap = disparity(frameeftgray, frameightgray);图;imshow(disparityMap, [0,64]);标题(“差距地图”);colormap飞机colorbar

重建三维场景

重建视差图中每个像素对应的点的三维世界坐标。

point3d = reconstructScene(disparityMap, stereoParams);转换为米并创建一个pointCloud对象points3D = points3D ./ 1000;ptCloud = pointCloud(points3D,“颜色”, frameLeftRect);创建一个流点云查看器player3D = pcplayer([- 3,3], [- 3,3], [0,8],“VerticalAxis”“y”“VerticalAxisDir”“下来”);可视化点云视图(player3D ptCloud);

检测左图像中的人

使用愿景。PeopleDetector系统对象来检测人。

创建人员检测器对象。的最小对象大小%的速度。peopleDetector =视觉。PeopleDetector (“MinSize”, [166 83]);%检测人。bboxes = peopleDetector.step(frameeftgray);

确定每个人到摄像机的距离

求每个被检测人的质心的三维世界坐标,计算质心到摄像机的距离,单位为米。

找出被检测的人的质心。质心= [round(bboxes(:, 1) + bboxes(:, 3) / 2),Round (bboxes(:, 2) + bboxes(:, 4) / 2)];找出质心的三维世界坐标。centroidsIdx = sub2ind(size(disparityMap), centroids(:, 2), centroids(:, 1));X = points3D(:,:, 1);Y = points3D(:,:, 2);Z = points3D(:,:, 3);centroids3D = [X(centroidsIdx)];Y (centroidsIdx) ';Z (centroidsIdx)];找出到相机的距离,单位是米。dsts = sqrt(sum(centroids3D .^ 2));显示检测到的人员及其距离。Labels = cell(1, numel(dists));I = 1:numel(dists)标签{I} = sprintf(“% 0.2 f米”距离(我));结束图;imshow (insertObjectAnnotation (frameLeftRect,“矩形”(盒子,标签));标题(检测到人的);

处理视频的其余部分

应用上面描述的步骤来检测人员,并在视频的每一帧中测量他们到摄像机的距离。

~isDone(readerLeft) && ~isDone(readerRight)%读取帧。frameLeft = readerLeft.step();frameRight = readerRight.step();%修复帧。[frameeftrect, frameightrect] =rectifyStereoImages(frameLeft, frameight, stereparams);%转换为灰度。frameLeftRect = rgb2gray(frameLeftRect);frameRightGray = rgb2gray(frameRightRect);%计算视差。disparityMap = disparity(frameeftgray, frameightgray);%重建三维场景。point3d = reconstructScene(disparityMap, stereoParams);points3D = points3D ./ 1000;ptCloud = pointCloud(points3D,“颜色”, frameLeftRect);视图(player3D ptCloud);%检测人。bboxes = peopleDetector.step(frameeftgray);如果~ isempty (bboxes)找出被检测的人的质心。质心= [round(bboxes(:, 1) + bboxes(:, 3) / 2),Round (bboxes(:, 2) + bboxes(:, 4) / 2)];找出质心的三维世界坐标。centroidsIdx = sub2ind(size(disparityMap), centroids(:, 2), centroids(:, 1));X = points3D(:,:, 1);Y = points3D(:,:, 2);Z = points3D(:,:, 3);centroids3D = [X(centroidsIdx), Y(centroidsIdx), Z(centroidsIdx)];找出到相机的距离,单位是米。dists = sqrt(sum(centroids3D .^ 2, 2));显示检测到的人员和他们的距离。Labels = cell(1, numel(dists));I = 1:numel(dists)标签{I} = sprintf(“% 0.2 f米”距离(我));结束disframe = insertObjectAnnotation(frame - eftrect,“矩形”bboxes,标签);其他的disframe = frameeftrect;结束%显示框架。步骤(球员,dispFrame);结束

清理干净。重置(readerLeft);重置(readerRight);释放(球员);

总结

这个例子展示了如何使用校准的立体摄像机在3d中定位行人。

参考文献

G. Bradski和A. Kaehler,“学习OpenCV:使用OpenCV库的计算机视觉”,O'Reilly, Sebastopol, CA, 2008。

[10] Dalal, N.和Triggs, B.,用于人体检测的定向梯度直方图。CVPR 2005。