Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleOpenCVCam.cpp
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10  BIAS is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 2.1 of the License, or
13  (at your option) any later version.
14 
15  BIAS is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with BIAS; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 /** @example ExampleOpenCVCam.cpp
26  @ingroup g_examples
27  @ingroup g_videosource
28  @brief Example for capturing images using OpenCV.
29  @author esquivel (adapted from OpenCV examples)
30  @date 05/2013
31 */
32 
33 #include "cv.h"
34 #include "highgui.h"
35 #include <iostream>
36 #include <vector>
37 
38 using namespace std;
39 
40 int main(int argc, char *argv[])
41 {
42  // Read camera IDs from command line
43  int numCams = 1;
44  std::vector<int> camIDs(1, CV_CAP_ANY);
45  if (argc > 1) {
46  numCams = argc - 1;
47  camIDs.resize(numCams);
48  for (int i = 0; i < numCams; i++)
49  camIDs[i] = atoi(argv[i+1]);
50  }
51 
52  // Create capture devices
53  std::vector<CvCapture*> camDevs(numCams, NULL);
54  for (int i = 0; i < numCams; i++) {
55  camDevs[i] = cvCaptureFromCAM(camIDs[i]);
56  if (!camDevs[i]) {
57  cout << "Failed to create capture device for ID " << camIDs[i] << endl;
58  return -1;
59  } else {
60  cout << "Created capture device for ID " << camIDs[i] << endl;
61  }
62  }
63 
64  // Create window to display the captured camera images
65  cvNamedWindow("ExampleOpenCVCam", CV_WINDOW_AUTOSIZE);
66 
67  // Show captured camera images and repeat
68  int currentID = 0;
69  bool isRunning = true;
70  std::vector<IplImage*> frames(numCams, NULL);
71  while (isRunning)
72  {
73  // Get frames
74  for (int i = 0; i < numCams; i++)
75  frames[i] = cvQueryFrame(camDevs[i]);
76 
77  // Show current camera's frame
78  if (!frames[currentID]) {
79  cout << "Captured frame is empty!" << endl;
80  isRunning = false;
81  } else {
82  cvShowImage("ExampleOpenCVCam", frames[currentID]);
83  int key = cvWaitKey(10) & 255;
84  if (key == 27)
85  isRunning = false;
86  else if (key == 13)
87  currentID = (currentID + 1) % numCams;
88  else if (key == 8)
89  currentID = (currentID + numCams - 1) % numCams;
90  }
91  }
92 
93  // Release capture devices
94  cout << "Release capture devices and quit" << endl;
95  for (int i = 0; i < numCams; i++)
96  cvReleaseCapture(&camDevs[i]);
97  cvDestroyWindow("ExampleOpenCVCam");
98 
99  return 0;
100 }