Image Blending Using OpenCV

Last Updated : 3 Feb, 2026

Blending two images is a common image processing technique similar to applying filters. Instead of blending a single color overlay, two separate images are combined into one. This approach is widely used for creative effects such as transparency blending, overlays, background replacement, and artistic compositions.

In OpenCV, image blending is performed using the addWeighted() function, which blends two images based on specified weights.

Working Principle of Image Blending

For proper merging, both images must have the same dimensions. If the background image differs in size, it must first be resized to match the foreground image before blending. The blended image is computed using:

Final Image = Foreground × alpha + Background × beta

Where:

  • alpha controls the contribution of the foreground image
  • beta controls the contribution of the background image

Typically:

alpha + beta = 1

This ensures a balanced blend between both images while preserving visibility.

Python Implementation

Below is a function that merges two images by resizing the background to match the foreground and blending them together.

Note: For this article, we will be using two sample images "shoes.png" and "bg.jpg".

Explanation:

  • def merge(foreground_path, background_path, alpha, beta): Defines a function to merge two images using blending weights.
  • img = cv.imread(foreground_path): Loads the foreground image (main image).
  • background = cv.imread(background_path): Loads the background image.
  • background = cv.resize(background, (img.shape[1], img.shape[0])): Resizes the background to match the size of the foreground image so both can be merged properly.
  • final = cv.addWeighted(img, alpha, background, beta, 0): Blends both images using the specified weights (alpha and beta).
  • cv.waitKey(0): Waits until a key is pressed before closing the window.
  • cv.destroyAllWindows(): Closes all OpenCV display windows.
  • merge('shoes.png', 'bg.jpg', 0.5, 0.5): Calls the function with equal blending weights, merging both images equally.
Python
import cv2 as cv

def merge(foreground_path, background_path, alpha, beta):

    img = cv.imread(foreground_path)
    background = cv.imread(background_path)

    background = cv.resize(background, (img.shape[1], img.shape[0]))

    final = cv.addWeighted(img, alpha, background, beta, 0)

    cv.imshow('Foreground Image', img)
    cv.imshow('Merged Image', final)
    cv.waitKey(0)
    cv.destroyAllWindows()

merge('shoes.png', 'bg.jpg', 0.5, 0.5)

Output:

originalImage
Original Image
MergedImage
Resulting Image

Merged Image is a combination of foreground and background.

Comment

Explore