OpenCV 3.x with Python By Example
eBook - ePub

OpenCV 3.x with Python By Example

  1. 268 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

OpenCV 3.x with Python By Example

Book details
Book preview
Table of contents
Citations

About This Book

Learn the techniques for object recognition, 3D reconstruction, stereo imaging, and other computer vision applications using examples on different functions of OpenCV.

Key Features

  • Learn how to apply complex visual effects to images with OpenCV 3.x and Python
  • Extract features from an image and use them to develop advanced applications
  • Build algorithms to help you understand image content and perform visual searches
  • Get to grips with advanced techniques in OpenCV such as machine learning, artificial neural network, 3D reconstruction, and augmented reality

Book Description

Computer vision is found everywhere in modern technology. OpenCV for Python enables us to run computer vision algorithms in real time. With the advent of powerful machines, we have more processing power to work with. Using this technology, we can seamlessly integrate our computer vision applications into the cloud. Focusing on OpenCV 3.x and Python 3.6, this book will walk you through all the building blocks needed to build amazing computer vision applications with ease.

We start off by manipulating images using simple filtering and geometric transformations. We then discuss affine and projective transformations and see how we can use them to apply cool advanced manipulations to your photos like resizing them while keeping the content intact or smoothly removing undesired elements. We will then cover techniques of object tracking, body part recognition, and object recognition using advanced techniques of machine learning such as artificial neural network. 3D reconstruction and augmented reality techniques are also included. The book covers popular OpenCV libraries with the help of examples.

This book is a practical tutorial that covers various examples at different levels, teaching you about the different functions of OpenCV and their actual implementation. By the end of this book, you will have acquired the skills to use OpenCV and Python to develop real-world computer vision applications.

What you will learn

  • Detect shapes and edges from images and videos
  • How to apply filters on images and videos
  • Use different techniques to manipulate and improve images
  • Extract and manipulate particular parts of images and videos
  • Track objects or colors from videos
  • Recognize specific object or faces from images and videos
  • How to create Augmented Reality applications
  • Apply artificial neural networks and machine learning to improve object recognition

Who this book is for

This book is intended for Python developers who are new to OpenCV and want to develop computer vision applications with OpenCV and Python. This book is also useful for generic software developers who want to deploy computer vision applications on the cloud. It would be helpful to have some familiarity with basic mathematical concepts such as vectors, matrices, and so on.

Frequently asked questions

Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes, you can access OpenCV 3.x with Python By Example by Gabriel Garrido Calvo, Prateek Joshi, Naren Yellavula in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming in Python. We have over one million books available in our catalogue for you to explore.

Information

Year
2018
ISBN
9781788396769
Edition
2

Applying Geometric Transformations to Images

In this chapter, we are going to learn how to apply cool geometric effects to images. Before we get started, we need to install OpenCV-Python. We will explain how to compile and install the necessary libraries to follow every example in this book.
By the end of this chapter, you will know:
  • How to install OpenCV-Python
  • How to read, display, and save images
  • How to convert to multiple color spaces
  • How to apply geometric transformations such as translation, rotation,
    and scaling
  • How to use affine and projective transformations to apply funny geometric effects to photos

Installing OpenCV-Python

In this section, we explain how to install OpenCV 3.X with Python 2.7 on multiple platforms. If you desire it, OpenCV 3.X also supports the use of Python 3.X and it will be fully compatible with the examples in this book. Linux is recommended as the examples in this book were tested on that OS.

Windows

In order to get OpenCV-Python up and running, we need to perform the following steps:
  1. Install Python: Make sure you have Python 2.7.x installed on your machine. If you don't have it, you can install it from: https://www.python.org/downloads/windows/.
  2. Install NumPy: NumPy is a great package to do numerical computing in Python. It is very powerful and has a wide variety of functions. OpenCV-Python plays nicely with NumPy, and we will be using this package a lot during the course of this book. You can install the latest version from: http://sourceforge.net/projects/numpy/files/NumPy/.
We need to install all these packages in their default locations. Once we install Python and NumPy, we need to ensure that they're working fine. Open up the Python shell and type the following:
 >>> import numpy 
If the installation has gone well, this shouldn't throw up any errors. Once you confirm it, you can go ahead and download the latest OpenCV version from: http://opencv.org/downloads.html.
Once you finish downloading it, double-click to install it. We need to make a couple of changes, as follows:
  1. Navigate to opencv/build/python/2.7/.
  2. You will see a file named cv2.pyd. Copy this file to C:/Python27/lib/site-packages.
