Example for capturing images using OpenCV.
#include "cv.h"
#include "highgui.h"
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char *argv[])
{
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]);
}
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;
}
}
cvNamedWindow("ExampleOpenCVCam", CV_WINDOW_AUTOSIZE);
int currentID = 0;
bool isRunning = true;
std::vector<IplImage*> frames(numCams, NULL);
while (isRunning)
{
for (int i = 0; i < numCams; i++)
frames[i] = cvQueryFrame(camDevs[i]);
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;
}
}
cout << "Release capture devices and quit" << endl;
for (int i = 0; i < numCams; i++)
cvReleaseCapture(&camDevs[i]);
cvDestroyWindow("ExampleOpenCVCam");
return 0;
}