Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Superimpose two web cam streams - Matlab

I currently have a code in Matlab that takes images from two webcams, overlays them and displays them in a figure which get's updated in time intervals to give semi-realtime. However, I need to make this realtime, does anyone have any idea of how to overlay two webcam streams like you would do with a 3D movie?

Thanks!

like image 762
user1300561 Avatar asked Nov 14 '22 08:11

user1300561


1 Answers

If you mean Anaglyph 3D, having both images you can do the following:

left = imread('vipstereo_hallwayLeft.png');
right = imread('vipstereo_hallwayRight.png');

imshow(cat(3, left(:,:,1), right(:,:,2:3)));

both png's already come with the image processing toolbox.

The result will be this (and you can look at it with Red/Cyan glasses. I did!): image

I already tried this method with real pictures in 2 ways:
1. 2 pictures taken at the same time with 2 diferent cameras a little displaced;
2. 2 pictures taken in a very short time with a moving camera. (burst mode)
And they both gave excelent results.


Then, to do it with 2 webcams, you need to:
1. init them properly;
2. set them to get 1 frame per trigger;
3. trigger them and get both frames;
4. mix frames and show them.

I do not have 2 webcams so I was no able to test it, but I think this code can do it:

Cameras setup:

% Get a handle to each cam
Lvid = videoinput('winvideo', 1, 'YUY2_1280x1024');
Rvid = videoinput('winvideo', 2, 'YUY2_1280x1024');

% Set them to get one frame/trigger
Lvid.FramesPerTrigger = 1;
Rvid.FramesPerTrigger = 1;

Then do an infinite loop to get frames, mix them and show the result.

while(1)
    % Trigers both video sources
    start(Lvid);
    start(Rvid);

    % Get the frames
    left = getdata(Lvid);
    right = getdata(Rvid);

    % Convert them to RGB
    left = ycbcr2rgb(left);
    right = ycbcr2rgb(right);
    % mix them (R from right + GB from left)
    frame = cat(3, left(:,:,1), right(:,:,2:3));
    % show
    imshow(frame);
    pause(0.0001) % to refresh imshow 
end

Note that since my webcam is YUV i have to convert it to RGB prior to mixing the images.

Hope this helps you!

like image 124
Mikhail Avatar answered Jan 03 '23 18:01

Mikhail