Deinterleaving in Dlib

The Dlib library uses the unsigned char type for pixel color representation, and we can use floating-point types only for grayscaled images. The Dlib library stores pixels in the row-major order with interleaved channels and data is placed in memory continuously with a single block. There are no special functions in the Dlib library to manage image channels, so we cannot deinterleave them or mix them. However, we can use raw pixel data to manage color values manually. There are two functions in the Dlib library that can help us: the image_data() function to access raw pixel data, and the width_step() function to get the padding value.

The most straightforward approach to deinterleave the Dlib image object is using a loop over all pixels. In such a loop, we can split each pixel value into separate colors.

As a first step, we define containers for each of the channels, as follows:

auto channel_size = static_cast<size_t>(img.nc() * img.nr());
std::vector<unsigned char> ch1(channel_size);
std::vector<unsigned char> ch2(channel_size);
std::vector<unsigned char> ch3(channel_size);

Then, we read color values for each pixel with two nested loops over image rows and columns, like this:

size_t i{0};
for (long r = 0; r < img.nr(); ++r) {
for (long c = 0; c < img.nc(); ++c) {
ch1[i] = img[r][c].red;
ch2[i] = img[r][c].green;
ch3[i] = img[r][c].blue;
++i;
}
}

The result is three containers with color channel values, which we can use separately. They are suitable to initialize grayscaled images for use in the image processing routines. Alternatively, we can use them to initialize a matrix-type object that we can process with linear algebra routines.

..................Content has been hidden....................

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