Open CV: First Look! First App!

This is the first introduction and code-along tutorial to learn OpenCV in Python. We will be looking at installation and super basic functions of OpenCV.

OpenCV History & Facts

Short For Open Source Computer vision, is a programming library for Computer Vision functions. It was an Intel initiative that was officially launched in 1999. OpenCV was initiated to fulfill research needs for computer vision applications. A lot of the research was written in C++ which is what OpenCV is also written in. Later on, wrappers for all different languages like Java, Python, IOS, and Android were introduced with their individual documentation that can be found on OpenCV website. However, if you are looking for speed, it will be slower if you use a language other than C++.

First major release v1.0 was in 2006, second v2.0 in 2009, and the third v3.0 was in 2015. The latest stable release for OpenCV 2.x is 2.4.13. OpenCV 3.x is very similar and has new functions, however, important algorithms such as SIFT and SURF were removed due to some patents.

In this blog, I will be using Python 2 and OpenCV 2.4.13 just because it is easier to grasp concepts using python and to also use NumPy library. NumPy gives supporting functions for large multidimensional arrays in python. In addition, we will be looking at Anaconda python package which comes with huge bundles of Python libraries that include Jupyter and NumPy.

OpenCV Installation and Setup For Windows

STEP 1 — Download and Install Anaconda Python Package

  • Visit to https://www.continuum.io/downloads.
  • Download Windows Python 2.7 version.
  • Install with defaults.
  • Test if installation went well by typing into a command line jupyter notebook. This should open your default browser to localhost.

STEP 2 — Download and Run OpenCV

  • Visit http://opencv.org/downloads.html.
  • Download OpenCV 2.4.13
  • Run .exe file
  • Locate cv2.py file in the directory where OpenCV was installed. Path to the file is *\opencv\build\python\2.7\x64
  • Copy cv2.py and place it in *\Anaconda2\Lib\site-packages where * is the directory where you installed Anaconda package.

STEP 3 — TEST

  • Create a folder to store your code at.
  • Open command line Window in that folder.
  • Create a new notebook. By going to file → new notebook → python 2
  • Copy & Run the following code. Shortcut to run is ctl + Enter
import cv2
import numpy
import matplotlib
print(“Packages successfully imported”)

If everything went well, your screen should look like this:

OpenCV Installation and Setup For Mac

STEP 1 — Install Xcode

STEP 2 — Install Homebrew

  • Open a terminal
  • Copy & Run the following cd ~ then ruby -e "$(curl -fsSl https://raw.githubusercontent.com/Homebrew/install/master/install)"
  • Update Homebrew by running brew install python in the same terminal window

STEP 3 — Install the Anaconda Python Package

  • Visit https://www.continuum.io/downloads
  • Select the OSX installer
  • Install with defaults
  • Test if installation went well by typing into a command line jupyter notebook. This should open your default browser to localhost.

STEP 3 — Install OpenCV

  • Open a terminal
  • Copy & run the following lines
brew install opencv
brew tap homebrew/science
  • Create aliases of cv2.py & cv2.so files into Anaconda site-packages folder by copying the following commands in order

1.cd /Library/Python/2.7/site-packages/

2.ln -s /usr/local/Cellar/opencv/2.4.13.2/lib/python2.7/site-packages/cv.py cv.py

3.ln -s /usr/local/Cellar/opencv/2.4.13.2/lib/python2.7/site-packages/cv2.so cv2.so

Note: Above bolded part of the directory can be different depending on the version of OpenCV 2.x version. Check the path in finder to be on the safe side.

STEP 3 — TEST

  • Create a folder to store your code at.
  • Open command line Window in that folder.
  • Create a new notebook. By going to file → new notebook → python 2
  • Copy & Run the following code. Shortcut to run is ctl + Enter
import cv2
import numpy
import matplotlib
print(“Packages successfully imported”)

If everything went well, your screen should look like this:


Reading, Writing, and Displaying Images

