MATLAB Answers

0

what is wrong with this code??

Asked by Kamran Asif on 25 Jun 2018
Latest activity Commented on by Florian Morsch on 27 Jun 2018
this is my code....I'm trying to detect the person and than measure the distance from my stereoCamera...
leftcam= imaq.VideoDevice('winvideo', 2 ,'YUY2_640x480');
rightcam= imaq.VideoDevice('winvideo', 3, 'YUY2_640x480' );
if ~exist('stereoParams','var')
load ('calibrationSession.mat');
end
ax = axes;
maxDepth = 5;
while true
I1 = step(leftcam);
I2 = step(rightcam);
[J1,J2] = rectifyStereoImages(I1, I2, stereoParams);
%figure;
%imshow(stereoAnaglyph(J1, J2));
%title('Rectified Video Frames');
frameLeftGray = rgb2gray(J1);
frameRightGray = rgb2gray(J2);
%disparity map
disp = disparity(frameLeftGray, frameRightGray, 'DisparityRange', [0 ,64] );
%reconstruct 3d scene
pointCloud = reconstructScene(disp, stereoParams) ./1000;
z = pointCloud(:, :, 3);
z(z<0) = NaN;
z(z>maxDepth) = NaN;
pointCloud(:,:,3) = z;
if ~ishandle(ax)
break;
else
pcshow(pointCloud, J1, 'VerticalAxis', 'Y', 'VerticalAxisDir','Down','parent',ax);
%setting for visualization below..
xlabel('X (m)');
ylabel('Y (m)');
zlabel('Z (m)');
xlim(ax, [-.8, .8]);
ylim(ax, [-.8, .8]);
zlim(ax, [0, maxDepth]);
daspect(ax, 'manual');
pbaspect(ax, 'manual')
drawnow;
% view(pcshow,pointCloud);
end
%People Detetion
peopledetector= vision.PeopleDetector('MinSize',[166,83]);
bboxes= peopledetector.step(frameLeftGray);
centroids = [round(bboxes(:,1) + bboxes(:, 3) /2), round(bboxes(:, 2) + bboxes(:, 4)/2)];
centroidsIdx = sub2ind(size(disp), centroids(:, 2), centroids(:, 1));
X = pointCloud(:, :, 1);
Y = pointCloud(:, :, 2);
Z = pointCloud(:, :, 3);
centroids3D = [X(centroidsIdx), Y(centroidsIdx), Z(centroidsIdx)];
dists = sqrt(sum(centroids3D .^2, 2));
labels = cell(1, numel(dists));
for i= 1:numel(dists)
labels{i} = sprintf('%0.2f meters',dists(i));
end
% dispFrame = insertObjectAnnotation(J1, 'rectangle', bboxes, labels);
%dispFrame = J1;
figure
imshow(insertObjectAnnotation(J1, 'rectangle', bboxes, labels));
end
release(leftcam);
release(rightcam);
this is the error i got....
Error using insertObjectAnnotation
Expected LABEL to be nonempty.
Error in insertObjectAnnotation
Error in insertObjectAnnotation
Error in insertObjectAnnotation
Error in Untitled4 (line 81)
imshow(insertObjectAnnotation(J1, 'rectangle', bboxes, labels));
.... i'm new to matlab and i have very short time . this is my fyp work.. please help me to solve this problem as soon as possible

  0 Comments

Sign in to comment.

1 Answer

Answer by Florian Morsch on 25 Jun 2018

imshow(insertObjectAnnotation(J1, 'rectangle', bboxes, labels));
You use "labels"-variable and the compiler tells you, that this variable is empty. Its expected to be nonempty, meaning you should check your code (break points help) why there is no value in the variable.
My guess is: You dont detect anything with the PeopleDetector, and so your variable is empty.

  4 Comments

Show 1 older comment
Since i dont have the input from your camera i cant tell you if you detect someone with the detector or not.
Your code order is:
Detect something (i assume people) with the detector
Get the bounding box
bboxes= peopledetector.step(frameLeftGray);
Get the centroids
centroids = [round(bboxes(:,1) + bboxes(:, 3) /2), round(bboxes(:, 2) + bboxes(:, 4)/2)];
centroidsIdx = sub2ind(size(disp), centroids(:, 2), centroids(:, 1));
X = pointCloud(:, :, 1);
Y = pointCloud(:, :, 2);
Z = pointCloud(:, :, 3);
centroids3D = [X(centroidsIdx), Y(centroidsIdx), Z(centroidsIdx)];
Calculate dists
dists = sqrt(sum(centroids3D .^2, 2));
Get labels
labels = cell(1, numel(dists));
And then you use the labels in
imshow(insertObjectAnnotation(J1, 'rectangle', bboxes, labels));
The compiler tells you, that labels is empty. This means in return labels is empty, which could indicate you get no dists calculation. This might be due to missing centroids, which cant be created because you have no bounding box. I simply cant tell you where your values are missing since i dont have the camera input.
Use breakpoints and step through your code, check for the values you get and find whats missing.
The closest guess i had was: you dont detect anything. The code itself looks fine, but something is missing and i would assume its the detector not detecting anything. Cant tell for sure since i also miss the camera input.
Thanks sir, i'll check it again
Did you find your error?
If so and if my answer helped would you mind marking it as correct answer?

Sign in to comment.