Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Hi Guys, after doing further research, i came to the conclusion that we can show transparent image on top of a non transparent image by using masking.

Here is the code

#include <stdio.h>
#include <iostream>

#include "opencv2/opencv.hpp"

using namespace cv;

int main (int argc, char** argv) {

    if(argc != 3)
        return -1;

    Mat frame = imread(argv[1],-1);

    Mat srcImage = imread(argv[2],-1);

    if(!srcImage.data)
        return -1;

    if(!frame.data)
        return -1;

    Mat mask;
    vector<Mat> rgbLayer;
    split(srcImage,rgbLayer);



    if(srcImage.channels() == 4)
    {
        split(srcImage,rgbLayer);         // seperate channels
        Mat cs[3] = { rgbLayer[0],rgbLayer[1],rgbLayer[2] };
        merge(cs,3,srcImage);  // glue together again
        mask = rgbLayer[3];       // png's alpha channel used as mask
    }

    srcImage.copyTo(frame(cv::Rect(0,0,srcImage.cols, srcImage.rows)),mask);


    std::cout<<frame.channels()<<std::endl;

    namedWindow("output",1);

    imshow("output",frame);
    waitKey(0);
    return 0;
}

and here is the result image description

Here in this image, the smily face is non transparent image and the goggle is a transparent image.