Skip to content

Instantly share code, notes, and snippets.

@moorage
Created March 2, 2016 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moorage/d1e6e98ab2975a1cb23d to your computer and use it in GitHub Desktop.
Save moorage/d1e6e98ab2975a1cb23d to your computer and use it in GitHub Desktop.
Collect side-by-side stereovision video and image captures with openCV in C++
//
// main.cpp
// OpenCVTest
//
// Created by ThriveSmart One on 2/29/16.
// Copyright © 2016 ThriveSmart One. All rights reserved.
//
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio/videoio.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <stdio.h>
using namespace cv;
using namespace std;
//hide the local functions in an anon namespace
namespace {
void help(char** av) {
cout << "The program captures frames from two cameras connected to your computer." << endl
<< "Usage:\n" << av[0] << " <left device number> <right device number>" << endl
<< "q,Q,esc -- quit" << endl
<< "space -- save frame" << endl << endl
<< "\tTo capture from a camera pass the device number. To find the device number, try ls /dev/video*" << endl
<< "\texample: " << av[0] << " 0 2" << endl;
}
int process(VideoCapture& captureL, VideoCapture& captureR) {
int n = 0;
char filename[200];
string window_name = "video | q or esc to quit";
cout << "press space to save a picture. q or esc to quit" << endl;
namedWindow(window_name, WINDOW_KEEPRATIO); //resizable window;
Mat frameL;
Mat frameR;
for (;;) {
captureL >> frameL;
captureR >> frameR;
if (frameL.empty())
break;
Mat combinedFrame(frameL.size().height, frameL.size().width+frameR.size().width, frameL.type());
Mat cfL(combinedFrame, Rect(0, 0, frameL.size().width, frameL.size().height));
Mat cfR(combinedFrame, Rect(frameL.size().width, 0, frameR.size().width, frameR.size().height));
frameL.copyTo(cfL);
frameR.copyTo(cfR);
Mat displayFrame(combinedFrame.size().height / 4, combinedFrame.size().width / 4, combinedFrame.type());
resize(combinedFrame, displayFrame, displayFrame.size());
imshow(window_name, displayFrame);
char key = (char)waitKey(30); //delay N millis, usually long enough to display and capture input
switch (key) {
case 'q':
case 'Q':
case 27: //escape key
return 0;
case ' ': //Save an image
sprintf(filename,"capture-%.3d.jpg",n++);
imwrite(filename,combinedFrame);
cout << "Saved " << filename << endl;
break;
default:
break;
}
}
return 0;
}
}
int main(int ac, char** av) {
if (ac != 3) {
help(av);
return 1;
}
std::string argLeft = av[1];
std::string argRight = av[2];
VideoCapture captureL(argLeft); //try to open string, this will attempt to open it as a video file or image sequence
if (!captureL.isOpened()) //if this fails, try to open as a video camera, through the use of an integer param
captureL.open(atoi(argLeft.c_str()));
if (!captureL.isOpened()) {
cerr << "Failed to open the left video device!\n" << endl;
help(av);
return 1;
}
VideoCapture captureR(argRight); //try to open string, this will attempt to open it as a video file or image sequence
if (!captureR.isOpened()) //if this fails, try to open as a video camera, through the use of an integer param
captureR.open(atoi(argRight.c_str()));
if (!captureR.isOpened()) {
cerr << "Failed to open the right video device!\n" << endl;
help(av);
return 1;
}
return process(captureL, captureR);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment