Removing Black Background and Make Transparent using OpenCV Python


In digital images, Transparency is the functionality that supports transparent areas in an image or image layer. For image processing/image editing, background removing is allowing us to highlight the subject of the photo and create a transparent background to place the subject into various new designs and destinations.

Certain image formats do not support transparency, for example, TIFF, PNG, and WebP graphics formats support transparency, whereas JPEGs have none.

In this article, we will see how to remove the black background from an image to make it transparent using OpenCV Python. Like RGB channels, the alpha channel is used to stores the transparency information.

We will follow the below steps to to remove the black background and make it transparent.

Approach

  • Load the image.

  • Create the alpha channels by specifying threshold values.

  • Split the RGB channels.

  • Merge the RGB and alpha channels.

  • And finally save the image using the combined channels.

The mainly used functions in this article are cv2.split() and cv2.merge() functions, which are used to split and merge the channels.

The cv2.split() function

Python OpenCV module provides a function cv2.split() to split a multi-channel/colored array into separate single-channel arrays. It will return an array with the three channels, each of which corresponds to blue, green, and red channels represented as a ndarray with two dimensions. Following is the syntax of this function –

cv2.split(m[, mv])

Parameters

  • src: input multi-channel array.

  • mv: output array or vector of arrays.

The cv2.merge() function

The cv2.merge() function takes single-channel arrays and combines them to make a multi-channel array/image. This function returns an array of the concatenation of the elements of the input arrays. Following is the syntax of this function –

cv2.merge(mv[, dst])

Parameters

  • mv: input vector of matrices to be merged. all the matrices must have the same size and the same depth.

  • count: must be greater than zero. Specifies the number of input matrices when the input vector is a plain C array.

  • dst: output array with the same size and the same depth as input array.

Example

In this example, we will take a “flower-black-background.jpg” image as an input to remove the background.

import cv2

src = cv2.imread('Images/flower-black-background.jpg', 1)
tmp = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
_,alpha = cv2.threshold(tmp,0,255,cv2.THRESH_BINARY)

b, g, r = cv2.split(src)
rgba = [b,g,r, alpha]
dst = cv2.merge(rgba,4)

cv2.imwrite("Images/Background Transparent Image1.png", dst)

Input Image

Output Image

In the output, we can see the transparent image “Background Transparent Image1.png” in the images folder. As our input image is in .jpg format so we converted it to the BGRA domain from the BGR domain by adding the alpha channel.

Example

In this example, we will remove the black background and make it transparent of a PNG image with sing the numpy functions.

import cv2
import numpy as np

# Load image 
na = cv2.imread('Images/WhiteDots.png')

# Make a True/False mask of pixels whose BGR values sum to more than zero
alpha = np.sum(na, axis=-1) > 0

# Convert True/False to 0/255 and change type to "uint8" to match "na"
alpha = np.uint8(alpha * 255)

# Stack new alpha layer with existing image to go from BGR to BGRA, i.e. 3 channels to 4 channels
result = np.dstack((na, alpha))

# Save result
cv2.imwrite('result_image.png', result)

Input Image

Output Image

We have successfully converted black pixels to transparent and saved it as a .png file.

Updated on: 30-May-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements