that is the whole code which should get face detection with distance
load('callast.mat');
double faceDetector();
vid = videoinput('macvideo', 1, 'YCbCr422_1280x960');%left
vid2 = videoinput('macvideo', 3, 'YCbCr422_1280x960');%right
triggerconfig([vid vid2],'manual');
vid2.FramesPerTrigger = 10;
vid.FramesPerTrigger = 10;
start([vid vid2]);
pause(1)
trigger([vid vid2]);
I1 = getsnapshot(vid);
I2 = getsnapshot(vid2);
% I1 = undistortImage(T1,stereoParams.CameraParameters1);
%I2 = undistortImage(T2,stereoParams.CameraParameters2);
faceDetector = vision.CascadeObjectDetector;
face1 = faceDetector(I1);
face2 = faceDetector(I2);
center1 = face1(1:2) + face1(3:4)/2;
center2 = face2(1:2) + face2(3:4)/2;
point3d = triangulate(center1, center2, stereoParams);
distanceInMeters = norm(point3d)/1000;
distanceAsString = sprintf('%0.2f meters', distanceInMeters);
I1 = insertObjectAnnotation(I1,'rectangle',face1,distanceAsString,'FontSize',18);
I2 = insertObjectAnnotation(I2,'rectangle',face2, distanceAsString,'FontSize',18);
I1 = insertShape(I1,'FilledRectangle',face1);
I2 = insertShape(I2,'FilledRectangle',face2);
imshowpair(I1, I2, 'montage');
the error
Undefined function or variable 'stereoParams'.
point3d = triangulate(center1, center2, stereoParams);

 Réponse acceptée

Walter Roberson
Walter Roberson le 22 Jan 2018

1 vote

You did not call estimateCameraParameters to generate the stereo parameters.

10 commentaires

