Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7964641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:56:22+00:00 2026-06-04T05:56:22+00:00

How can I overlay two images? Essentially I have a background with no alpha

  • 0

How can I overlay two images? Essentially I have a background with no alpha channel and than one or more images that have alpha channel that need to be overlaid on top of each other.

I have tried the following code but the overlay result is horrible:

// create our out image
Mat merged (info.width, info.height, CV_8UC4);

// get layers
Mat layer1Image = imread(layer1Path);
Mat layer2Image = imread(layer2Path);

addWeighted(layer1Image, 0.5, layer2Image, 0.5, 0.0, merged);

I also tried using merge but I read somewhere that it doesn’t support alpha channel?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T05:56:24+00:00Added an answer on June 4, 2026 at 5:56 am

    I don’t know about a OpenCV function that does this. But you could just implement it yourself. It is similar to the addWeighted function. But instead of a fixed weight of 0.5 the weights are computed from the alpha channel of the overlay image.

        Mat img = imread("bg.bmp");
        Mat dst(img);
        Mat ov = imread("ov.tiff", -1);
    
    
        for(int y=0;y<img.rows;y++)
        for(int x=0;x<img.cols;x++)
        {
                //int alpha = ov.at<Vec4b>(y,x)[3];
                int alpha = 256 * (x+y)/(img.rows+img.cols);
                dst.at<Vec3b>(y,x)[0] = (1-alpha/256.0) * img.at<Vec3b>(y,x)[0] + (alpha * ov.at<Vec3b>(y,x)[0] / 256);
                dst.at<Vec3b>(y,x)[1] = (1-alpha/256.0) * img.at<Vec3b>(y,x)[1] + (alpha * ov.at<Vec3b>(y,x)[1] / 256);
                dst.at<Vec3b>(y,x)[2] = (1-alpha/256.0) * img.at<Vec3b>(y,x)[2] + (alpha * ov.at<Vec3b>(y,x)[2] / 256);
        }
    
        imwrite("bg_ov.bmp",dst);
    

    Note that I was not able to read in a file with the alpha channel because apparently OpenCV does not support this. That’s why I computed an alpha value from the coordinates to get some kind of gradient.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got two images, say png and jpeg and I need to overlay
I have an array of images (can be up to 25) that need to
In a nutshell I have two images I want to overlay one over the
Here I have two divs with a css background images set for both. I'm
I have two bitmaps that differ only by color. The green one is used
I am creating a website and need two images to overlay on top of
I have two bitmap images. One contains a picture taken with a usb camera.
I have two images. One could be any size and aspect ratio. The other
I have two images they actually do not overlap but I also can't have
every master hand person,in android,the textView can transparent background display on the overlay windowmanage

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.