Convert into CByteImage

  • Nobody/Anonymous


    I'm trying to convert a OpenCV IplImage or a QT QImage into a CByteImage but all I get is a gray image. This is how I do it for an QImage but it is a smiliar way for a IplImage:

    QImage *tempQImage = new QImage("1.PNG");
    CByteImage *destCBImage = new CByteImage(tempQImage.width(), tempQImage.height(), CByteImage::eRGB24, true);
    destCBImage->pixels = (unsigned char *) tempQImage.bits();

    The QImage is in QImage format RGB888 (RGB 24bit).

    Can anybody help me.

    • Pedram Azad

      Pedram Azad - 2009-04-16


      your code looks fine. Could you send me your test image 1.PNG, so I can check (you can find my e-mail-address on the contact page on\)?


    • Pedram Azad

      Pedram Azad - 2009-04-18


      the problem is that Qt doesn't represent the image as 24 bit RGB but as 32 bit RGBA. 

      Below you find code for both Qt and OpenCV (assuming that 1.PNG is a color image). I tested both with the image you sent me and it works fine. 

      Note that you can also use the function IplImageAdaptor::Adapt from IVT/src/Image/IplImageAdaptor.h in the case of OpenCV. 


      #include "Image/ByteImage.h"
      #include <qimage.h>
      #include <highgui.h>

      int main()
              QImage qtImage("1.PNG");
              CByteImage image(qtImage.width(), qtImage.height(), CByteImage::eRGB24);

              // convert from 32 bit RGBA to 23 bit RGB
              const unsigned char *input = (unsigned char *) qtImage.bits();
              const int nOutputBytes = 3 * qtImage.width() * qtImage.height();
              for (int i = 0, offset = 0; i < nOutputBytes; i += 3, offset += 4)
                  image.pixels[i] =  input[offset];
                  image.pixels[i + 1] = input[offset + 1];
                  image.pixels[i + 2] = input[offset + 2];


              IplImage *pIPLImage = cvLoadImage("1.PNG");
              CByteImage image(pIPLImage->width, pIPLImage->height, CByteImage::eRGB24, true);

              image.pixels = (unsigned char *) pIPLImage->imageData;


          return 0;

    • Nobody/Anonymous

      It works fine. Thank you very much for your help Pedram!



Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks