Skip to content Skip to sidebar Skip to footer

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.COL

Solution 1:

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 python3import cv2
import numpy as np

defimshow(title,im):
    """Decorator for OpenCV "imshow()" to handle images with transparency"""# Check we got np.uint8, 2-channel (grey + alpha) or 4-channel RGBA imageif (im.dtype == np.uint8) and (len(im.shape)==3) and (im.shape[2] inset([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 simpleriflen(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 trimif 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.

Post a Comment for "How To Add An Alpha Channel Of Particular Value In An Bgr Image"