The cv::Mat
data structure is essentially made up of two parts: a header and a data block. The header contains all the information associated with the matrix (size, number of channels, data type, and so on). The previous recipe showed you how to access some of the attributes of this structure contained in its header (for example, by using cols
, rows
, or channels
). The data block holds all the pixel values of an image. The header contains a pointer variable that points to this data block; it is the data
attribute. An important property of the cv::Mat
data structure is the fact that the memory block is only copied when explicitly requested for. Indeed, most operations will simply copy the cv::Mat
header such that multiple objects will point to the same data block at the same time. This memory management model makes your applications more efficient while avoiding memory leaks, but its consequences have to be understood. The examples of this recipe illustrate this fact.
By default, the cv::Mat
objects have a zero size when they are created, but you can also specify an initial size as follows:
In this case, you also need to specify the type of each matrix element; CV_8U
here, which corresponds to 1-byte pixel images. The letter U
means it is unsigned. You can also declare signed numbers by using the letter S
. For a color image, you would specify three channels (CV_8UC3
). You can also declare integers (signed or unsigned) of size 16 and 32 (for example, CV_16SC3
). You also have access to 32-bit and 64-bit floating-point numbers (for example, CV_32F
).
Each element of an image (or a matrix) can be composed of more than one value (for example, the three channels of a color image); therefore, OpenCV has introduced a simple data structure that is used when pixel values are passed to functions. It is the cv::Scalar
structure, which is generally used to hold one value or three values. For example, to create a color image initialized with red pixels, you will write the following code:
Similarly, the initialization of the gray-level image could also have been done using this structure by writing cv::Scalar(100)
.
The image size also often needs to be passed to functions. We have already mentioned that the cols
and rows
attributes can be used to get the dimensions of a cv::Mat
instance. The size information can also be provided through the cv::Size
structure that simply contains the height and width of the matrix. The size()
method allows you to obtain the current matrix size. This is the format that is used in many methods where a matrix size must be specified.
For example, an image could be created as follows:
The data block of an image can always be allocated or re-allocated using the create
method. When an image has been previously allocated, its old content is de-allocated first. For reasons of efficiency, if the new proposed size and type matches the already existing size and type, then no new memory allocation is performed:
When no more references point to a given cv::Mat
object, the allocated memory is automatically released. This is very convenient because it avoids the common memory leak problems often associated with dynamic memory allocation in C++. This is a key mechanism in OpenCV 2 that is accomplished by having the cv::Mat
class implement reference counting and shallow copy. Therefore, when an image is assigned to another one, the image data (that is, the pixels) is not copied; both the images will point to the same memory block. This also applies to images passed by value or returned by value. A reference count is kept such that the memory will be released only when all the references to the image will be destructed or assigned to another image:
Any transformation applied to one of the preceding images will also affect the other images. If you wish to create a deep copy of the content of an image, use the copyTo
method. In that case, the create
method is called on the destination image. Another method that produces a copy of an image is the clone
method, which creates a new identical image as follows:
If you need to copy an image into another image that does not necessarily have the same data type, you have to use the convertTo
method:
In this example, the source image is copied into a floating-point image. The method includes two optional parameters: a scaling factor and an offset. Note that both the images must, however, have the same number of channels.
The allocation model for the cv::Mat
objects also allows you to safely write functions (or class methods) that return an image:
We can also call this function from our main
function as follows:
If we do this, then the gray
variable will now hold the image created by the function without extra memory allocation. Indeed, as we explained, only a shallow copy of the image will be transferred from the returned cv::Mat
instance to the gray
image. When the ima
local variable goes out of scope, this variable is de-allocated, but since the associated reference counter indicates that its internal image data is being referred to by another instance (that is, the gray
variable), its memory block is not released.
It's worth noting that in the case of classes, you should be careful and not return image class attributes. Here is an example of an error-prone implementation:
Here, if a function calls the method of this class, it obtains a shallow copy of the image attributes. If later this copy is modified, the class
attribute will also be surreptitiously modified, which can affect the subsequent behavior of the class (and vice versa). To avoid these kinds of errors, you should instead return a clone of the attribute.