Opencv reads video code
# Include "stdafx. H "# include" highgui. H "int main (INT argc, char * argv []) {cvnamedwindow (" Avi "); cvcapture * capture = cvcreatefilecapture (" D: \ sample. avi "); iplimage * frame; while (1) {frame = cvqueryframe (capture); If (! Frame) break; cvshowimage ("Avi", frame); char c = cvwaitkey (33); If (C = 27) break;} cvreleasecapture (& capture ); cvdestroywindow ("Avi"); Return 0 ;}
Struct
Cvcapture
Cvcapture is a struct that stores the information required for image capture. Opencv provides two ways to capture images from the outside:
One is from the camera, and the other is to obtain the image by decoding the video. Both methods must be obtained in sequence from the first frame to the first frame. Therefore, the corresponding status and parameters must be saved after each frame is obtained. For example, if you want to obtain a video file, you need to save the video file name, the corresponding decoder type, and the next time you want to obtain the frame to be decoded. The information is stored in the cvcapture structure. After each frame is obtained, the information is updated. To obtain the next frame, you need to pass the new information to the obtained API interface.
Cvcreatefilecapture (char * name)Input the path of the AVI file to be read, and then the function returns a pointer to the cvcapture struct.
Cvqueryframe (capture)Input a cvcapture pointer. This function is mainly used to load the next frame of a video file to the memory. Unlike cvloadimage, this function does not reallocate memory space.
C = cvwaitkey (33)After the current frame is displayed, wait for 33 Ms. If a key is triggered, C is set to the ASCII code of the key; otherwise, it is set to-1. Another function of cvwaitkey (33) Here is to control the frame rate.
Cvreleasecapture (& capture)Release the memory space opened for the cvcapture struct to close the file handle related to the opened AVI file.
Video usage and operation instructions:
- Initialize camera capture
- Cvcapture * capture = cvcapturefromcam (0); // capture from camera 0
- File initialization capture
- Cvcapture * capture = cvcapturefromavi ("/home/lubo/... Avi"); // capture from a file
- Capture a frame
- Iplimage * IMG = 0;
- Method 1
- If (! Cvgrabframe (capture) {// capture failed exit (0 );}
- IMG = cvretrieveframe (capture );
- Method 2
- IMG = cvqueryframe (capture );
- If you capture images from several cameras at the same time, capture the images from each camera before capturing the images.
- Release the capture source (the image is allocated and released by the capture function, so do not release the image)
- Cvreleasecapture (& capture );
Get video frame information
- Get the properties of the capturing device
Cvqueryframe (capture); int frameh = (INT) cvgetcaptureproperty (capture, cv_cap_prop_frame_height); // video height
Cv_cap_prop_frame_width // video width cv_cap_prop_fps // Number of frames per second
Cv_cap_prop_frame_count // Number of frames cv_cap_prop_fourcc // 4-character code of Codec
Cv_cap_prop_brightness // brightness cv_cap_prop_contrast // contrast
Cv_cap_prop_saturation // saturation cv_cap_prop_hue // color
- Obtains the current position of the frame.
Cv_cap_prop_pos_msec // The video time in milliseconds or the Video timestamp
Cv_cap_prop_pos_frame // 0-based
Cv_cap_prop_avi_ratio // relative location of the video file
Save video files