Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleOpenCVCam.cpp

Example for capturing images using OpenCV.

Author
esquivel (adapted from OpenCV examples)
Date
05/2013
/*
This file is part of the BIAS library (Basic ImageAlgorithmS).
Copyright (C) 2003-2009 (see file CONTACT for details)
Multimediale Systeme der Informationsverarbeitung
Institut fuer Informatik
Christian-Albrechts-Universitaet Kiel
BIAS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
BIAS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with BIAS; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/** @example ExampleOpenCVCam.cpp
@ingroup g_examples
@ingroup g_videosource
@brief Example for capturing images using OpenCV.
@author esquivel (adapted from OpenCV examples)
@date 05/2013
*/
#include "cv.h"
#include "highgui.h"
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char *argv[])
{
// Read camera IDs from command line
int numCams = 1;
std::vector<int> camIDs(1, CV_CAP_ANY);
if (argc > 1) {
numCams = argc - 1;
camIDs.resize(numCams);
for (int i = 0; i < numCams; i++)
camIDs[i] = atoi(argv[i+1]);
}
// Create capture devices
std::vector<CvCapture*> camDevs(numCams, NULL);
for (int i = 0; i < numCams; i++) {
camDevs[i] = cvCaptureFromCAM(camIDs[i]);
if (!camDevs[i]) {
cout << "Failed to create capture device for ID " << camIDs[i] << endl;
return -1;
} else {
cout << "Created capture device for ID " << camIDs[i] << endl;
}
}
// Create window to display the captured camera images
cvNamedWindow("ExampleOpenCVCam", CV_WINDOW_AUTOSIZE);
// Show captured camera images and repeat
int currentID = 0;
bool isRunning = true;
std::vector<IplImage*> frames(numCams, NULL);
while (isRunning)
{
// Get frames
for (int i = 0; i < numCams; i++)
frames[i] = cvQueryFrame(camDevs[i]);
// Show current camera's frame
if (!frames[currentID]) {
cout << "Captured frame is empty!" << endl;
isRunning = false;
} else {
cvShowImage("ExampleOpenCVCam", frames[currentID]);
int key = cvWaitKey(10) & 255;
if (key == 27)
isRunning = false;
else if (key == 13)
currentID = (currentID + 1) % numCams;
else if (key == 8)
currentID = (currentID + numCams - 1) % numCams;
}
}
// Release capture devices
cout << "Release capture devices and quit" << endl;
for (int i = 0; i < numCams; i++)
cvReleaseCapture(&camDevs[i]);
cvDestroyWindow("ExampleOpenCVCam");
return 0;
}