You're all set! Let's make sure that OpenCV is working. Open up the Python shell and type the following:
 >>> import cv2
If you don't see any errors, then you are good to go! You are now ready to use OpenCV-Python.

macOS X

To install OpenCV-Python, we will be using Homebrew. Homebrew is a great package manager for macOS X and it will come in handy when you are installing various libraries and utilities on macOS X. If you don't have Homebrew, you can install it by running the following command in your terminal:
$ ruby -e "$(curl -fsSL
https://raw.githubusercontent.com/Homebrew/install/master/install)"
Even though OS X comes with inbuilt Python, we need to install Python using Homebrew to make our lives easier. This version is called brewed Python. Once you install Homebrew, the next step is to install brewed Python. Open up the terminal, and type the following:
$ brew install python
This will automatically install it as well. Pip is a package management tool to install packages in Python, and we will be using it to install other packages. Let's make sure the brewed Python is working correctly. Go to your terminal and type the following:
$ which python 
You should see /usr/local/bin/python printed on the terminal. This means that we are using the brewed Python, and not the inbuilt system Python. Now that we have installed brewed Python, we can go ahead and add the repository, homebrew/science, which is where OpenCV is located. Open the terminal and run the following command:
$ brew tap homebrew/science 
Make sure the NumPy package is installed. If not, run the following in your terminal:
$ pip install numpy
Now, we are ready to install OpenCV. Go ahead and run the following command from your terminal:
$ brew install opencv --with-tbb --with-opengl
OpenCV is now installed on your machine, and you can find it at /usr/local/Cellar/opencv/3.1.0/. You can't use it just yet. We need to tell Python where to find our OpenCV packages. Let's go ahead and do that by symlinking the OpenCV files. Run the following commands from your terminal (please, double check that you are actually using the right versions, as they might be slightly different):
$ cd /Library/Python/2.7/site-packages/
$ ln -s /usr/local/Cellar/opencv/3.1.0/lib/python2.7/site-packages/cv.py
cv.py
$ ln -s /usr/local/Cellar/opencv/3.1.0/lib/python2.7/site-packages/cv2.so
cv2.so
You're all set! Let's see if it's installed properly. Open up the Python shell and type the following:
 > import cv2 
If the installation went well, you will not see any error messages. You are now ready to use OpenCV in Python.
If you want to use OpenCV within a virtual environment, you could follow the instructions in the Virtual environments section, applying small changes to each of the commands for macOS X.

Linux (for Ubuntu)

First, we need to install the OS requirements:
[compiler] $ sudo apt-get install build-essential
[required] $ sudo apt-get install cmake git libgtk2.0-dev pkg-config
libavcodec-dev libavformat-dev libswscale-dev git
libgstreamer0.10-dev libv4l-dev
[optional] $ sudo apt-get install python-dev python-numpy libtbb2
libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev
libdc1394-22-dev
Once the OS requirements are installed, we need to download and compile the latest version of OpenCV along with several supported flags to let us implement the following code samples. Here we are going to install Version 3.3.0:
$ mkdir ~/opencv
$ git clone -b 3.3.0 https://github.com/opencv/opencv.git opencv
$ cd opencv
$ git clone https://github.com/opencv/opencv_contrib.git opencv_contrib
$ mkdir release
$ cd release
$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=OFF -D OPENCV_EXTRA_MODULES_PATH=~/opencv/opencv_contrib/modules -D BUILD_PYTHON_SUPPORT=ON -D WITH_XINE=ON -D WITH_OPENGL=ON -D WITH_TBB=ON -D WITH_EIGEN=ON -D BUILD_EXAMPLES=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4...

Table of contents

  1. Title Page
  2. Copyright and Credits
  3. Contributors
  4. Packt Upsell
  5. Preface
  6. Applying Geometric Transformations to Images
  7. Detecting Edges and Applying Image Filters
  8. Cartoonizing an Image
  9. Detecting and Tracking Different Body Parts
  10. Extracting Features from an Image
  11. Seam Carving
  12. Detecting Shapes and Segmenting an Image
  13. Object Tracking
  14. Object Recognition
  15. Augmented Reality
  16. Machine Learning by an Artificial Neural Network
  17. Other Books You May Enjoy