Home > Software design >  How to merge one RGBA and one RGB images in opencv
How to merge one RGBA and one RGB images in opencv

Time:01-05

I have two images. In one image, the white pixels are set to be transparent (or colorless) by editing the channel A in RGBA image format. Suppose this image name is image_rgba.png and the other image is image.jpg. Now, I want to put the image_rgba.png image in a specific location of the image.jpg using the following python code

import cv2
import numpy as np
import matplotlib.pyplot as plt

image = cv2.imread('image.jpg')
label = cv2.imread('image_rgba.png')
label = cv2.cvtColor(label, cv2.COLOR_BGR2RGBA)

new_image = image.copy()
new_image = cv2.cvtColor(new_image, cv2.COLOR_BGR2RGBA)

new_image[200:290, 300:384, :] = label

When I write this image, the edges of the image_rgba.png which is colorless, are set to be white in the output image (the white edges of the green box in the following image).

enter image description here

I want the white edges of the green box label does not to show and instead the background image shown in that pixels. I found the Background

And this foreground:

Foreground

Result as shown by the Python code:

Result

  •  Tags:  
  • Related