Вы находитесь на странице: 1из 16

Lections on Image analysis, OpenCV

3. OpenCV basics for C++: 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).

2. Image processing apps - 32-bit


(OpenCV, Photoshop, 3D-graphics).

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.

1) Values may be integer


(Usually 8-bitThe values 0 .. 255).

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.

2) Values may be a floating point number (Usually 32-bit float).

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

- The pixel values are the vector.

Also, values can be integer or floating point number.

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.

Also, CMYK format is used in the polygraphic industry.


4. Multi-channel image in OpenCV
OpenCV is open from 1, 2, 3, 4-channel images.

1-channel - Halftone.

2-channel - Used to predstavlyaniya vector fields (such as optical flow).

Warning: they will not be displayed on the screen display by conventional means such as
imshow.

3-channel - Color.

4-channel - Provide an additional channel of Alpha, which is usually used to define


transparency.

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:

1) Let the picture without some type of

Mat imageEmpty;

2) Image wxh pixels, the values 0 .. 255 (8U = unsigned 8 bit), grayscale (C1,
ie, one channel)

int w = 150; int h = 100;


Mat imageGray (h, w, CV_8UC1);
// Note that w and h are interchanged

3) 1-channel with the floating-point values (32F = float 32 bit)

Mat imageFloat (h, w, CV_32FC1);

4) 3-channel image with values 0 .. 255 for each channel

Mat imageRGB (h, w, CV_8UC3);


2. Conversion types
Note:
In the derivation of the on-screen images with floating-point OpenCV means we must
bear in mind that they are displayed on the assumption that their values lie in [0,1].
Therefore, when converting 8-bit images in an image float to do the transformation -
the multiplication by 1.0 / 255.0.

Used to convert a class member convertTo.


In its second argument - the type of the image.

imageGray.convertTo (imageFloat, CV_32FC1, 1.0 / 255.0);

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:

imageRGB.convertTo (imageEmpty, CV_32FC3, 1.0 / 255.0);

(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 + +:

1. Memory for the image stands out and is automatically cleared.

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);

- Here OpenCV itself allocate imageFloat.


It is important that if your image is already the right size, there are no operations on
memory allocation is performed.

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).

Although, of course, const Mat & will be transmitted still faster.

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:

Rect rect (0, 0, 100, 100); // Create a rectangle x, y, w, h:

Mat subimage = image (rect);


// Cut out part of the image, while the data is not copied,
// And also increases the reference count

// If you want to copy data:


image (rect). copyTo (subimage);
5. Per-pixel access
to images
In OpenCV has several ways of per-pixel access to images.
They vary in the degree of security (typing and go beyond the border), the speed and
convenience.

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;

Note that x and y in the call are swapped.


6. About displaying images
to screen
Function imshow displays the image in a separate window screen

imshow ("image", image);


// Displays the image in the window with the title «image»

This feature is extremely useful for debugging and tuning of algorithms by


showing the intermediate images during processing.

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.

Вам также может понравиться