Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Merge Two Images



  • Hi,
    my code generate two quadaratic grayscale images. The background is white. In the first image an object is in the center, in the second picture the object is shifted horizontally, as you can see in the figures below.

    First Image Second Image
    1_1561787748362_i2.jpg 0_1561787748361_i1.jpg

    I want to merge these images, so that the second image lies over the first. But with the result that you still see the object from the first image as shown below.

    Merged Image:

    2_1561787748362_i3.jpg

    Unfortunately, I'm not able to generate the merged image. So I have to merge the two images.

    I tried the following:

    QImage image1; // converted from the grayscale image to ARGB32 format
    QImage image2; // converted from the grayscale image to ARGB32 format
    
    QPainter painter(&image1);
    painter.setCompositionMode(); // I tried a few modes but without expected result
    painter.drawImage(image1.rect(), image2);
    

    Any advice?


  • Lifetime Qt Champion

    Hi,

    Since you are generating these image, can you generate a translucent background rather than a white one ? That would simplify things to merge the two images.



  • @SGaist said in Merge Two Images:

    Since you are generating these image, can you generate a translucent background rather than a white one ?

    By now: no!

    The images are generated with a convolution of a target image and a impulse response function (point spread function, psf). Which is the multiplication of the frequencies in the frequency domain of the two images. To calculate the frequency domain with a FFT the target and the psf are both grayscale (PGM) images.

    Maybe there is a way to calcuate the FFT of an image with a transparent background, I don't know.


  • Lifetime Qt Champion

    Then maybe replace white with the alpha channel - e.g. with QImage::createMaskFromColor()



  • Thanks for the help.

    I solved my problem by converting the gray levels into alpha values.

    double opacity = 0.5;
    int alpha = opacity * (255 - grayValue(x, y));
    
    QImage converted;
    converted.setPixelColor(x, y, QColor(0, 0, 0, alpha)
    

    So the whiter the pixel the lower the alpha value. With this converted image I'm able to draw it over the other with

    QPainter painter(&image1);
    painter.drawImage(image1.rect(), converted);
    

Log in to reply