OpenCV

OpenCV is a popular library in the computer vision community, being actively used in industry and academy. Started in 1999 and popularized in the following decade, OpenCV is covered in books and tutorials, so this text will not provide another overview of the library. The interested reader that is not familiar to OpenCV is referred to those cited texts and the library’s official documentation.

Developed in efficient C/C++ code, OpenCV presents a stable Python interface since 2009. The functions’ prototypes in the Python API can differ from the C++ version, but the OpenCV documentation presents both versions for reference. IPython code completion capabilities can also help programmers used to the C++ API to quickly identify the proper Python prototypes.

Interactive OpenCV using IPython

In [1]:
import cv2

Let's play with interactive frame grabbing from a video camera:

In [2]:
capture = cv2.VideoCapture(1)
In [3]:
val, image = capture.read()
In [4]:
val
Out[4]:
True
In [5]:
imshow(image)
Out[5]:
<matplotlib.image.AxesImage at 0x7fa557d9f490>
In [6]:
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
imshow(image_rgb)
Out[6]:
<matplotlib.image.AxesImage at 0x7fa54c352090>

What is an image in OpenCV under Python?

In [7]:
type(image)
Out[7]:
numpy.ndarray
  • In C++, OpenCV employs its Mat matrix structure
  • But in Python, OpenCV represents images as NumPy $n$-dimensional arrays
  • Let's talk about NumPy arrays...

In OpenCV operation under Python, NumPy arrays replace the OpenCV’s Mat type as the data structure for images. NumPy arrays will be presented in the next section.