【问题标题】:How to calculate distance from camera to object using CV toolbox?如何使用 CV 工具箱计算相机到物体的距离?
【发布时间】:2020-05-09 04:03:21
【问题描述】:

我创建了一个自定义对象检测器,效果很好,但我的矩阵中的所有值都得到 NaN。我在调试代码后发现了这一点,我的图像中的有界区域显示 NaN 米。 我使用 Matlab 中的代码来计算距离,但我不知道这是否是计算距离的通用方法。下面是代码。

points3D = reconstructScene(disparityMapBM,stereoParams);
points3D = points3D ./ 1000;
trainingData = objectDetectorTrainingData(gTruth);
myAFCdetector = trainACFObjectDetector(trainingData,'NumStages',5, 'NegativeSamplesFactor',2);

img = imread('Right_Image.jpg');

[bboxes,scores] = detect(myAFCdetector,img);

for i = 1:length(scores)
   annotation = sprintf('Confidence = %.1f',scores(i));
   I1 = insertObjectAnnotation(img,'rectangle',bboxes(i,:),annotation);
end

figure
imshow(I1)

centroids = [round(bboxes(:, 1) + bboxes(:, 3) / 2), ...
    round(bboxes(:, 2) + bboxes(:, 4) / 2)];

% Find the 3-D world coordinates of the centroids.
centroidsIdx = sub2ind(size(disparityMapBM), centroids(:, 2), centroids(:, 1));
X = points3D(:, :, 1);
Y = points3D(:, :, 2);
Z = points3D(:, :, 3);
centroids3D = [X(centroidsIdx)'; Y(centroidsIdx)'; Z(centroidsIdx)'];

% Find the distances from the camera in meters.
dists = sqrt(sum(centroids3D .^ 2));

% Display the detected people and their distances.
labels = cell(1, numel(dists));
for i = 1:numel(dists)
    labels{i} = sprintf('%0.2f meters', dists(i));
end
figure;
imshow(insertObjectAnnotation(I1, 'rectangle', bboxes, labels));
title('Detected People');

我在 centroids3D 找到了 NaN 矩阵。为什么我让这些矩阵充满了 NaN 有什么原因吗?

Centoids3D = [Nan NaN NaN NaN; Nan NaN NaN NaN; Nan NaN NaN NaN]

【问题讨论】:

  • 是否检查了您的 3D 点(points3D 中的值)的样子?它们可能包含无效点。它们是根据视差图计算得出的,视差图可能很嘈杂(甚至包含一些零,这意味着无限距离)。如果您的所有 3D 点都是正确的(没有 NaN),那么您应该检查用于从每个 2D 质心中选择 3D 点的索引 (centroidsIdx)。
  • 质心包含一个 4x2 双矩阵,而 centroidsIdx 包含 4x1 双矩阵,但值非常大,分别为 298760、434672、482956、474044。我仍然无法理解这一点。我可以在视差图之后做一些过滤。也许它可以改变结果

标签: matlab 3d computer-vision matlab-cvst stereo-3d


【解决方案1】:

首先您需要获取已知距离的参考物体以获得焦距,然后找到物体与相机的距离是可行的。

Here is the full tutorial for reference.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-08
    • 1970-01-01
    • 1970-01-01
    • 2012-12-11
    • 2019-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多