Ask Your Question
-1

How to show transparent images

asked 2017-09-18 04:54:01 -0600

sushanta gravatar image

I am able to show a simple png image on a imshow image window. but a blue strip is coming.

I am using opencv2

here is the code

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

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;

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

    if(argc != 2)
        return -1;

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

    if(!srcImage.data)
        return -1;

    namedWindow("output",1);

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

I am also attaching an image to show my problem.

Please help me understand the issue.

Here is the loaded image image description

Here is the original image image description

edit retag flag offensive close merge delete

Comments

transparency has no use in computer-vision, so imshow() just drops the alpha channel.

you probably should avoid using images with alpha, they were made to look nice in a webrowser, but are quite problematic otherwise.

berak gravatar imageberak ( 2017-09-18 05:16:29 -0600 )edit
2

Thanks for the information, i thought there is problem with my code. After doing further research i am able to show a transparent image on top of a non transparent image. I will post it as an answer so it could help someone else.

sushanta gravatar imagesushanta ( 2017-09-19 08:43:01 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-09-25 06:40:18 -0600

sushanta gravatar image

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.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-09-18 04:54:01 -0600

Seen: 11,185 times

Last updated: Sep 25 '17