This is an example of opening the camera with a OpenCV2.4.10, see the code below:
#include <iostream>#include<stdio.h>#include<opencv2/opencv.hpp>using namespaceCV;using namespacestd;intMainintargcChar*argv[]) {Cvcapture* Cam0 = Cvcapturefromcam (0); if(!cam0) {fprintf (stderr,"Could not initialize opening of Camera 0..\n"); System ("Pause"); return-1; } printf ("Cam0 initialized\n"); Cvnamedwindow ("Camera 0", cv_window_autosize);//Create a window called "Camera 0" while(1) {Iplimage*cam0frame =Cvqueryframe (CAM0); if(cam0frame) {cvshowimage ("Camera 0", Cam0frame); } if(Cvwaitkey ( -) == -)//wait for the ' ESC ' key press for 30ms. If ' ESC ' key is pressed, break loop{cout<<"ESC key is pressed by user"<<Endl; Break; }} cvreleasecapture (&cam0); Cvdestroywindow ("Camera 0"); return 0;}
If you want to open two cameras at the same time, see my previous blog OpenCV show two cameras display two cameras simultaneously.
OpenCV open camera turn on webcam