Alpha blending

来源:互联网 发布:郑州启凡网络 编辑:程序博客网 时间:2024/05/23 15:37

What is "Alpha" ? "Alpha" 指的是「不透明度」= "Opacity"。
Alpha 值越大,則越不透明。本文將以 a 代替 Alpha。
What is "Alpha Blending" ? Alpha Blending 是指當有兩個 Pixel 在同一個位置時,且上方的 Pixel 為「透明的」(也就是其 alpha < 1),如此一來,後方的 Pixel 就應該會“透出“來。 事實上 Alpha Blending 也就是我們常說的 Compositing。


# Copyright 2017 by  Sunita Nayak <nayak.sunita@gmail.com>



import cv2


# Read the foreground image with alpha channel
foreGroundImage = cv2.imread("foreGroundAsset.png", -1)


# Split png foreground image
b,g,r,a = cv2.split(foreGroundImage)


# Save the foregroung RGB content into a single object
foreground = cv2.merge((b,g,r))


# Save the alpha information into a single Mat
alpha = cv2.merge((a,a,a))


# Read background image
background = cv2.imread("backGround.jpg")


# Convert uint8 to float
foreground = foreground.astype(float)
background = background.astype(float)
alpha = alpha.astype(float)/255


# Perform alpha blending
foreground = cv2.multiply(alpha, foreground)
background = cv2.multiply(1.0 - alpha, background)
outImage = cv2.add(foreground, background)


cv2.imwrite("outImgPy.png", outImage)


cv2.imshow("outImg", outImage/255)
cv2.waitKey(0)
原创粉丝点击