A Quick Guide to OpenCV: Read and Write Video Files

Part 2: Read and write Video files and Blur ROI

Arjit Sharma
Analytics Vidhya

--

Till now based on Part 1, we have done all the configurations and installation and now our MacBook is ready to play with this amazing library OpenCV.
Next step is to write and execute Cpp program [Github Repo link]to read and write a video file. Lets start step by step:

Step 1:

Open Xcode and click on Create New Project.

Step2:

Click on macOS and selection application type as Command Line Tool.

And then write the name of your application as per your choice

and then finally choose and the location to create the project. Thats it..! Step 2 completed. Go Have a quick tea.

Step 3:

The next screen you would see is your application Project Setting screen. On the the general tab, under Framework and Libraries add OpenCV libraries. Click on small “+” symbol and add all the library files which we downloaded in part 1. Location is opencv-4.1.1buildlib → All “*.4.1.1.dylib” files.

Step4:

Next, go to Build Setting and update following three paths, based on your installation:

  1. Framework Search Path
  2. Header Search Path
  3. Library Search Path

So guys, you are ready for a launch now. All setup part is complete. Now click on main.cpp file and try compile and build the code to do a sense check and confirm everything is working fine till here. Next step will be to start exploring OpenCV library (Class and Functions).

Step 5:

So everything is now ready for you to start coding.
In this step I will share the details of key OpenCV library Class and its functions and my codebase from Github repository that you can download and execute on your machine.

  1. The most important class is VideoCapture. We can use open() & read() function of this class to open and read any video file.
  2. The second one is VideoWriter. We can open() & write() to open and write a video file
  3. And finally imshow() function to display the video.

I am displaying some key bit of my Git Repo here.

//------- Initial setup ----------
VideoCapture cap;
cap.open("SampleIn.mp4");
if(!cap.isOpened()){
cout << "Error opening video stream or file" << endl;
return -1;
}

frameWidth = cap.get(CAP_PROP_FRAME_WIDTH);
frameHeight = cap.get(CAP_PROP_FRAME_HEIGHT);
VideoWriter video;
// Define the codec and create VideoWriter object. video.open("SampleOut.mp4",cv::VideoWriter::fourcc('M','J','P','G'),70, Size(frameWidth,frameHeight));
// ---- Reading & Writing the Video --------
while(1){
cap >> frame; // Read frames
if (frame.empty())
break;
video.write(frame); // Write file frame by frame
imshow( "Frame", frame );
// Press ESC on keyboard to exit
char c=(char) waitKey(25);
if(c==27)
break;
}
// ---- Gracefully End -----
cap.release();
destroyAllWindows();

The Repository have a lot of other functionality as well like blur ROI, blur the face using HaarC, take screenshot of the video, play and pause video. If you follow the repo commit by commit you can get all the details in there.

I hope you find both these articles and the code repository, good enough to do your hands dirty using OpenCV. Please let me know, if you have any question regarding the code repository and I’ll try to respond with best of my knowledge. If you find this article series useful, let me know with a clap :-).

--

--

Arjit Sharma
Analytics Vidhya

My favourite Quote — printf(“Hello World..!”)