OpenCV Lections: 3. Mat Class
OpenCV Lections: 3. Mat Class
http://blog.iseesystems.com/wp-content/uploads/2009/07/cube-matrix.jpg
Kinds of images
in computer
1. The life cycle of images
1. Input - 8bit
(Video / photo camera).
3. Output - 8 bits
(On the screen, printer, web, as bmp, jpg, png).
Why do so: the fact that for accurate computation interval of integers 0 .. 255 is too
small. But for displaying information on the screen it is quite enough.
2. The notion of grayscale images
Grayscale – when a pixel value is a scalar.
Such images are used to display images on the screen, as well as for data entry with
video and photo cameras.
In particular, jpg, png and bmp image formats stored in this form.
Such images are used graphics, programs, image processing and image analysis
algorithms for computing.
3. The concept of color images
"Colored" image
- Also it is called multichannel
Number of Channels:
often 3,
most often it is RGB = Red, Green, Blue.
3. The concept of color images
In addition to RGB, for computer vision is a valuable format YUV.
It contains a separate component of brightness, and two others are responsible for the
color.
To design and apply formats HUI, HSV - they separately represent brightness,
saturation, contrast.
1-channel - Halftone.
Warning: they will not be displayed on the screen display by conventional means such as
imshow.
3-channel - Color.
Please note: the current implementation OpenCV ignores the work of the channel Alpha.
In particular, when drawing images means OpenCV 4-channel images are not
transparent.
Creation and Image
Processing
in OpenCV C + +
1. The main type of data in OpenCV C + +
Mat - A class for storing images
Creating Images:
Mat imageEmpty;
2) Image wxh pixels, the values 0 .. 255 (8U = unsigned 8 bit), grayscale (C1,
ie, one channel)
Warning:
OpenCV can automatically change the type and size of the resulting image. In
particular, if the resulting izobarzhenie was empty after applying convertTo it will be
the right type and size. This remark is true of all the main functions of OpenCV:
(At this point there was a conversion of 3-channel 8-bit images in 3-channel floating-
point. Note that to use the command imageEmpty was empty.)
3. Copying images
and memory management
Base for understanding this is idelogiya memory handling in OpenCV C + +:
This means that OpenCV itself creates the image of the desired size and type, if
this image is an output parameter of a function:
Image imageFloat;
imageGray.convertTo (imageFloat, CV_32FC1, 1.0 / 255.0);
2. Assignment operator shall not copy the data (as does the std:: vector), and not
by copying pointers, and using mechanism of the reference counter.
3. Copying images
and memory management
The mechanism of the reference count (In STL is a shared_ptr, in Java it is used
for all pointers)
Works like this:
void fun () {
Mat A (100, 100, CV_8UC1);
// Allocate memory for the image at the same time remember that this // memory
using a single image.
{
Mat B = A;
// Here the memory for the image does not stand out, but just the data in the B
// point to the same area in memory. Therefore, if we change B, then // change, and
A. Reference count of images increased, was / is 2.
}
// Here B came out of scope, the reference count is decreased,
// And became equal to 1.
}
Then A went out of scope, the reference count becomes equal to 0,
and memory for the image is automatically cleared, as it has no one used ..
3. Copying images
and memory management
Therefore, to create a duplicate image for later use, apply explicit command
copyTo and clone:
image1.copyTo (image2);
image3 = image1.clone ();
Outcome:
1) an assignment Mat B = A; is very fast, and does not copy the data and sets up a
special way pointers. This allows you to transfer Mat as a function directly, without
pointers and references. This will not cause unwanted copying Mat in the stack (as
it would stalal std:: vector).
2) to copy the images to use explicit commands copyTo () and clone ().
4. Cut rectangular pieces
It is possible to effectively cut a rectangular portion of the image. At the same time as
there is no backup data, which makes this operation very effective and convenient to
carry out the changes and analysis of fragments of images.
Technically this is realized by storing the jump between adjacent rows of the image.
How it works:
Wherever possible, you should try to avoid direct references to the pixels, but instead
use the functions of OpenCV, since they usually work faster and the code more
understandable.
One way to access the pixels for images that have known the type -
the use of the at. For single-channel images 0 ... 255 it is:
// Get values
int value = imageGray.at <uchar> (y, x);
// Set the values
imageGray.at <uchar> (y, x) = 100;
At the same time, in the final product, these windows usually show not
worth it.
Notes:
1. This function does not show the 2-channel images.
2. Remember that when shown images of a float is assumed that the
channels of pixels lie in the interval [0,1]. Therefore, the values outside
this interval will be truncated.