My opencv Study Notes (12): videocapture class

Source: Internet
Author: User

This time, we will introduce some basic operations of the videocapture class through a comprehensive example of video reading:

# Include <opencv2/CORE/core. HPP> # include <opencv2/highgui. HPP> # include <opencv2/imgproc. HPP> # include <iostream> using namespace STD; using namespace CV; int main () {// open the video file: Actually, A videocapture structure videocapture capture ("D: /videos/petsd2tec2. avi "); // check whether it is opened normally: when it is opened successfully, isopened returns tureif (! Capture. isopened () cout <"fail to open! "<Endl; // get the total number of frames long totalframenumber = capture. get (cv_cap_prop_frame_count); cout <"total video" <totalframenumber <"frame" <Endl; // set the start frame () Long frametostart = 300; capture. set (cv_cap_prop_pos_frames, frametostart); cout <"starting from" <frametostart <"starting from frame read" <Endl; // sets the end frame int frametostop = 400; if (frametostop <frametostart) {cout <"the end frame is smaller than the start frame. The program is incorrect and will exit soon! "<Endl; Return-1 ;}else {cout <" end frame: "<frametostop <" frame "<Endl ;} // obtain the frame rate double rate = capture. get (cv_cap_prop_fps); cout <"Frame Rate:" <rate <Endl; // defines a variable bool stop = false to control the end of the read Video loop; // The Mat frame of the image that carries each frame; // display the namedwindow ("extracted frame") of each frame; // The interval between the two frames: // int delay = 1000/rate; int delay = 1000/rate; // The while loop is used to read frames. // currentframe is the variable long currentframe = frametostart after the specified frame is read in the loop body. // The Int kernel_size of the core of the filter. = 3; MAT kernel = mat: ones (kernel_size, kernel_size, cv_32f)/(float) (kernel_size * kernel_size); While (! Stop) {// read the next frame if (! Capture. read (FRAME) {cout <"failed to read the video" <Endl; Return-1 ;}// Add the filter program imshow ("extracted frame", frame) here ); filter2d (frame, frame,-1, kernel); imshow ("after filter", frame ); cout <"reading the nth" <currentframe <"frame" <Endl; // waitkey (INT delay = 0) will always wait when delay ≤ 0; when delay is greater than 0, it will wait for delay in milliseconds // when no key is pressed before the end of the time, the returned value is-1; otherwise, the Return key is int c = waitkey (Delay ); // Press ESC or exit to read the video if (char) C = 27 | currentframe> frametostop) {stop = true ;} // press the button and it will stay in the current frame. Wait for the next button if (C> = 0) {waitkey (0) ;}currentframe ++ ;} // disable the video file capture. release (); waitkey (0); Return 0 ;}

The comments are quite detailed. I believe everyone can understand them. Here are some additional notes:

1. Because the original video was collected by a web camera, there are many snowflake points. Here we perform simple mean filtering.

2. Although the videocapture class includes grab (capture the next frame) and retrieve (decode the frame) operations, it is relatively simple to use read directly.

3. The get function is very powerful. You can obtain most of the video information. For more information, see the help manual.

4. latency is added between frames to ensure video playing smoothness. This latency is calculated by frame rate.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.