OpenCV Quick Start


OpenCV is an open-source computer vision library that has an extensive collection of great algorithms. Since one of the latest mergers, OpenCV contains an easy-to-use interface for implementing Super Resolution (SR) based on deep learning methods. The interface contains pre-trained models that can be used for inference very easily and efficiently. So far, it works with C++ and Python.

OpenCV often works together with different frameworks such as Caffe, Pytorch, and Tensorflow in Artificial Intelligence, in particular, Computer Vision. In this post, I will explain what it can do and show how to build a simple Python-based program using OpenCV to read, display, and save frames.

A video is a sequence of fast moving images. The obvious question that follows is how fast are the pictures moving? The measure of how fast the images are transitioning is given by a metric called frames per second (FPS). When someone says that the video has an FPS of 40, it means that 40 images are being displayed every second. Alternatively, after every 25 milliseconds, a new frame is displayed.


Quick Demo

The script below shows a demo of how to read frames from a video source, to display frames on a window, and finally to save frames in a video file as an output.

import cv2

# Input sources
cap = cv2.VideoCapture(0) #camera 
cap = cv2.VideoCapture('./input.mp4') #video source
# Reset the size of input frames, 3 stands for width; 4 stands for height
cap.set(3,640)
cap.set(4,480)
frame_width = int(640)
frame_height = int(480)

# Decoder 
# fourcc = cv2.VideoWriter_fourcc('M','J','P','G') #Mjpeg
fourcc = cv2.VideoWriter_fourcc(*'XVID') #FFmpeg MPEG-4
# framerates is set to 10.0
out = cv2.VideoWriter('./out.avi',fourcc,10.0,(frame_width,frame_height))

while True:
    ret, frame = cap.read()
    # Display frames
    cv2.imshow('Cap',frame)
    # Reset window position
    cv2.moveWindow('Cap', 0, 0,)
    # Write frames 
    out.write(frame)
    if cv2.waitKey(1)==ord('q'):
        break

# Destroy all the frames
cap.release()
out.release()
cv2.destroyAllWindows()

Dig into the code

Read frames

In OpenCV, a video can be read either by using the feed from a camera connected to a computer or by reading a video file. The first step towards reading a video file is to create a VideoCapture object. Its argument can be either the device index or the name of the video file to be read.

In most cases, only one camera is connected to the system. So, all we do is pass ‘0’ and OpenCV uses the only camera attached to the computer. When more than one camera is connected to the computer, we can select the second camera by passing ‘1’, the third camera by passing ‘2’ and so on.

# Create a VideoCapture object and read from the input file
# If the input is taken from the camera, pass 0 instead of the video file name.

cap = cv2.VideoCapture('test.mp4')
cap = cv2.VideoCapture(0) #webcam
Display frames

After reading a video file, we can display the video frame by frame. A frame of a video is simply an image and we display each frame the same way we display images, i.e., we use the function imshow().

# Read until video is completed
while(cap.isOpened()):
  # Capture frame-by-frame
  ret, frame = cap.read()
  if ret == True:
    # Display the resulting frame
    cv2.imshow('Frame',frame)
  # Break the loop
  else: 
    break
# When everything done, release the video capture object
cap.release()
# Closes all the frames
cv2.destroyAllWindows()
Write frames

After we are done with capturing and processing the video frame by frame, the next step we would want to do is to save the video.

For images, it is straightforward. We just need to use cv2.imwrite(). But for videos, we need to toil a bit harder. We need to create a VideoWriter object. First, we should specify the output file name with its format (eg: output.avi). Then, we should specify the FourCC code and the number of frames per second (FPS). Lastly, the frame size should be passed.

# Define the codec and create VideoWriter object.The output is stored in 'out.avi' file.
# Define the fps to be equal to 10. Also, frame size is passed.
out = cv2.VideoWriter('out.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 10, (frame_width,frame_height))

FourCC is a 4-byte code used to specify the video codec. The list of available codes can be found at fourcc.org. There are many FOURCC codes available, but in this post, we will work only with MJPG.


Conclusion

This post is just quick demo demonstrating the basic usage of OpenCV, a powerful tool for Compution Vision in AI. I will be posting more articles about more advanced usage of OpenCV in Object Detection and Object Tracking in the future. Stay tuned !


15 Replies to “OpenCV Quick Start”

  1. Hey there! This is kind of off topic but I need some advice from an established blog. Is it very hard to set up your own blog? I’m not very techincal but I can figure things out pretty fast. I’m thinking about setting up my own but I’m not sure where to begin. Do you have any ideas or suggestions? Thanks

  2. Hello, you used to write wonderful, but the last several posts have been kinda boring?I miss your tremendous writings. Past few posts are just a little bit out of track! come on!

  3. With thanks for sharing your superb website!

  4. Please keep uss up to date like this. Thanks for sharing…

  5. Very nice post. I just stumbled upon your blog and wanted to
    say that I’ve truly enjoyed surfing around your blog posts.
    In any case I will be subscribing too your feed and I hope you write again very soon!

  6. Way cool! Somee very valid points! I appreciate you penning this post and
    the rest of the website is also really good.

  7. Hi there to every body, it’s my first go to see of this webpage; this wevpage contains amazing and really excellent data in support of
    readers.

  8. I need to to tjank you for this fantastic read!! I definitely enjoyed every bit oof it.I have got you book-marked to look
    at new things you post…

  9. Hello! I wish to say that this post is awesome,
    great written and come witfh approximately all important infos.
    I’d like to look extra posts like this! 🙂

  10. Veryy good post! We will be linking to this
    great content on our website.Keep up the great writing!

    1. Thanks. Stay tuned for the upcoming post ~

  11. Woww cuzz this is great work! Congrats and keep it up!

  12. Hi there to every body, it’s mmy first pay a quick visit of this website;
    this blog contains awesom and gehuinely excellent data
    for visitors.

  13. Really Appreciate this article, how can I make is so that I receive an email sent to me when you write a fresh post?

    1. Subscribe on the RSS!

Leave a Reply