Important: Please read the Qt Code of Conduct -

Qt5 C++ - How to split color image efficiently into 3 channels without using OpenCV

  • I have successfully extract the 3 channels from a color image by looping each of the pixel. However, this is way too slow and would like to ask for other more efficient and faster way to do it?

    Below are my inefficient way of doing it:

    for (int y = 0; y < HEIGHT; ++y)
    int x_stride = y * WIDTH;
    for (int x = 0; x < WIDTH; ++x)
    int index = x_stride + x;
    QRgb rgb_1 = snap_buf_1.pixel(x, y);

        img_bit_red = qRed(rgb_1);
        img_bit_green = qGreen(rgb_1);
        img_bit_blue = qBlue(rgb_1);
        ucSnapBuffer1[0].GetBuffer_UNSAFE().data()[index] = img_bit_red;
        ucSnapBuffer1[1].GetBuffer_UNSAFE().data()[index] = img_bit_green;
        ucSnapBuffer1[2].GetBuffer_UNSAFE().data()[index] = img_bit_blue;


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What data structure do you need to fill with these data ?

  • Hi,

    I would like to fill into an array of unsigned char.

  • Lifetime Qt Champion

    Then why not use memcpy ?

  • Did not use memcpy because I need to take the correct color bit from the color image. I need to split all the 3 channels into 3 mono buffer. Can I use memcpy to achieve this? Sorry if I'm wrong as I'm not expert in image processing and Qt.

  • Lifetime Qt Champion

    No indeed since there's the split. But you can use pointer arithmetic to move around the data more efficiently.

  • op makes no mention of the format of the image data. That will dictate in large part how efficient the process can be made. The most efficient to do what he wants would be if the image is already in planar format, where the memory is a seriies of color planes. memory backed video hardware is often in planar format. If not in planar format then any real optimization will still involve addressing the image directly and bypassing the Qt pixel addressing operations, then using any available processor native multimedia instructions. I hope the op is comfortable with pointers. LOL

    At the very least, op should quit using array indexes and instead use pointer math to access array elements, as it is faster than array indexing.

Log in to reply