OpenCV the video into an equal-length small fragment _opencv

Source: Internet
Author: User

Recently on the phone some gesture data, so need to use the phone's video function to collect gesture data, we take the approach is to record a whole video, of which three seconds to change the action, so in the post-processing process, only need to a whole video every three seconds cut into a small paragraph, In this way, you can get every gesture as a training sample, which is certainly something.

To share here are some of the problems encountered during the interception of video, there are two main problems:

1, on the compression parameter CV_FOURCC problem;

2, about the mobile phone front camera frame rate change problem.

My code runs under win10+vs2013+opencv3.1.

For the operation of the video, mainly used in the OpenCV Videocapture class and Videowriter class, about these two classes of operations (that is, read and write video), you can refer to the following links:

The reading and writing of video in Http://blog.jobbole.com/84231/OpenCV is in frames, we define an object of the Videocapture class, and then call the read () member function of the object to realize the reading of each frame of the video. Similarly, the definition of a Videowriter class object, you can achieve video writing.

One of the things that needs to be noted here is:

	Videowriter writer;	Defines examples of videowriter of video-writing classes
	Writer.open (videopathcuttotal, CV_FOURCC (' D ', ' I ', ' V ', ' X '), rate, videosize);
When calling the Videowriter class object writer The open function, there is a CV_FOURCC parameter, which indicates that the compression method used when the video is written, different compression methods will have different effects, of course, due to the codec reason, Some compression methods may not be supported, in the beginning, I set the video format to. mp4, but it always goes wrong (here I am, when reading the video is. Mp4/.avi/.mov are all OK), and later I changed the format of the writing to. avi. Therefore, the feeling of using opencv words, or to the. avi format of the operation of video is relatively safer.

For CV_FOURCC parameters, you can view the following links:

Http://www.07net01.com/program/144635.html In addition to the above problems, there is also a more interesting phenomenon is that I used the Huawei Glory 6 of the front camera video frame rate is changed, for example, I recorded a, B, C, D, E Five video, may A, C frame rate is 15 frames/sec, while the B, e frame rate is 24 frames/sec, D may be another frame rate. More interested in this phenomenon, I took a few friends of the mobile phone to do a simple test.

I found that I tested the apple, the charm, Samsung mobile phone's front camera frame rate is unchanged, and Huawei, Millet mobile phone's front camera frame rate is changing. I have simply done a test here, may not have any representativeness, and we note that the discussion here is the "front" camera, interested friends can use their own mobile phone to try, test.

To say this is just to think about the possibility of having friends in the video clip later, it may be helpful to notice such a detail.

So, finally, I chose to use the front camera video frame rate fixed by the Apple phone as a sample of the acquisition cell phone (the cell phone frame rate of 30 frames/sec), in this case, I only need to do a simple count in the program, when the number of read and write 90 (that is, 3 seconds long), the output of the video saved as a small segment , you can refer to the following code:

#include <iostream> #include <string> #include <opencv2/opencv.hpp> using namespace std;

using namespace CV;		String Num2str (double);
	A function that you define to convert a number to string int main () {int videonum = 0;	String Videopath = "F:\\my_desktop\\video\\img_0098.mov";		To be cut video string videopathcut = "f:\\my_desktop\\video\\cut1\\";
	After cutting the video Store folder string videopathcuttotal = "F:\\my_desktop\\video\\cut1\\0.avi";	Videocapture capture (Videopath);
		Defines an instance of the video processing class Videocapture if (!capture.isopened ()) {cout << "Loading error!\n";
	Abort ();	int rate = 30;		Frame rate Size videosize (Capture.get (cv_cap_prop_frame_width), Capture.get (Cv_cap_prop_frame_height));	Gets the wide and high specification Videowriter writer of the read video;
	Defines examples of videowriter of video-writing classes Writer.open (Videopathcuttotal, CV_FOURCC (' D ', ' I ', ' V ', ' X '), rate, videosize);
	Mat frame;
			while (1) {if (Capture.read (frame)) {videonum++;
			Writer << frame; if (!) ( videonum% (rate*3))) {videopathcuttotal = Videopathcut + num2str (videonUM/(Rate * 3)) + ". avi"; When reading rate * 3 images (here rate=30, so for 90), modify the path written by the video to perform a segmented effect on the video Writer.open (Videopathcuttotal, CV_FOURCC (' D ', ' I ', ' V ', ')
			X '), rate, videosize);
		} else {break;
	}} string Num2str (double i) {StringStream ss;
	SS << I;

return Ss.str (); }




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.