- OpenCV Essentials
- Oscar Deniz Suarez等
- 473字
- 2021-04-02 10:03:16
Live input from a camera
Usually, the computer vision problems we face are related with processing live video input from one or several cameras. In this section, we will describe the recLiveVid
example, which grabs a video stream from a webcam (connected to our computer), displays the stream in a window, and records it in a file (recorded.avi
). By default, in the following example, the video capture is taken from the camera with cam_id=0
. However, it is possible to handle a second camera (cam_id=1
) and grab the video from it, setting an argument at the command line:
//… (omitted for brevity) int main(int argc, char *argv[]) { Mat frame; const char win_name[]="Live Video..."; const char file_out[]="recorded.avi"; int cam_id=0; // Webcam connected to the USB port double fps=20; if (argc == 2) sscanf(argv[1], "%d", &cam_id); VideoCapture inVid(cam_id); // Open camera with cam_id if (!inVid.isOpened()) return -1; int width = (int)inVid.get(CV_CAP_PROP_FRAME_WIDTH); int height = (int)inVid.get(CV_CAP_PROP_FRAME_HEIGHT); VideoWriter recVid(file_out, CV_FOURCC('F','F','D','S'), fps, Size(width, height)); if (!recVid.isOpened()) return -1; namedWindow(win_name); while (1) { inVid >> frame; // Read frame from camera recVid << frame; // Write frame to video file imshow(win_name, frame); // Show frame if (waitKey(1000/fps) >= 0) break; } inVid.release(); // Close camera return 0; }
The code explanation is given as follows:
VideoCapture::VideoCapture(int device)
– This class constructor initializes aVideoCapture
object to receive a video from a camera rather than a file. In the following code example, it is used with a camera identifier:VideoCapture inVid(cam_id); // Open camera with cam_id
VideoWriter::VideoWriter(const string& filename, int fourcc, double fps, Size frameSize, bool isColor=true)
– This class constructor creates an object to write a video stream to a file with the name passed as the first argument. The second argument identifies the video codec with a code of four single characters (for example, in the previous sample code, FFDS stands forffdshow
). Obviously, only codecs actually installed in the local system can be used. The third argument indicates the frames per second of the recording. This property can be obtained from theVideoCapture
object with theVideoCapture::get
method, although it may return0
if the property is not supported by the backend. TheframeSize
argument indicates the total size for each frame of the video that is going to be written. This size should be the same as the input video grabbed. Finally, the last argument allows writing the frame in color (default) or in grayscale. In the example code, the constructor is used with theffdshow
codec and the size of the video capture is as follows:int width = (int)inVid.get(CV_CAP_PROP_FRAME_WIDTH); int height = (int)inVid.get(CV_CAP_PROP_FRAME_HEIGHT); VideoWriter recVid(file_out, CV_FOURCC('F','F','D','S'), fps,Size(width, height));
void VideoCapture::release()
– This method closes the capturing device (webcam) or the video file. This method is always called implicitly at the end of the program. However, in the preceding example, it is called explicitly to avoid wrong termination of the output file (only noticeable when playing the recorded video).
推薦閱讀
- PPT辦公高手應(yīng)用技巧
- 新編電腦辦公(Windows 7 + Office 2013版)從入門(mén)到精通
- Excel高效數(shù)據(jù)處理分析:效率是這樣煉成的!
- 新編Office 2013從入門(mén)到精通
- Word/Excel 2003辦公應(yīng)用實(shí)戰(zhàn)從入門(mén)到精通
- 精通Excel數(shù)據(jù)統(tǒng)計(jì)與分析
- 新編Word/Excel/PPT 2016從入門(mén)到精通
- Excel函數(shù)與公式應(yīng)用技巧
- 從零開(kāi)始學(xué)Excel VBA
- Word/Excel/PPT 2016高效辦公實(shí)戰(zhàn)從入門(mén)到精通
- Excel市場(chǎng)分析應(yīng)用之道
- Excel商務(wù)數(shù)據(jù)分析:思維、策略與方法
- PowerPoint 2010辦公專(zhuān)家從入門(mén)到精通(精編版)
- Excel 2013操作與技巧
- 新編PowerPoint 2016從入門(mén)到精通