Chris
Chris

Reputation: 23

How to display 2 webcam videos side by side in the same window for stereo?

I want to create a stereo video feed for first person view (FPV) using a 3D head mounted display (HMD). How can I display 2 webcam videos in the same video and then adjust their position using the arrow keys?

Upvotes: 1

Views: 1734

Answers (1)

cyriel
cyriel

Reputation: 3522

Displaying 2 videos side by side is very simple - just get frames from both cameras and copy result into one image(Mat):

VideoCapture videoLeft(0), videoRight(1);
Mat left, right, both;
int width = 640, height = 480;
both = Mat(height + 100, 2 * width + 150, CV_MAKETYPE(8, 3), CV_RGB(100, 100, 100));
while(true)
{
    videoLeft >> left;
    videoRight >> right;
    if (left.data == NULL || right.data == NULL)
        break;
    left.copyTo(Mat(both, Rect(50, 50, width, height)));
    right.copyTo(Mat(both, Rect(100 + width, 50, width, height)));
    imshow("images", both);
    waitKey(30);
}

Replace in this code values of width and height with size of images grabbed from you cameras - you can easily check it by checking values of left.rows(height) and left.cols(width).

You can adjust the positions changing this 2 lines:

left.copyTo(Mat(both, Rect(50, 50, width, height)));
right.copyTo(Mat(both, Rect(100 + width, 50, width, height)));

Just play with values of first and second parameter of Rect constructor. First parameter is x position and second is y. You can change them after pressing some key. More information - [updated to a web archive because of broken link:] http://web.archive.org/web/20130619174223/http://bsd-noobz.com/opencv-guide/45-1-using-keyboard

Upvotes: 2

Related Questions