# First we have to make OpenCV availble
import cv2
# READING
# Load an image using 'imread' into a variable
# Make sure that 'input.jpg' exists at declared path
input = cv2.imread('input.jpg')
# DISPLAYING
# To display out image variable, we use 'imshow'
# 1st parameter: title on image window
# 2nd parameter: image varible
cv2.imshow('Hello World', input)
# waitKey acts as a blocking statement until you want
# the application to move on to the next lines of code.
# It waits until any key is pressed before continuing.
# By placing numbers (except 0), we can specify a delay
# in milliseconds instead of a key press.
cv2.waitKey()
# DESTROYING WINDOWS
# destroyAllWindows closes all open windows
cv2.destroyAllWindows()
# FOR MAC ONLY
# You have to include waitKey() again after destroyAllWindows()
# to avoid hanging window.
cv2.waitKey()
# WRITING
# Simply use 'imwrite' to save image to disk
# 1st parameter: Directory + file name
# 2nd parameter: Image variable to be saved
cv2.imwrite('output.jpg', input)

How Images are Stored in OpenCV

Dimensions

By printing input.shape where input is an image variable, you get an array of three elements.
At index [0] is the height.
At index [1] is the width.
At index [2] tells you how many components make up this image. In the example below, it 3 which corresponds to RGB color space.

You could also get attributes by specifying the index.

Color Spaces

Now we know that images are saved in RGB color spaces. Now, first thing to remember is in OpenCV, RGB are saved as BGR (reversed) sequence.
Next, you can target a specific pixel by providing the X & Y axis of the pixel. input[0, 0] (where input is an image variable) returns BGR values of x: 0 & y:0 pixel.

Or you could look at individual color levels like the following example.

Now, what if you want to convert an image to another color space. In the code below, we are converting an BGR image to gray scale.

# cvtColor is function used to convert an image's color space
# 1st parameter: image variable
# 2nd parameter: output image color space
gray_img = cv2.cvtColor(input, cv2.COLOR_BGR2GRAY)

Gray scaling changes how the image is stored. We can see in the example below that the dimensions are now only a two-dimensionsal array instead of 3. In addition, now every cell only holds one value ranging (0 to 255) instead of three values (RGB). Gray scaling makes images smaller and faster to process.


Looking at RGB Channels Individually

#Get in the habit of always importing OpenCV and numPY
import cv2
import numpy as np
#Reading input image
input = cv2.imread('input.jpg')
# OpenCV's 'split' function splits the whole
# image into each color index.
B, G, R = cv2.split(input)
# Displaying every channel alone
# Displays gray images because each pixel has only 1 value
# while colorful images (BGR) have 3 values
cv2.imshow("Red Channel", R)
cv2.imshow("Green Channel", G)
cv2.imshow("Blue Channel", B)
cv2.waitKey()
cv2.destroyAllWindows()
# Let's re-make the original image by merging all channels again
merged = cv2.merge([B, G, R])
cv2.imshow("Merged", merged)
# Let's amplify the blue color in the original picture
merged = cv2.merge([B+100, G, R])
cv2.imshow("Merged with Blue Amplified", merged)
cv2.waitKey()
cv2.destroyAllWindows()
# ------------------------
# Lastly, let's get each channel alone without having it turn gray.
# We'd have to eventually give each pixel a value of zero
# in non-selected channel and leave only selected channel.
# This is where numPy comes in play
# Let's create a matrix of (int) zeros with dimensions of
# the image h x w .
zeros = np.zeros(image.shape[:2], dtype = "uint8")
# Now we can show each channel in BGR instead of grayscale
cv2.imshow("Red", cv2.merge([zeros, zeros, R]))
cv2.imshow("Green", cv2.merge([zeros, G, zeros]))
cv2.imshow("Blue", cv2.merge([B, zeros, zeros]))
cv2.waitKey()
cv2.destroyAllWindows()

Results

RGB channels with each pixel having one value (R or G or B)
Original with blue Amplified (left). Original photo after merge (right)
RGB Channels split with each pixel having three values with two levels set to zero.

What’s Next?

My third sequence of Computer Vision blogs will continue on exploring OpenCV functions. Stay tuned!