My programming ramblings

OpenCV video editing tutorial

Posted on June 4, 2015 by Paul

I’ve always knew that OpenCV can be used to do some video editing, however when I’ve actually tried to use it to open a video file, a few weeks ago, I was amazed at the quantity of misleading or incomplete tutorials you find on the web. I wrote this tutorial to save some time for others like me that need to do some quick and dirty video editing with C++ and OpenCV.

Simplest thing that you could try is to read frames from your computer’s webcam. OpenCV uses the same function, VideoCapture, for opening a video file or a camera attached to your computer, the only difference is that for a camera you will feed the function a number, while for an actual video file you will use the video path.

If your computer has a camera attached to it, you can access it with the next piece of code (use the escape key to close the program):

from which the second parameter, fourcc - 4-character code of codec used to compress the frames, is the most problematic. Basically, you need to know what video codec is provided by your operating system or what codec you have installed. On OS X, for example, I was able to use the MP4V codec to encode videos. The catch was that you need to write the codec four character code in lower cases if you want to actually save the video. Same codec can be used on Windows.

We can modify the above example in order to save the camera feed in a file, see lines 17, 18, 21 and 35 for the important differences between the original and the modified code:

Now, that we can read and write video files, let’s try something more ambitious - modifying an existing video. For the purpose of this tutorial we’ll use an open video like Big Buck Bunny, also we are going to ignore the audio.

When we read a video with OpenCV, we have access to each frame and we can apply any of the typical image processing operations provided by the library to this frame, after that we can save the modified video. Let’s try an experiment: split each frame in three, equal area, vertical rectangles, we leave the left rectangle untouched, we convert to gray the middle rectangle and we apply a Canny edge detector to the right rectangle. Finally, if we copy the modified middle and right rectangles back to the original image, this is what we get:

Please note that the original video used to generate the video included above is (c) copyright 2008, Blender Foundation www.bigbuckbunny.org.

Here is the code I’ve used to obtain the above video:

1 // OpenCV editing a video and saving the result 2 #include<iostream> 3 #include<opencv2/opencv.hpp> 4 5 intmain(){ 6 // Open a video file: 7 cv::VideoCapturecap("big_buck_bunny_720p_h264.mov"); 8 if(!cap.isOpened()){ 9 std::cout<<"Unable to open the camera\n";10 std::exit(-1);11 }12 13 // Get the width/height and the FPS of the vide14 intwidth=static_cast<int>(cap.get(CV_CAP_PROP_FRAME_WIDTH));15 intheight=static_cast<int>(cap.get(CV_CAP_PROP_FRAME_HEIGHT));16 doubleFPS=cap.get(CV_CAP_PROP_FPS);17 18 // Open a video file for writing (the MP4V codec works on OS X and Windows)19 cv::VideoWriterout("output.mov",CV_FOURCC('m','p','4','v'),FPS,cv::Size(width,height));20 if(!out.isOpened()){21 std::cout<<"Error! Unable to open video file for output."<<std::endl;22 std::exit(-1);23 }24 25 cv::Matimage;26 cv::Matpanel_middle,panel_right;27 intdelta=width/3;28 29 while(true){30 cap>>image;31 if(image.empty()){32 std::cout<<"Can't read frames from your camera\n";33 break;34 }35 36 // Copy the middle panel from the image and convert it to gray37 panel_middle=image(cv::Rect(delta,0,image.cols/3,image.rows));38 cv::cvtColor(panel_middle,panel_middle,cv::COLOR_BGR2GRAY);39 cv::cvtColor(panel_middle,panel_middle,cv::COLOR_GRAY2BGR);40 41 // Copy the modified middle panel back to the source image42 panel_middle.copyTo(image(cv::Rect(delta,0,image.cols/3,image.rows)));43 44 // Copy the right panel from the image and convert it to gray45 panel_right=image(cv::Rect(2*delta,0,image.cols/3,image.rows));46 cv::cvtColor(panel_right,panel_right,cv::COLOR_BGR2GRAY);47 48 // Apply a Canny edge detector to the right panel, convert the result to BGR49 cv::Canny(panel_right,panel_right,100,300,3);50 cv::cvtColor(panel_right,panel_right,cv::COLOR_GRAY2BGR);51 52 // Copy the modified right panel back to the source image53 panel_right.copyTo(image(cv::Rect(2*delta,0,image.cols/3,image.rows)));54 55 // Save frame to video56 out<<image;57 58 cv::imshow("Modified video",image);59 60 // Stop the camera if the user presses the "ESC" key61 if(cv::waitKey(1000.0/FPS)==27)break;62 }63 64 return0;65 }

If you want to learn more about OpenCV I would recommend reading OpenCV Essentials by O. D. Suarez:

Disclaimer:All data and information provided on this site is for informational purposes only. solarianprogrammer.com makes no representations as to accuracy, completeness, currentness, suitability, or validity of any information on this site and will not be liable for any errors, omissions, or delays in this information or any losses, injuries, or damages arising from its display or use. All information is provided on an as-is basis. solarianprogrammer.com does not collect any personal information about its visitors except that which they provide voluntarily when leaving comments. This information will never be disclosed to any third party for any purpose. Some of the links contained within this site have my referral id, which provides me with a small commission for each sale. Thank you for understanding.