3
Getting to Know OpenCV
OpenCV Data Types
OpenCV has many data types, which are designed to make the representation and handling of important
concepts of computer vision relatively easy and intuitive. At the same time, many algorithm developers
require a set of relatively powerful primitives that can be generalized or extended for their particular needs.
OpenCV attempts to address both of these needs through the use of templates for fundamental data types,
and specializations of those templates that make everyday operations easier.
From an organizational perspective, it is convenient to divide the data types into three major categories.
First, the basic data types are those that are assembled directly from C++ primitives (int, float, etc.).
These types include simple vectors and matrices, as well as representations of simple geometric concepts
like points, rectangles, sizes, and the like. The second category contains helper objects. These objects
represent more abstract concepts such as the garbage collecting pointer class, range objects used for slicing,
and abstractions such as termination criteria. The third category is what might be called large array types.
These are objects whose fundamental purpose is to contain arrays or other assemblies of primitives or,
more often, the basic data types mentioned first. The star example of this latter group is the cv::Mat
class, which is used to represent arbitrary-dimensional arrays containing arbitrary basic elements. Objects
such as images are specialized uses of the cv::Mat class but, unlike in earlier versions of OpenCV (i.e.,
before version 2.1), such specific use does not require a different class or type. In addition to cv::Mat,
this category contains related objects such as the sparse matrix cv::SparseMat class, which is more
naturally suited to non-dense data such as histograms.
In addition to these types, OpenCV also makes heavy use of the Standard Template Library (STL) [STL].
This vector class is particularly relied on by OpenCV, and many OpenCV library functions now have
vector template objects in their argument lists. We will not cover STL in this book, other than as necessary
to explain relevant functionality. If you are already comfortable with STL, many of the template
mechanisms used “under the hood” in OpenCV will be familiar to you.
Overview of the Basic Types
The most straightforward of the basic data types is the template class cv::Vec<>. cv::Vec<> is a
container class for primitives,
1
which we will refer to as “the fixed vector classes.” Why not just use
1
Actually, this is an oversimplification that we will clear up a little later in the chapter. In fact, cv::Vec<> is a vector
container for anything, and uses templating to create this functionality. As a result, cv::Vec<> can contain other

Get Learning OpenCV, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.