i added a loop i forgot
load('callast.mat');
double faceDetector();
vid = videoinput('macvideo', 1, 'YCbCr422_1280x960');%left
vid2 = videoinput('macvideo', 3, 'YCbCr422_1280x960');%right
triggerconfig([vid vid2],'manual');
vid2.FramesPerTrigger = 10;
vid.FramesPerTrigger = 10;
start([vid vid2]);
pause(1)
trigger([vid vid2]);
for x = 1:100
I1 = getsnapshot(vid);
I2 = getsnapshot(vid2);
% I1 = undistortImage(T1,stereoParams.CameraParameters1);
%I2 = undistortImage(T2,stereoParams.CameraParameters2);
faceDetector = vision.CascadeObjectDetector;
face1 = faceDetector(I1);
face2 = faceDetector(I2);
center1 = face1(1:2) + face1(3:4)/2;
center2 = face2(1:2) + face2(3:4)/2;
point3d = triangulate(center1, center2, stereoParams);
distanceInMeters = norm(point3d)/1000;
distanceAsString = sprintf('%0.2f meters', distanceInMeters);
I1 = insertObjectAnnotation(I1,'rectangle',face1,distanceAsString,'FontSize',18);
I2 = insertObjectAnnotation(I2,'rectangle',face2, distanceAsString,'FontSize',18);
I1 = insertShape(I1,'FilledRectangle',face1);
I2 = insertShape(I2,'FilledRectangle',face2);
end
imshowpair(I1, I2, 'montage');
the error changed
Index exceeds matrix dimensions.
center1 = face1(1:2) + face1(3:4)/2;
ahmed nasr
ahmed nasr le 22 Jan 2018
i loaded the calibration file isn't that the camera parameters ??
We have no idea what is stored in your callast.mat file.
Even if there is a variable named stereoParams in there, if this code is inside a function, the Just In Time compiler would not believe it exists. You should not "poof" variables into exists by using load() without an output: you should assign the results of load() to a variable and either access the resulting struct directly or pull out parts of it, like
filedata = load('callast.mat');
stereoParams = filedata.stereoParams;
Note by the way that your line
double faceDetector();
means exactly the same thing as
double('faceDetector()');
which computes [102 97 99 101 68 101 116 101 99 116 111 114 40 41] and then throws it away. MATLAB does not have type declarations.
Index exceeds matrix dimensions.
center1 = face1(1:2) + face1(3:4)/2;
Your code assumes that the face detector found exactly one face. You should not assume that. You should be testing the size of the output to determine whether it found anything, and you should assume it might have found multiple faces (in which case the information for any one face is a row in the results, whereas you assume you can use linear indexing, which will fail if multiple faces were returned.
ahmed nasr
ahmed nasr le 22 Jan 2018
thank you you have been so helpful... but still i can't understand
Delete the line
double faceDetector();
Change
load('callast.mat')
to
filedata = load('callast.mat');
stereoParams = filedata.stereoParams;
Change
center1 = face1(1:2) + face1(3:4)/2;
center2 = face2(1:2) + face2(3:4)/2;
followed by the rest of your code
to
if ~isempty(face1) & ~isempty(face2)
center1 = face1(1, 1:2) + face1(1, 3:4)/2;
center2 = face2(1, 1:2) + face2(1, 3:4)/2;
followed by the rest of your code and then
end
so that the rest of your code is not executed if no face was detected.
i did what u told me but the filedata part gave me an error but everything else ran smoothly with no errors but no output .
load('callast.mat');
vid = videoinput('macvideo', 1, 'YCbCr422_1280x960');%left
vid2 = videoinput('macvideo', 2, 'YCbCr422_1280x960');%right
triggerconfig([vid vid2],'manual');
vid2.FramesPerTrigger = 10;
vid.FramesPerTrigger = 10;
start([vid vid2]);
pause(1)
trigger([vid vid2]);
I1 = getsnapshot(vid);
I2 = getsnapshot(vid2);
faceDetector = vision.CascadeObjectDetector;
face1 = faceDetector(I1);
face2 = faceDetector(I2);
if ~isempty(face1) & ~isempty(face2)
center1 = face1(1:2) + face1(3:4)/2;
center2 = face2(1:2) + face2(3:4)/2;
point3d = triangulate(center1, center2, stereoParams);
distanceInMeters = norm(point3d)/1000;
distanceAsString = sprintf('%0.2f meters', distanceInMeters);
I1 = insertObjectAnnotation(I1,'rectangle',face1,distanceAsString,'FontSize',18);
I2 = insertObjectAnnotation(I2,'rectangle',face2, distanceAsString,'FontSize',18);
I1 = insertShape(I1,'FilledRectangle',face1);
I2 = insertShape(I2,'FilledRectangle',face2);
imshowpair(I1, I2, 'montage');
end
ahmed nasr
ahmed nasr le 23 Jan 2018
but thats a nice breakthrough, thank you Walter. i don't know how i forgot this loop
Walter Roberson
Walter Roberson le 23 Jan 2018
I do not see any loop in your code.
ahmed nasr
ahmed nasr le 23 Jan 2018
The condition am sorry, ishould add a loop to take screenshots and analyze, right ?
You have two choices:
First possibility:
maxframe = 10;
vid = videoinput('macvideo', 1, 'YCbCr422_1280x960');%left
vid2 = videoinput('macvideo', 3, 'YCbCr422_1280x960');%right
faceDetector = vision.CascadeObjectDetector;
for frame = 1 : maxframe
I1 = getsnapshot(vid);
I2 = getsnapshot(vid2);
face1 = faceDetector(I1);
face2 = faceDetector(I2);
if ~isempty(face1) & ~isempty(face2)
center1 = face1(1, 1:2) + face1(1, 3:4)/2;
center2 = face2(1, 1:2) + face2(1, 3:4)/2;
point3d = triangulate(center1, center2, stereoParams);
distanceInMeters = norm(point3d)/1000;
distanceAsString = sprintf('%0.2f meters', distanceInMeters);
I1 = insertObjectAnnotation(I1,'rectangle',face1,distanceAsString,'FontSize',18);
I2 = insertObjectAnnotation(I2,'rectangle',face2, distanceAsString,'FontSize',18);
I1 = insertShape(I1,'FilledRectangle',face1);
I2 = insertShape(I2,'FilledRectangle',face2);
imshowpair(I1, I2, 'montage');
drawnow();
end
end
Second possibility:
maxframe = 10;
vid = videoinput('macvideo', 1, 'YCbCr422_1280x960');%left
vid2 = videoinput('macvideo', 3, 'YCbCr422_1280x960');%right
faceDetector = vision.CascadeObjectDetector;
vid2.FramesPerTrigger = maxframe;
vid.FramesPerTrigger = maxframe;
start([vid vid2]);
pause(1)
trigger([vid vid2]);
frames1 = getdata(vid);
frames2 = getdata(vid);
for frame = 1 : size(frames1, 4)
I1 = frames1(:,:,:,frame);
I2 = frames1(:,:,:,frame);
face1 = faceDetector(I1);
face2 = faceDetector(I2);
if ~isempty(face1) & ~isempty(face2)
center1 = face1(1, 1:2) + face1(1, 3:4)/2;
center2 = face2(1, 1:2) + face2(1, 3:4)/2;
point3d = triangulate(center1, center2, stereoParams);
distanceInMeters = norm(point3d)/1000;
distanceAsString = sprintf('%0.2f meters', distanceInMeters);
I1 = insertObjectAnnotation(I1,'rectangle',face1,distanceAsString,'FontSize',18);
I2 = insertObjectAnnotation(I2,'rectangle',face2, distanceAsString,'FontSize',18);
I1 = insertShape(I1,'FilledRectangle',face1);
I2 = insertShape(I2,'FilledRectangle',face2);
imshowpair(I1, I2, 'montage');
drawnow();
end
end

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by