ocean 发表于 2014-3-3 16:52:30

Displaying the video feed from default camera device

Displaying the video feed from default camera device
// Program to display a video from attached default camera device
// Author: Samarth Manoj Brahmbhatt, University of Pennsylvania
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
// 0 is the ID of the built-in laptop camera, change if you want to use other camera
VideoCapture cap(0);
//check if the file was opened properly
if(!cap.isOpened())
{
cout << "Capture could not be opened successfully" << endl;
return -1;
}
namedWindow("Video");
// Play the video in a loop till it ends
while(char(waitKey(1)) != 'q' && cap.isOpened())
{
Mat frame;
cap >> frame;
// Check if the video is over
if(frame.empty())
{
cout << "Video over" << endl;
break;
}
imshow("Video", frame);
}
return 0;
}
The code itself is self-explanatory, and I would like to touch on just a couple of lines.
VideoCapture cap(0);
This creates a VideoCapture object that is linked to device number 0 (default device) on your computer. And
cap >> frame;
extracts a frame from the device to which the VideoCapture object cap is linked. There are some other ways to extract
frames from camera devices, especially when you have multiple cameras and you want to synchronize
them (extract
frames from all of them at the same time). I shall introduce such methods in Chapter 10.
You can also give a file name to the VideoCapture constructor, and OpenCV will play the video in that file for you
exactly in the same manner (see Listing 4-5).
页: [1]
查看完整版本: Displaying the video feed from default camera device