I need to convert image, that was loaded with CImg library into format of image, which can be used in OpenCV.
The problem is that CImg creates uchar array, where data stored in the following way (in the case of 3-channel image):
- first there are pixels of Red Channel,
- then all pixels of Green channel follows,
- then – Blue channel.
It looks like this: R R R R R R …. G G G G G G … B B B B B B…
OpenCV stores data in a differnt way: B G R B G R B G R B G R…
Here is my code where I convert from CImg to IplImage:
CImg<uint8_t> src;
src.load_jpeg_buffer(srcData, size);
size_t width = src._width;
size_t height = src._height;
size_t nChannels = src._spectrum;
size_t depth = 8;
IplImage* m_image = cvCreateImage(cvSize(width, height), depth, nChannels);
for(size_t i = 0; i < height; i++)
{
for(size_t j = 0; j < width;j++)
{
for(size_t k = 0; k < nChannels; k++)
{
((m_image->imageData + i * m_image->widthStep))[j * nChannels + nChannels - 1 - k] =
src._data[k * src.size() / 3 + k + (i * m_image->widthStep + j * nChannels) / 3];
}
}
}
This code worked fine. Converted image of OpenCV format was the full copy of the original image.
I tested this code with valgrind. It said that it causes a lot of memory problems. I can’t find the cause of this memory problems.
I will be grateful, if you have any ideas on this matter!
Or may be you know another method, which can load image from buffer in OpenCV (not cvDecodeImage).
The problems weren’t in my code. As I found out OpenCV library functions cause memory problems. Examples of messages of valgrind are: