Distance Calculation using Stereo camera

I am trying to calculate the distance of a person from a video frame. But this error keeps on coming. I have been trying a lot but can't correct it. I know this code is given on the MathWorks site. But if someone can remove that error it would be of much help.
The error:
Error using insertObjectAnnotation
Expected LABEL to be nonempty.
Error in insertObjectAnnotation
Error in insertObjectAnnotation
Error in insertObjectAnnotation
Error in depthmappingsample1 (line 82)
imshow(insertObjectAnnotation(frameLeftRect, 'rectangle', bboxes, labels));
My code:
%Load the Parameters of the Stereo Camera
% Load the stereoParameters object.
%load('handshakestereoParams.mat');
% Visualize camera extrinsics.
showExtrinsics(stereoParams1);
%Create Video File Readers and the Video Player
videoFileLeft = 'framesL.avi';
videoFileRight = 'framesR.avi';
readerLeft = vision.VideoFileReader(videoFileLeft, 'VideoOutputDataType', 'uint8');
readerRight = vision.VideoFileReader(videoFileRight, 'VideoOutputDataType', 'uint8');
player = vision.DeployableVideoPlayer('Location', [20, 400]);
%Read and Rectify Video Frames
frameLeft = readerLeft.step();
frameRight = readerRight.step();
[frameLeftRect, frameRightRect] = ...
rectifyStereoImages(frameLeft, frameRight, stereoParams1);
figure;
imshow(stereoAnaglyph(frameLeftRect, frameRightRect));
title('Rectified Video Frames');
%Compute Disparity
frameLeftGray = rgb2gray(frameLeftRect);
frameRightGray = rgb2gray(frameRightRect);
disparityMap = disparity(frameLeftGray, frameRightGray);
figure;
imshow(disparityMap, [0, 64]);
title('Disparity Map');
colormap jet
colorbar
%Reconstruct the 3-D Scene
points3D = reconstructScene(disparityMap, stereoParams1);
% Convert to meters and create a pointCloud object
points3D = points3D ./ 1000;
ptCloud = pointCloud(points3D, 'Color', frameLeftRect);
% Create a streaming point cloud viewer
player3D = pcplayer([-3, 3], [-3, 3], [0, 8], 'VerticalAxis', 'y', ...
'VerticalAxisDir', 'down');
% Visualize the point cloud
view(player3D, ptCloud);
%Detect People in the Left Image
% Create the people detector object. Limit the minimum object size for
% speed.
peopleDetector = vision.PeopleDetector('MinSize', [166 83]);
% Detect people.
bboxes = peopleDetector.step(frameLeftGray);
% Find the centroids of detected people.
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(disparityMap), 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.
a= numel(dists);
labels = cell(1, a);
for i = 1:a
labels(i) = sprintf('%0.2f meters', dists(i));
end
figure;
imshow(insertObjectAnnotation(frameLeftRect, 'rectangle', bboxes, labels));
title('Detected People');
%Process the Rest of the Video
while ~isDone(readerLeft) && ~isDone(readerRight)
% Read the frames.
frameLeft = readerLeft.step();
frameRight = readerRight.step();
% Rectify the frames.
[frameLeftRect, frameRightRect] = ...
rectifyStereoImages(frameLeft, frameRight, stereoParams1);
% Convert to grayscale.
frameLeftGray = rgb2gray(frameLeftRect);
frameRightGray = rgb2gray(frameRightRect);
% Compute disparity.
disparityMap = disparity(frameLeftGray, frameRightGray);
% Reconstruct 3-D scene.
points3D = reconstructScene(disparityMap, stereoParams1);
points3D = points3D ./ 1000;
ptCloud = pointCloud(points3D, 'Color', frameLeftRect);
view(player3D, ptCloud);
% Detect people.
bboxes = peopleDetector.step(frameLeftGray);
if ~isempty(bboxes)
% Find the centroids of detected people.
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(disparityMap), 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, 2));
% Display the detect people and their distances.
b = numel(dists);
labels = cell(1, b);
for i = 1:b
labels{i} = sprintf('%0.2f meters', dists(i));
end
dispFrame = insertObjectAnnotation(frameLeftRect, 'rectangle', bboxes,...
labels);
else
dispFrame = frameLeftRect;
end
% Display the frame.
step(player, dispFrame);
end
% Clean up.
reset(readerLeft);
reset(readerRight);
release(player);

4 Comments

Have you verified whether the labels are empty?
No sir I am not able to verify that. Can you please tell me how to do it?
Well, you are passing a variable called "labels" to insertObjectAnnotation. The error message is complaining that it is empty, so it seems natural to check if this is true and why.
Sir I am trying really hard to know how the variable "labels" is always empty no matter whatever I try. Can you please help me on this.

Sign in to comment.

Answers (0)

Asked:

on 19 Jul 2019

Commented:

on 19 Jul 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!