Deinterleaving in OpenCV

By default, when we load an image with the OpenCV library, it loads the image in the BGR format and with char as the underlying data type. So, we need to convert it to the RGB format, like this:

cv::cvtColor(img, img, cv::COLOR_BGR2RGB);

Then, we can convert the underlying data type to the float type, like this:

img.convertTo(img, CV_32FC3, 1/255.0);

Next, to deinterleave channels, we need to split them with the cv::split() function, like this:

cv::Mat bgr[3];
cv::split(img, bgr);

Then, we can place channels back to the cv::Mat object in the order we need with the cv::vconcat() function, which concatenates matrices vertically, as follows:

cv::Mat ordered_channels;
cv::vconcat(bgr[2], bgr[1], ordered_channels);
cv::vconcat(ordered_channels, bgr[0], ordered_channels);

There is a useful method in the cv::Mat type named isContinuous that allows us to check if the matrix's data is placed in memory with a single contiguous block. If that is true, we can copy this block of memory or pass it to the routines that work with plain C arrays.

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

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