Opencv2.4 learning notes-use opencv to create a video

Source: Internet
Author: User
Tags spl

The structure of the video file:

  Each video fileNatureIsContainer, FileExtensionOnly indicatesContainer format(For example
Avi, mov, or MKV) andNot the compression format of videos and audios.

  There may be many elements in the container, suchVideo Stream,Audio streamsAnd someSubtitle streamAnd so on. These streamsStorage MethodIs corresponding to each streamCodec). Generally, audio streams are likely to be used.
MP3 or AAC format for storage. More video formats, usually
Xvid, DivX, h264, or lags (lagarith lossless codec.

 

As you can see, video files are indeed much more complex than image files. However, opencv is only a computer vision library rather than a video processing coding library. So developers try to streamline this Part as much as possible, and the result is that only the video that opencv can process is left
AviExtension. Another limitation is that you cannot create a single video larger than 2 GB. In addition, each file can only support one video stream and cannot store audio streams, subtitle streams, and other data. Even so, any system-supported codecs should work here. If the processing capabilities of these videos are insufficient, I think you should find some libraries dedicated to video processing, such
FFmpegOr more codecs suchHuffyuv,CorepngAndLCL. You can use opencv to create an original video stream and convert it to another format through other codecs.
VirtualdubAndAvisynthThis software creates video files of various formats

   Video files are much more complex than image files. However, opencv is only a computer vision library rather than a video processing coding library. So developers try to streamline this Part as much as possible, and the result isOnly the AVI extension is available for videos processed by opencv..

   Another restriction isCannot create more than 2 GBAnd there is a single video in each file.Only one video stream is supported.You cannot store audio streams, subtitle streams, and other data in it.

 

About video codecs:

   The main function of the decoder isVideo SignalProceedCompression and decompression. The computer industry defines the true color of a 24-bit measurement system, which defines millions of colors, close to the limits of human vision. Currently, the most basic VGA display has 640x480 pixels. This means that if the video needs to be played at 30 frames per second, it will transmit up to 27 MB of information per second. At this speed,A 1 GB hard disk can only store video information for about 37 seconds.Therefore, information must be compressed. PassDiscard some digital information or select visual information that is easily overlooked by our eyes and brain, so that the hard disk capacity consumed by videos is reduced.This video compression process is the decoder. The compression ratio of the decoder ranges from ~ A range of makes it possible to process a large amount of video data.

   For Digital Multimedia, the decoder includes the video decoder and audio decoder. the image and sound of digital media use special software encoding formats, such as MPEG4 of the video, MP3, AC3, and DTS of the audio. These encoders can compress and store the original data, they are commonly used encoding formats, and some professional encoding formats are not used in general. Decoding software is usually called a plug-in to replay these videos and audios on a home device or computer. For example, MPEG4 decoding plug-in ffdshow and AC3 decoding plug-in ac3fliter. Only a computer with various decoding extensions can replay these images and sounds.

 

Use opencv to create a video:

1. Its operation is in
In the videowriter class, OPEN function to open,

The Write function sends some image frames to this object in sequence.

2. The following program code mainly includes the following:

   (1): create a video file

    (2): Obtain the Video Decoder

    All video codecs are identified by names of up to four bytes. For example:Xvid,
DivX
, AndH264. This is called fourcc (four character code ). You can useGetFunction to ask the video about the encoding,
GetThe function returns a double number, only because the double contains 64-bit data, because the fourcc encoding occupies only four low bytes, therefore, you can forcibly convert the data to the int type to discard the four high bytes. For detailed code, see the following program.

    (3): release a color channel in the video file.

 

Source code:

# Include <iostream>

# Include <string>

# Include <opencv2/CORE. HPP>
# Include <opencv2/highgui. HPP>

Using namespace STD;
Using namespace CV;
Int main (INT argc, char * argv [], char * window_name)
{
// Read the video
Const string source = "Global. Avi ";
Videocapture inputvideo (source );

// Determine whether the read is successful
If (! Inputvideo. isopened ())
{
Cout <"cocould not open the video" <Endl;
Return-1;

}

String: size_type PAT = source. find_last_of ('.');

Char channelnum;
Cout <"Please input the number of channel you want to release (R, G, B ):";
Cin> channelnum;
Const string name = source. substr (0, Pat) + channelnum + ". Avi ";
Int EX = static_cast <int> (inputvideo. Get (cv_cap_prop_fourcc ));

Char ext [] = {ex & 0xff, (ex & 0xff00)> 8, (ex & 0xff0000)> 16, (ex & 0xff000000)> 24, 0 };
Size S = size (INT) inputvideo. Get (cv_cap_prop_frame_width), (INT) inputvideo. Get (cv_cap_prop_frame_height ));

Char choice;
Cout <"Please input Yes 'y' or no 'n' (N: use the video input format to create an output file. Y: a dialog box is displayed to allow you to select an encoder .) : "<Endl;
Cin> choice;
Const bool askoutputtype = choice = 'y ';

Videowriter outputvideo;

If (askoutputtype)
{
Outputvideo. Open (name, Ex =-1, inputvideo. Get (cv_cap_prop_fps), S, true );
}
Else
Outputvideo. Open (name, ex, inputvideo. Get (cv_cap_prop_fps), S, true );

If (! Outputvideo. isopened ())
{
Cout <"cocould not open the output video for write:" <source <Endl;
Return-1;
}

// Output video size and frames
Cout <"input frame resolution: width =" <S. width <"Height =" <S. Height
<"Of NR #:" <inputvideo. Get (cv_cap_prop_frame_count) <Endl;

// Output encoder type
Cout <"input codec type:" <ext <Endl;

Int channel = 2;
Switch (channelnum)
{
Case 'r': {channel = 2; break ;}
Case 'G': {channel = 1; break ;}
Case 'B': {channel = 0; break ;}
}

Mat SRC, Res;
Vector <mat> SPL;

While (true)
{
Inputvideo> SRC;
If (SRC. Empty () break;
Split (SRC, Spl );
For (INT I = 0; I <3; I ++)
If (I! = Channel)
SPL [I] = mat: zeros (S, SPL [0]. Type ());

Merge (SPL, Res );

Outputvideo <res;

}
Cout <"finished writing" <Endl;

Return 0;
}

Running result:

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.