Want transparent image even after blending
Asked Answered
A

1

2

I am trying to blend two images as shown here.

This is my whole code

#include <cv.h>
#include <highgui.h>
#include <iostream>

using namespace cv;

int main( int argc, char** argv )
{
 double beta; double input;

 Mat src1, src2, dst;

 /// Ask the user enter alpha
 std::cout<<" Simple Linear Blender "<<std::endl;
 std::cout<<"-----------------------"<<std::endl;

 src1 = imread("face.jpg");
 src2 = imread("necklace1.png");

 if( !src1.data ) { printf("Error loading src1 \n"); return -1; }
 if( !src2.data ) { printf("Error loading src2 \n"); return -1; }

 double alpha = 0.1; // something
 int min_x = ( (src1.cols - src2.cols)/2 );
 int min_y = ( (src1.rows - src2.rows)/2 );
 int width, height;

 // min_x, min_y should be valid in src1 and [width height] = size(src2)
 cv::Rect roi = cv::Rect(min_x, min_y, src2.cols, src2.rows);  

 // "out_image" is the output ; i.e. src1 with a part of it blended with  src2
 cv::Mat out_image = src1.clone();

 // Set the ROIs for the selected sections of src1 and out_image (the same at the moment)
 cv::Mat src1_roi= src1(roi);
 cv::Mat out_image_roi = out_image(roi);

 // Blend the ROI of src1 with src2 into the ROI of out_image
 cv::addWeighted(src1_roi,alpha,src2,1-alpha,0.0,out_image_roi);

 imshow( "Linear Blend", out_image );

 waitKey(0);
 return 0;
}

My input images are enter image description here enter image description here

But the output got is

enter image description here

I want this necklace as transparent as before. How to do it?

I tried cvtColor( out_image_roi,out_image_roi, CV_BGR2BGRA );before cv::addWeighted(src1_roi,alpha,src2,1-alpha,0.0,out_image_roi);

But then the output is only the face image.

Averett answered 9/9, 2015 at 11:58 Comment(4)
Is the input image a 3 channel image? Maybe you just have to convert it to have 4 channels before mixing. (e.g. after cloning): cv::Mat converted_out_img = cv::Mat(out_img,rows,cols,CV_8UC4);Lucillalucille
@Lucillalucille If we do cv::Mat converted_out_img = cv::Mat(out_img,rows,cols,CV_8UC4); converted_out_img will be new blank image right?Averett
I'm sorry, you should do it this way: cv::Mat conv_img = cv::Mat(img.rows,img.cols,CV_8UC4). This image is blank. Next you define how to fill it. int from_to[] = {0,0,1,1,2,2,3,3}; And fill it: cv::mixChannels(&img,2,&conv_img,1,from_to,4);Lucillalucille
@Lucillalucille It is not working. Please try to insert your logic into my code and try.. I think now converted image and src1 have pointers to different address. So blending will not workAverett
A
6

You can blend only pixels that have alpha channel greater than 0, with a custom loop. I think that the code is more clear than an explanation:

#include <opencv2\opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
    Mat3b bkg = imread("path_to_girl_image");
    Mat4b fgd = imread("path_to_necklace_image", IMREAD_UNCHANGED);

    int x = 150;
    int y = 500;
    double alpha = 0.5; // alpha in [0,1]

    Mat3b roi = bkg(Rect(x, y, fgd.cols, fgd.rows));

    for (int r = 0; r < roi.rows; ++r)
    {
        for (int c = 0; c < roi.cols; ++c)
        {
            const Vec4b& vf = fgd(r,c);
            if (vf[3] > 0) // alpha channel > 0
            {
                // Blending
                Vec3b& vb = roi(r,c);
                vb[0] = alpha * vf[0] + (1 - alpha) * vb[0];
                vb[1] = alpha * vf[1] + (1 - alpha) * vb[1];
                vb[2] = alpha * vf[2] + (1 - alpha) * vb[2];
            }
        }
    }

    imshow("Girl with necklace", bkg);
    waitKey();

    return 0;
}

Result:

enter image description here

Aurore answered 9/9, 2015 at 13:36 Comment(3)
Nice moustache ;-)Alpinist
If you move the definition of alpha to the innermost block as double alpha = vf[3] / 255.;, the blending will respect partial transparency, instead of treating each pixel as completely on or off. (You can further multiply alpha by .5 to fade the image if desired)Selfdenial
See #46104231 if you need to overlay over white.Feudality

© 2022 - 2024 — McMap. All rights reserved.