Opencv basics-video reading and operations

Source: Internet
Author: User
Tags time in milliseconds

 

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

  • Initialize video writing
        Cvcideowriter * Writer = 0;
        Int iscolor = 1;
        Int FPS = 25; // 30
        Int framew = 640;
        Int frameh = 480;
    Writer = cvcreatevideowriter ("out. avi ", cv_fourcc ('P', 'I', 'M', '1'), FPS, cvsize (framew, frameh), iscolor );
  • The format of video writing is also
        Cv_fourcc ('M', 'J', 'P', 'G ');
        Cv_fourcc ('M', 'P', '4', '2 ');
    Cv_fourcc ('D', 'I', 'V', '3 ');
        Cv_fourcc ('D', 'I', 'V', 'x ');
        Cv_fourcc ('U', '2', '6', '3 ');
        Cv_fourcc ('I', '2', '6', '3 ');
      Cv_fourcc ('F', 'l', 'V', '1 ');
  • Write video files
        Iplimage * IMG = 0;
        Int nframes = 50;
        For (INT I = 0; I <nframes; I ++ ){
          Cvgrabframe (capture );
          IMG = cvretreveframe (capture );
        Cvwriteframe (writer, IMG );
      }
  • Release video write

    Cvreleasevideowriter (& writer );

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.