Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying OpenCV with Python By Example
  • Table Of Contents Toc
OpenCV with Python By Example

OpenCV with Python By Example

By : Prateek Joshi
3.5 (10)
close
close
OpenCV with Python By Example

OpenCV with Python By Example

3.5 (10)
By: Prateek Joshi

Overview of this book

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 are getting more processing power to work with. Using this technology, we can seamlessly integrate our computer vision applications into the cloud. Web developers can develop complex applications without having to reinvent the wheel. This book will walk you through all the building blocks needed to build amazing computer vision applications with ease. We start off with applying geometric transformations to images. We then discuss affine and projective transformations and see how we can use them to apply cool geometric effects to photos. We will then cover techniques used for object recognition, 3D reconstruction, stereo imaging, and other computer vision applications. This book will also provide clear examples written in Python to build OpenCV applications. The book starts off with simple beginner’s level tasks such as basic processing and handling images, image mapping, and detecting images. It also covers popular OpenCV libraries with the help of examples. The book is a practical tutorial that covers various examples at different levels, teaching you about the different functions of OpenCV and their actual implementation.
Table of Contents (14 chapters)
close
close
13
Index

Installing OpenCV-Python

Let's see how to install OpenCV with Python support on multiple platforms.

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 any error. 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.

Mac OS X

To install OpenCV-Python, we will be using Homebrew. Homebrew is a great package manager for Mac OS X and it will come in handy when you are installing various libraries and utilities on OS X. If you don't have Homebrew, you can install it by running the following command on 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 pip 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 package NumPy 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/2.4.9/. 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:

$ cd /Library/Python/2.7/site-packages/
$ ln -s /usr/local/Cellar/opencv/2.4.9/lib/python2.7/site-packages/cv.py cv.py
$ ln -s /usr/local/Cellar/opencv/2.4.9/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 message. You are now ready to use OpenCV in Python.

Linux (for Ubuntu)

Before we start, we need to install some dependencies. Let's install them using the package manager as shown below:

$ sudo apt-get -y install libopencv-dev build-essential cmake libdc1394-22 libdc1394-22-dev libjpeg-dev libpng12-dev libtiff4-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev libtbb-dev libqt4-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils python-scipy python-pip python-virtualenv

Now that you have installed the necessary packages, let's go ahead and build OpenCV with Python support:

$ wget "https://github.com/Itseez/opencv/archive/2.4.9.tar.gz" -O ./opencv/opencv.tar.gz
$ cd opencv
$ tar xvzf opencv.tar.gz -C .
$ mkdir release
$ cd release
$ sudo apt-get –y install cmake
$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -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_V4L=ON ../ 
$ make –j4
$ sudo make install

Let's make sure that it's installed correctly. Open up the Python shell and type the following:

>>> import cv2

If you don't see any errors, you are good to go.

If you have any other Linux distribution, please refer to the OpenCV downloads page (http://opencv.org/downloads.html) for installation details.

Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
OpenCV with Python By Example
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon