Reader small image

You're reading from  Mastering OpenCV 4 with Python

Product typeBook
Published inMar 2019
Reading LevelExpert
PublisherPackt
ISBN-139781789344912
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Alberto Fernández Villán
Alberto Fernández Villán
author image
Alberto Fernández Villán

Alberto Fernndez Villn is a software engineer with more than 12 years of experience in developing innovative solutions. In the last couple of years, he has been working in various projects related to monitoring systems for industrial plants, applying both Internet of Things (IoT) and big data technologies. He has a Ph.D. in computer vision (2017), a deep learning certification (2018), and several publications in connection with computer vision and machine learning in journals such as Machine Vision and Applications, IEEE Transactions on Industrial Informatics, Sensors, IEEE Transactions on Industry Applications, IEEE Latin America Transactions, and more. As of 2013, he is a registered and active user (albertofernandez) on the Q&A OpenCV forum.
Read more about Alberto Fernández Villán

Right arrow

Handling Files and Images

In any kind of project, coping with files and images is a key aspect. In this sense, many projects should work with files as forms of data input. Additionally, the project can generate some data after any kind of processing has been done, which can be outputted in the form of files or images. In computer vision, this information flow (input-processing-output) takes special relevance due to the inherent characteristics of these types of projects (for example, images to be processed and models that are generated by machine learning algorithms).

In this chapter, we are going to see how we can handle both files and images. You will learn how to cope with files and images, which are necessary for building computer vision applications.

More specifically, we will cover the following topics:

  • A theoretical introduction to handling files and images
  • Reading/writing...

Technical requirements

An introduction to handling files and images

Before going deeper in handling files and images, we are going to give you an overview of what we will look at in this chapter. This overview is summarized in the following diagram:

In the preceding diagram, you can see that a computer vision project (for example, an OpenCV and Python project) should deal with some input files (for example, files and images). Additionally, after some processing, the project can output some files (for example, images and files). So, in this chapter, we are going to see how to cope with these requirements and how to implement this flow (input-processing-output) properly.

A primary and necessary step to execute a program is to properly cope with command-line arguments, which are parameters that are given to a program or script containing some kind of parameterized information. For example, if you write...

Reading and writing images

In computer vision projects, images are commonly used as command-line arguments in our scripts. In the following sections, we are going to see how we can read and write images.

Reading images in OpenCV

The following example, argparse_load_image.py, shows you how to load an image:

# Import the required packages
import argparse
import cv2

# We first create the ArgumentParser object
# The created object 'parser' will have the necessary information
# to parse the command-line arguments into data types.
parser = argparse.ArgumentParser()

# We add 'path_image' argument using add_argument() including a help. The type of this argument is string (by default)
parser.add_argument("path_image...

Reading camera frames and video files

In some projects, you have to capture camera frames (for example, capture frames with the webcam of your laptop). In OpenCV, we have cv2.VideoCapture, which is a class for video capturing from different sources, such as image sequences, video files, and cameras. In this section, we are going to see some examples to introduce us to this class for capturing camera frames.

Reading camera frames

This first example, read_camera.py, shows you how to read frames from a camera that's connected to your computer. The required argument is index_camera, which indicates the index of the camera to read. If you have connected a webcam to your computer, it has an index of 0. Additionally, if you...

Writing a video file

In this section, we are going to see how we can write to video files using cv2.VideoWriter. However some concepts (for example, fps, codecs, and video file formats) should be introduced first.

Calculating frames per second

In the Reading camera frame and video files section, we saw how we can get some properties from the cv2.VideoCapture object. fps is an important metric in computer vision projects. This metric indicates how many frames are processed per second. It is safe to say that a higher number of fps is better. However, the number of frames your algorithm should process every second will depend on the specific problem you have to solve. For example, if your algorithm should track and detect people...

Playing with video capture properties

In some of the previous examples, we saw how to get some properties from the cv2.VideoCapture object. In this section, we are going to see how we can get all of the properties and understand how they work. Finally, we are going to use these properties to load a video file and output it backwards (showing the last frame of the video first and so on).

Getting all the properties from the video capture object

First, we create the read_video_file_all_properties.py script to show all the properties. Some of these properties only work when we're working with cameras (not with video files). In these cases, a 0 value is returned. Additionally, we have created the decode_fourcc() function...

Summary

In this chapter, we saw that working with images and files is a key element of computer vision projects. A common approach in this kind of project is to load some images first, perform some processing, and then output the processed images. In this chapter, we reviewed this flow. Additionally, in connection with video streams, both cv2.VideoCapture and cv2.VideoWriter were covered. We also looked at the cv2.VideoWriter class for video writing. Two key aspects were reviewed when writing video files—video codecs (for example, DIVX) and video file formats (for example, AVI). To work with video codecs, OpenCV provides FOURCC, a four-byte code. Typical codecs are DIVX, XVID, X264, and MJPG, while typical video file formats are AVI (*.avi), MP4 (*.mp4), QuickTime (*.mov), and Windows Media Video (*.wmv).

We also reviewed the concept of fps and how to calculate it in our...

Questions

  1. What is sys.argv[1]?
  2. Write a piece of code to add a first_number argument of the int type and include the help first number to be added using parser.add_argument().
  3. Write a piece of code to save the imagine img to disk with the name image.png.
  4. Create the capture object using cv2.VideoCapture() to read from the first camera that's connected to your computer.
  5. Create the object capture using cv2.VideoCapture() to read from the first camera connected to your computer and print the CAP_PROP_FRAME_WIDTH property.
  6. Read an image and save it to disk with the same name but ending in _copy.png (for example, logo_copy.png).
  7. Create a script (read_video_file_backwards_save_video.py) that loads a video file and creates another played backwards (containing the last frame of the video first and so on).

Further reading

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering OpenCV 4 with Python
Published in: Mar 2019Publisher: PacktISBN-13: 9781789344912
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Alberto Fernández Villán

Alberto Fernndez Villn is a software engineer with more than 12 years of experience in developing innovative solutions. In the last couple of years, he has been working in various projects related to monitoring systems for industrial plants, applying both Internet of Things (IoT) and big data technologies. He has a Ph.D. in computer vision (2017), a deep learning certification (2018), and several publications in connection with computer vision and machine learning in journals such as Machine Vision and Applications, IEEE Transactions on Industrial Informatics, Sensors, IEEE Transactions on Industry Applications, IEEE Latin America Transactions, and more. As of 2013, he is a registered and active user (albertofernandez) on the Q&A OpenCV forum.
Read more about Alberto Fernández Villán