Qt World Summit: Register Today!

Superposition of two images

  • Dear all,

    I'm facing a problem in my soft, I'm superposing two image coming from a video stream :

                *leftImg = im.copy(0, 0, 512/2, 512);
                *rightImg = im.copy( 256, 0, 512/2,  512);
                QPainter painter(dualViewImg);
                painter.drawImage(0, 0, *leftImg);
                painter.drawImage(0, 0, *rightImg);
                display->setPixmap(QPixmap::fromImage(dualViewImg->scaled(Xmaxi/2, Ymaxi)));

    The superposition is working, but I would like to assign a spécific color to each image in order to make the comparison of the images easie. to do that I assign a red or green colortable to each image :

    QVector<QRgb> redTable (256);
        QVector<QRgb> greenTable (256);
        for (int i = 0 ; i < 255; i++)
            redTable[i] = qRgb(i, 0, 0);
            greenTable[i] = qRgb(0, i, 0);

    but it's not working the resulting image is still gray. What did I miss?
    thanks in advance for your help.

  • Hi,

    Not sure if it's important, but your for loop will assign 255 values instead of 256.

    QVector<QRgb> redTable (256);
    QVector<QRgb> greenTable (256);
    for (int i = 0 ; i < 256; i++)
        redTable[i] = qRgb(i, 0, 0);
        greenTable[i] = qRgb(0, i, 0);

    That being said, I don't really understand what you are trying to achieve here.

  • Sorry I didnot explain very well my problem. But it can be summed up to :
    How do I transform a monochromatic QImage into a colored one?

  • Lifetime Qt Champion

    @Zhitoune said:


    Are you sure it uses a color table?
    Before you do ->setColorTable, what does
    For leftImg

  • It returns 0. I have check my images format is RGB16.
    there is always the options to rewritte a new image like this :

    for (int i = 0 ; i <resX ; i++)
         for (int j = 0 ; j <resY ; j++)
             newQImage.setPixel(i, j, qRgb (leftImg.pixel(i, j).red(), 0, 0)
    but I fear it to be too much time consuming..

  • Lifetime Qt Champion

    Well it seems not to use palette for that format.
    So only option is to rewrite pixels as far I can see.
    At least all samples do that.

  • I know that this post is pretty old but I think I found what is wrong! setColorTable only works for monochrome and 8-bit formats!! So the picture has to be transformed first :) I personally used
    image->convertToFormat(QImage::Format_Indexed8,redTable) and It worked!!

Log in to reply