real time disparity map in matlab

6 views (last 30 days)
Newman
Newman on 24 Jul 2015
Answered: Wing Fung Hui on 10 Jan 2018
i am using two webcam to create a dparity map in real itme in matlab this is my code
vid = videoinput('winvideo', 1, 'MJPG_640x480');%right
vid2= videoinput('winvideo', 2, 'MJPG_640x480');%left
triggerconfig([vid vid2],'manual');
vid2.FramesPerTrigger = 20;
vid.FramesPerTrigger = 20;
% start logging of acquired data.
start([vid vid2]);
pause(1)
% Trigger the devices to start logging of data.
trigger([vid vid2]);
while (vid.FramesAvailable<200)&&(vid2.FramesAvailable<200)
wait(vid,10)
wait(vid2,10)
data = getdata(vid, 1);
data2 = getdata(vid2, 1);
[J1, J2] = rectifyStereoImages(data2,data,stereoParams);
% Display the images before rectification.
% figure;
% imshow(stereoAnaglyph(data2, data), 'InitialMagnification', 50)
% drawnow
% title('Before Rectification')
%
% % Display the images after rectification.
% figure;
% imshow(stereoAnaglyph(J1, J2), 'InitialMagnification', 50)
% drawnow
% title('After Rectification')
%
%
[D,W,x,w,K,SEEDs] = gcs(J1,J2,[]);
imagesc(D)
axis image; colormap(jet); set(gca,'clim',[-100,100]);
drawnow
title ('Disparity map');
end
delete(vid)
clear vid
delete(vid2)
clear vid2
But my webcam is starting and stopping .the code stops after running abt 4-5 seconds.then the disparity map is displayed in a form of a video inside a figure box for some seconds.I want the loop to run always and generate the diaprity map simultaneously as the webcam is running .how to do that ?what is the error in my code?

Answers (2)

Shankar Subramanian
Shankar Subramanian on 24 Jul 2015
Edited: Shankar Subramanian on 24 Jul 2015
Hi Antariksha,
You have one call to trigger outside the loop and is giving you 20 frames. Your loop processes one frame at a time and probably takes around 4-5 (as you mentioned) to process the 20 frames. You have not triggered it to acquire more frames after that given that your trigger is outside the loop (and you have set it to manual trigger) and TriggerRepeat is 0.
=====
To fix your problem as is - you need to do the following:
1. You need to set the TriggerRepeat property (for 200 frames, it should be 9 repeats) before the loop starts. See this.
2. You need to trigger inside the loop to acquire the frames again.
=====
However, it is strange that you are acquiring 20 frames at a time and processing them individually. If you interested in only one frame, you can use getsnapshot (in a loop). The following page specifies how to do getsnapshot in a loop efficiently. Look under "Timing Implications" and use manual triggering as suggested on the page.
Thanks
Shankar
  2 Comments
ahmed nasr
ahmed nasr on 26 Nov 2017
did you know whats wrong with the code ?

Sign in to comment.


Wing Fung Hui
Wing Fung Hui on 10 Jan 2018
I have been working on similiar project and facing the same problem. Then I found https://www.mathworks.com/videos/solving-a-sudoku-puzzle-using-a-webcam-68773.html and it gave me a great insight on this problem. And these are my code.
% Camera setup
vid1 = videoinput('winvideo', 1, 'MJPG_640x480');%right
vid2 = videoinput('winvideo', 2, 'MJPG_640x480');%left
set([vid1 vid2],'FramesPerTrigger',Inf);
set([vid1 vid2], 'ReturnedColorspace', 'grayscale');
vid1.FrameGrabInterval = 1; vid2.FrameGrabInterval = 1;
hFigure=figure(1); load('paramStruct5.mat');
try
start([vid1 vid2]);
while islogging([vid1 vid2]);
I1 = getsnapshot(vid1);
I2 = getsnapshot(vid2);
I1 = im2double(I1); I2 = im2double(I2);
[frameLeftRect, frameRightRect] = ...
rectifyStereoImages(I1, I2, stereoParams);
w1=fspecial('log',[5 5],0.5); av = fspecial('average',[3 3]);
M1 = imfilter(I1,av,'replicate'); M2 = imfilter(I2,av,'replicate');
M1 = medfilt2(M1, 'indexed'); M2 = medfilt2(M2,'indexed');
frameLeftGray = imfilter(M1,w1,'replicate');
frameRightGray = imfilter(M2,w1,'replicate');
disparityMap = disparity(frameLeftGray, frameRightGray, 'BlockSize', 17);
imshow(disparityMap, [0, 64]);
end
catch err
stop([vid1 vid2]);
imaqreset
disp('Cleaned up')
rethrow(err);
end

Community Treasure Hunt

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

Start Hunting!