Harshit Gupta
Harshit Gupta

Reputation: 37

how to add an alpha channel of particular value in an BGR image

I tried the below code, it doesn't show any error and runs properly, but changing the value of the alpha channel, doesn't show any change in image

img3 = cv2.cvtColor(img2, cv2.COLOR_BGR2BGRA)
img3[:,:,3] = 100
cv2.imshow('img1',img2)    
cv2.imshow('img',img3)     
cv2.waitKey(0)

works ok, but the output of both images are same and there is no seen-able change after applying alpha channel

i have already tried the below code

Upvotes: 1

Views: 2867

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207863

Your code is actually correct.

The simple answer is that OpenCV's imshow() ignores transparency, so if you want to see its effect, save your image as a PNG/TIFF (both of which support transparency) and view it with a different viewer - such as GIMP, Photoshop or feh.

As an alternative, I made a wrapper/decorator for OpenCV's imshow() that displays images with transparency overlaid on a chessboard like Photoshop does. So, starting with this RGBA Paddington image and this grey+alpha Paddington image:

enter image description here

enter image description here

#!/usr/bin/env python3

import cv2
import numpy as np

def imshow(title,im):
    """Decorator for OpenCV "imshow()" to handle images with transparency"""

    # Check we got np.uint8, 2-channel (grey + alpha) or 4-channel RGBA image
    if (im.dtype == np.uint8) and (len(im.shape)==3) and (im.shape[2] in set([2,4])):

       # Pick up the alpha channel and delete from original
       alpha = im[...,-1]/255.0
       im = np.delete(im, -1, -1)

       # Promote greyscale image to RGB to make coding simpler
       if len(im.shape) == 2:
          im = np.stack((im,im,im))

       h, w, _ = im.shape

       # Make a checkerboard background image same size, dark squares are grey(102), light squares are grey(152)
       f = lambda i, j: 102 + 50*((i+j)%2)
       bg = np.fromfunction(np.vectorize(f), (16,16)).astype(np.uint8)

       # Resize to square same length as longer side (so squares stay square), then trim
       if h>w:
          longer = h
       else:
          longer = w
       bg = cv2.resize(bg, (longer,longer), interpolation=cv2.INTER_NEAREST)
       # Trim to correct size
       bg = bg[:h,:w]

       # Blend, using result = alpha*overlay + (1-alpha)*background
       im = (alpha[...,None] * im + (1.0-alpha[...,None])*bg[...,None]).astype(np.uint8)

    cv2.imshow(title,im)


if __name__ == "__main__":

    # Open RGBA image
    im = cv2.imread('paddington.png',cv2.IMREAD_UNCHANGED)

    imshow("Paddington (RGBA)",im)
    key = cv2.waitKey(0)
    cv2.destroyAllWindows()

    # Open Grey + alpha  image
    im = cv2.imread('paddington-ga.png',cv2.IMREAD_UNCHANGED)

    imshow("Paddington (grey + alpha)",im)
    key = cv2.waitKey(0)
    cv2.destroyAllWindows()

And you will get this:

enter image description here

and this:

enter image description here

Keywords: Image, image processing, Python, alpha channel, transparency, overlay, checkerboard, chessboard, blend, blending. OpenCV, imshow, cv2.imshow.

Upvotes: 9

Related Questions