real time disparity map in matlab

4 ビュー (過去 30 日間)
Newman
Newman 2015 年 7 月 24 日
回答済み: Wing Fung Hui 2018 年 1 月 10 日
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?

回答 (2 件)

Shankar Subramanian
Shankar Subramanian 2015 年 7 月 24 日
編集済み: Shankar Subramanian 2015 年 7 月 24 日
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 件のコメント
Newman
Newman 2015 年 7 月 24 日
@Shankar Subramanian it is still showing error
this my new code
vid = videoinput('winvideo', 1, 'MJPG_640x480');%right
vid2= videoinput('winvideo', 2, 'MJPG_640x480');%left
triggerconfig([vid vid2],'manual');
vid2.FramesPerTrigger = 10;
vid.FramesPerTrigger = 10;
% start logging of acquired data.
start([vid vid2]);
pause(1)
% Trigger the devices to start logging of data.
trigger([vid vid2]);
% vid.TriggerRepeat = 9;
% vid2.TriggerRepeat = 9;
%while (vid.FramesAvailable<200)&&(vid2.FramesAvailable<200)
% trigger([vid vid2]);
for i=1:5
data = getsnapshot(vid);
data2 = getsnapshot(vid2);
[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
%end
delete(vid)
clear vid
delete(vid2)
clear vid2
the dispairty map is running rel time but it runs very slowwly and the webcam led blinks one after the another.they are not running simultaneously i think . the trigger repeat is showing eror
TriggerRepeat: Property can not be set while Running is set to 'on'. how to do this in real time continuously i mean infinite time. it breaks only after i press a button or so
ahmed nasr
ahmed nasr 2017 年 11 月 26 日
did you know whats wrong with the code ?

サインインしてコメントする。


Wing Fung Hui
Wing Fung Hui 2018 年 1 月 10 日
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!

Translated by