Using Dlib 

Dlib is another popular library for image processing. This library has different types used for math routines and image processing. The library documentation recommends using the Dlib::array2d type for images. The Dlib::array2d type is a template type that has to be parametrized with a pixel type. Pixel types in the Dlib library are defined with pixel-type traits. There are the following predefined pixel types: rgb_pixel, bgr_pixel, rgb_alpha_pixel, hsi_pixel, lab_pixel, and any scalar type can be used for the grayscaled pixels' representation.

We can use the load_image() function to load an image from disk, as follows:

#include <Dlib/image_io.h>
#include <Dlib/image_transforms.h>
using namespace Dlib;
...
array2d<rgb_pixel> img;
load_image(img, file_path);

For a scaling operation, there is the Dlib::resize_image() function. This function has two different overloads. One takes a single scale factor and a reference to an image object. The second one takes an input image, an output image, the desired size, and an interpolation type. To specify the interpolation type in the Dlib library, we should call special functions: the interpolate_nearest_neighbor(), the interpolate_quadratic(), and the interpolate_bilinear() functions. The criteria for choosing one of them is the same as ones that we discussed in the Using OpenCV section. Notice that the output image for the resize_image() function should be already preallocated, as illustrated in the following code snippet:

array2d<rgb_pixel> img2(img.nr() / 2, img.nc() / 2);
resize_image(img, img2, interpolate_nearest_neighbor());
resize_image(1.5, img); // default interpolate_bilinear

To crop an image with Dlib, we can use the  Dlib::extract_image_chips() function. This function takes an original image, rectangle-defined bounds, and an output image. Also, there are overloads of this function that take an array of rectangle bounds and an array of output images, as follows:

extract_image_chip(img, rectangle(0, 0, img.nc() / 2, img.nr() / 2), img2);

The Dlib library supports image transformation operations through affine transformations. There is the Dlib::transform_image() function, which takes an input image, an output image, and an affine transformation object. An example of the transformation object could be an instance of the Dlib::point_transform_affine class, which defines the affine transformation with a rotation matrix and a translation vector. Also, the Dlib::transform_image() function can take an interpolation type as the last parameter, as illustrated in the following code snippet:

transform_image(img, img2, interpolate_bilinear(),
point_transform_affine(identity_matrix<double>(2),
Dlib::vector<double, 2>(-50, -50)));

In case we only need to do a rotation, Dlib has the Dlib::rotate_image() function. The Dlib::rotate_image() function takes an input image, an output image, a rotation angle in degrees, and an interpolation type, as follows:

rotate_image(img, img2, -45, interpolate_bilinear());

There is no complete analog of a function for adding borders to images in the Dlib library. There are two functions: the Dlib::assign_border_pixels() and the Dlib::zero_border_pixels() for filling image borders with specified values. Before using these routines, we should resize the image and place the content in the right position. The new image size should include borders' widths. We can use the Dlib::transform_image() function to move the image content into the right place. The following code sample shows how to add borders to an image: 

int top = 50;     // px
int bottom = 20; // px
int left = 150; // px
int right = 5; // px
img2.set_size(img.nr() + top + bottom, img.nc() + left + right);
transform_image(
img, img2, interpolate_bilinear(),
point_transform_affine(identity_matrix<double>(2),
Dlib::vector<double, 2>(-left/2, -top/2)));

For color space conversions, there exists the Dlib::assign_image() function in the Dlib library. This function uses color-type information from pixel-type traits we used for the image definition. So, to convert an image to another color space, we should define a new image with the desired type of pixels and pass it to this function. The following example shows how to convert the RGB image to a blue, green, red (BGR) one:

array2d<bgr_pixel> img_bgr;
assign_image(img_bgr, img);

To make a grayscale image, we can define an image with the unsigned char pixel type, as follows:

array2d<unsigned char> img_gray;
assign_image(img_gray, img);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.251.206