Reader small image

You're reading from  Learning OpenCV 4 Computer Vision with Python 3 - Third Edition

Product typeBook
Published inFeb 2020
Reading LevelIntermediate
PublisherPackt
ISBN-139781789531619
Edition3rd Edition
Languages
Tools
Right arrow
Authors (2):
Joseph Howse
Joseph Howse
author image
Joseph Howse

Joseph Howse lives in a Canadian fishing village, where he chats with his cats, crafts his books, and nurtures an orchard of hardy fruit trees. He is President of Nummist Media Corporation, which exists to support his books and to provide mentoring and consulting services, with a specialty in computer vision. On average, in 2015-2022, Joseph has written 1.4 new books or new editions per year for Packt. He also writes fiction, including an upcoming novel about the lives of a group of young people in the last days of the Soviet Union.
Read more about Joseph Howse

Joe Minichino
Joe Minichino
author image
Joe Minichino

Joe Minichino is an R&D labs engineer at Teamwork. He is a passionate programmer who is immensely curious about programming languages and technologies and constantly experimenting with them. Born and raised in Varese, Lombardy, Italy, and coming from a humanistic background in philosophy (at Milan's Università Statale), Joe has lived in Cork, Ireland, since 2004. There, he became a computer science graduate at the Cork Institute of Technology.
Read more about Joe Minichino

View More author details
Right arrow

Setting Up OpenCV

You've picked up this book, so you may already have an idea of what OpenCV is. Maybe you heard of capabilities that seem to come straight out of science fiction, such as training an artificial intelligence model to recognize anything that it sees through a camera. If this is your interest, you will not be disappointed! OpenCV stands for Open Source Computer Vision. It is a free computer vision library that allows you to manipulate images and videos to accomplish a variety of tasks, from displaying frames from a webcam to teaching a robot to recognize real-life objects.

In this book, you will learn to leverage the immense potential of OpenCV with the Python programming language. Python is an elegant language with a relatively shallow learning curve and very powerful features. This chapter is a quick guide to setting up Python 3, OpenCV 4, and other dependencies...

Technical requirements

This chapter assumes that you are using one of the following operating systems:

  • Windows 7 SP1 or a later version
  • macOS 10.7 (Lion) or a later version
  • Debian Jessie or a later version, or a derivative such as the following:
    • Ubuntu 14.04 or a later version
    • Linux Mint 17 or a later version

For editing Python scripts and other text files, this book's authors simply recommend that you should have a good text editor. Examples include the following:

  • Notepad++ for Windows
  • BBEdit (free version) for macOS
  • GEdit for the GNOME desktop environment on Linux
  • Kate for the KDE Plasma desktop environment on Linux

Besides the operating system, there are no other prerequisites for this setup chapter.

What's new in OpenCV 4

If you are an OpenCV veteran, you might want to know more about OpenCV 4's changes before you decide to install it. Here are some of the highlights:

  • The C++ implementation of OpenCV has been updated to C++11. OpenCV's Python bindings wrap the C++ implementation, so as Python users, we may gain some performance advantages from this update, even though we are not using C++ directly.
  • The deprecated C implementation of OpenCV and the deprecated Python bindings for the C implementation have been removed.
  • Many new optimizations have been implemented. Existing OpenCV 3 projects can take advantage of many of these optimizations without further changes beyond updating the OpenCV version. For OpenCV C++ projects, an entirely new optimization pipeline named G-API is available; however, OpenCV's Python bindings currently do not support this optimization...

Choosing and using the right setup tools

We are free to choose various setup tools, depending on our operating system and how much configuration we want to do.

Regardless of the choice of operating system, Python offers some built-in tools that are useful for setting up a development environment. These tools include a package manager called pip and a virtual environment manager called venv. Some of this chapter's instructions will cover pip specifically, but if you would like to learn about venv, please refer to the official Python documentation at https://docs.python.org/3/library/venv.html.

You should consider using venv if you plan to maintain a variety of Python projects that might have conflicting dependencies for example, projects that depend on different versions of OpenCV. Each of venv's virtual environments has its own set of installed libraries, and...

Running samples

Running a few sample scripts is a good way to test whether OpenCV has been set up correctly. Some samples are included in OpenCV's source code archive. If you have not already obtained the source code, go to https://opencv.org/releases/ and download one of the following archives:

  • For Windows, download the latest archive, labeled Windows. It is a self-extracting ZIP. Run it and, when prompted, enter any destination folder, which we will refer to as <opencv_unzip_destination>. Find the Python samples in <opencv_unzip_destination>/opencv/samples/python.
  • For other systems, download the latest archive, labeled Sources. It is a ZIP file. Unzip it to any destination folder, which we will refer to as <opencv_unzip_destination>. Find the Python samples in <opencv_unzip_destination>/samples/python.

Some of the sample scripts require command...

Finding documentation, help, and updates

OpenCV's documentation can be found at http://docs.opencv.org/, where you can either read it online or download it for offline reading. If you write code on airplanes or other places without internet access, you will definitely want to keep offline copies of the documentation.

The documentation includes a combined API reference for OpenCV's C++ API and its Python API. When you look up a class or function, be sure to read the section under the heading Python.

OpenCV's Python module is named cv2. The 2 in cv2 has nothing to do with the version number of OpenCV; we really are using OpenCV 4. Historically, there was a cv Python module that wrapped a now-obsolete C version of OpenCV. The cv module does not exist anymore in OpenCV 4. However, the OpenCV documentation sometimes erroneously refers to the module name as cv instead...

Summary

By now, we should have an OpenCV installation that will serve our needs for the diverse projects described in this book. Depending on which approach we took, we may also have a set of tools and scripts that can be used to reconfigure and rebuild OpenCV for our future needs.

Now, we also know where to find OpenCV's Python samples. These samples covered a different range of functionalities outside this book's scope, but they are useful as additional learning aids.

In the next chapter, we will familiarize ourselves with the most basic functions of the OpenCV API, namely, displaying images and videos, capturing videos through a webcam, and handling basic keyboard and mouse inputs.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learning OpenCV 4 Computer Vision with Python 3 - Third Edition
Published in: Feb 2020Publisher: PacktISBN-13: 9781789531619
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

Authors (2)

author image
Joseph Howse

Joseph Howse lives in a Canadian fishing village, where he chats with his cats, crafts his books, and nurtures an orchard of hardy fruit trees. He is President of Nummist Media Corporation, which exists to support his books and to provide mentoring and consulting services, with a specialty in computer vision. On average, in 2015-2022, Joseph has written 1.4 new books or new editions per year for Packt. He also writes fiction, including an upcoming novel about the lives of a group of young people in the last days of the Soviet Union.
Read more about Joseph Howse

author image
Joe Minichino

Joe Minichino is an R&D labs engineer at Teamwork. He is a passionate programmer who is immensely curious about programming languages and technologies and constantly experimenting with them. Born and raised in Varese, Lombardy, Italy, and coming from a humanistic background in philosophy (at Milan's Università Statale), Joe has lived in Cork, Ireland, since 2004. There, he became a computer science graduate at the Cork Institute of Technology.
Read more about Joe Minichino