层层堆叠RGBA图片层就可实时修改图片与视频流

用AI自动生成RGBA格式的PNG图片;一层层堆叠。

这也是Adobe Photoshop的Mask原理?

https://learnopencv.com/alpha-blending-using-opencv-cpp-python/

we will learn how to alpha blend two images and overlay a transparent PNG image over another image in OpenCV.

We are sharing code in both C++ and Python.

What is Alpha Blending?

Alpha blending is the process of overlaying a foreground image with transparency over a background image. Transparency is often the fourth channel of an image ( e.g. in a transparent PNG),

but it can also be a separate image. This transparency mask is often called the alpha mask or the alpha matte.

BGRA与BGR

  • BGRColor(颜色)数值化R(红色)G(绿色)B(蓝色) 三Channel(分量)每分量数值取值范围0-255

    通过组合这三个Color Channel(颜色分量)的不同数值,可以得到各种各样的颜色。

  • BGRA 的Color(颜色)数有R(红色)G(绿色)B(蓝色)A(透明度)四种分量.

    注意:PNG格式图片,才有BGRA

    BGR颜色模型的一种扩展只增加一个表示透明度(Alpha)透明分量(A)

    A代表Alpha通道,该通道决定像素的透明度,取值范围也是0-255。0: 透明度100%(rgb的色彩), 255:透明度0。

    因此 通过调整Alpha通道分量取值,可实现图像的半透明效果

图片尺寸

“图片长1920、宽1080”:略写的 单位pixels(像素点总数).

数字图片的“长宽”并非物理意义的长度单位,而是在图片“横”和“竖”这两个维度上包含的像素个数

比如,1920×1080的图片是由横向1920个像素、纵向1080个像素(合计2,073,600个像素)构成的。

图片分辨率(Image Resolution):

Pixel Density(像素密度):指 单位面积上的 像素数量单位dpi(dots per inch, 像素点/英寸)。

手机屏幕像素密度PPI

很大程度由 分辨率屏幕尺寸 决定,

引入概念: PPI(屏幕像素密度), 即屏幕斜对角线单位英寸像素点数

\(\large PPI = \frac{\sqrt{ HorizontalPixels_{pixel}^{2} + VerticalPixels_{pixel}^{2}} }{LengthOfDiagonalLine_{inch}}\)

PPI值越大,屏幕越清晰。

总结:

  • 屏幕尺寸: 屏幕对角线长度

    单位:英寸,1英寸等于2.54厘米

  • 分辨率: 横纵向方向像素的大小

    纵向像素横向像素,如1920px1080px

    单位:像素(px)

  • 像素密度: 指每英寸屏幕所拥有的像素的数量

    单位:dpi

  • 三者关系

  • 像素密度=Sqrt(横向像素数横向像素数+纵向像素数纵向像素数)/屏幕尺寸(对角线的长度inch)

  • 图像用的内存空间大小:分辨率 * 位深 / 8

    例如:一幅图像分辨率:1024768, 24位(所有通道位深的加和. 即38bit),则其用到的内存大小如下:

    大小 = 1024 * 768 * 24 / 8 = 2359296 byte = 2304 KB

  • 分辨率:宽 * 高

  • 位深度:指定图像的每个像素可以使用的颜色信息数量。如8bit 或 10bit

    每个像素使用的信息位数越多,可用的颜色就越多,颜色表现就更逼真。

实例 为RGB图片增加Alpha Mask(透明遮罩)

#! /usr/bin/env python

import os, sys
import itertools as it
import collections as ct
import cv2 as cv
import numpy as np
import pandas as pd img_path = "./Signature.jpg" # 计算每个 Color Channel 的 Most Common 色彩数值
def calculate_top_color(image):
a = image.copy()
cnt = ct.Counter(a)
topTen = cnt.most_common(10)
topTenColors = [k for k, v in topTen]
topColor = np.array(topTenColors).mean()
return topColor def replace_color(image, top=None, threshold=5, color=255):
color_threshold = threshold
a = image.copy()
s = pd.Series(image.flatten())
rows, cols = image.shape
topColor = top or calculate_top_color(s)
# topColor = top or int(s.describe()['50%'])
colorMin = topColor - color_threshold
colorMax = topColor + color_threshold
# print(s.describe(), "\n", "TopColor: %s, %s\n" % (topColor, topColor))
for x in range(rows):
for y in range(cols):
val = a.item(x, y)
if colorMin <= val < colorMax:
a.itemset((x, y), color)
return a # 如果rgb三Color Channel的色彩数值全都是“目标色彩数值”
# 则将Alpha Channel分量的色彩数值设置为 0:“100%透明,将显示背景色彩”
# 不然Alpha Channel分量的色彩数值设置为 255:“0%透明,将显示此像素色彩”
def compositeAlphaMask(r, g, b, a, color, alpha0, alpha1):
rows, cols = b.shape
for x in range(rows):
for y in range(cols):
vb, vg, vr = b.item(x, y), g.item(x, y), r.item(x, y)
if vb == color and vg == color and vr == color:
a.itemset((x, y), alpha1)
else:
a.itemset((x, y), alpha0)
return a # 将BGR图片统一转换为BGRA的格式,拆分出每个Color Channel处理完再合成BGRA的产出PNG图片
def remove_top_color(img, top=None, threshold=18, color=255, alpha=0):
if img.shape[-1] == 3:
img = cv.cvtColor(img, cv.COLOR_BGR2BGRA) b, g, r, a = img[:, :, 0], img[:, :, 1], img[:, :, 2], img[:, :, 3] print("\nProcessing Color Channel: BLUE")
b = replace_color(b, top, threshold, color) print("\nProcessing Color Channel: GREEN")
g = replace_color(g, top, threshold, color) print("\nProcessing Color Channel: RED")
r = replace_color(r, top, threshold, color) (alpha0, alpha1) = (0, 255) if alpha == 0 else (255, 0)
compositeAlphaMask(r, g, b, a, color, alpha0, alpha1) img_bgr = cv.merge((b, g, r))
img_bgra = cv.merge((b, g, r, a))
return img_bgr, img_bgra if __name__ == "__main__":
threshold = int(sys.argv[1]) if len(sys.argv) > 1 else 18
# Load the image first
img0 = cv.imread(img_path)
assert img0 is not None, ("file could not be read, check with os.path.exists('%s')" % img_path) # import pdb;pdb.set_trace()
cv.imwrite("Original.png", img0)
img_bgr, img_bgra = remove_top_color(img0, threshold=threshold, alpha=1)
cv.imwrite("OutputBGRA.png", img_bgra)

SciTech-BigDataAIML-CV+CG-Digital Image Processing-编辑与合成RGBA图片与视频:RGB图片转换成RGBA:增加Alpha Mask(透明遮罩, 即Alpha Channel透明度通道)的更多相关文章

  1. Digital Image Processing 学习笔记3

    第三章 灰度变换与空间滤波 3.1 背景知识 3.1.1 灰度变换和空间滤波基础 本章节所讨论的图像处理技术都是在空间域进行的.可以表示为下式: $$g(x, y) = T[f(x,y)]$$ 其中$ ...

  2. Digital image processing(数字图像处理)

    In computer science, digital image processing is the use of computer algorithms to perform image pro ...

  3. 信号处理的好书Digital Signal Processing - A Practical Guide for Engineers and Scientists

    诚心给大家推荐一本讲信号处理的好书<Digital Signal Processing - A Practical Guide for Engineers and Scientists>[ ...

  4. div,span,p等转换成可编辑

    当前它能够将任意不可编辑的标签(span.div.p...等)转换成可编辑的text input.password.textarea.下拉列表(drop-down list)等标签.你可以利用它的ed ...

  5. 如何使用ABBYY FineReader 12将JPEG文件转换成可编辑文本

    日常工作中,经常会收到一些JPEG格式的图像文件,有时候还需要做一些修改,可是大家都知道JPEG格式的文件是无法修改的,必须转换成可编辑的格式,当然,现在市场上也应用而生了很多转换工具,相信大家都听说 ...

  6. Sublime下MarkDown插件实现编辑和实时预览并转换成HTML格式

    最近在使用markdown做笔记,编辑器Sublime Text3用起来很轻巧,现在让他支持markdown的语法并且可以实时预览. 安装准备——安装Package Control Package C ...

  7. Emacs中编辑保存makefile文件时会错误地将TAB转成空格的解决方法

    问题描述 我的Emacs使用了Purcell的配置,在其配置中使用了whitespace-cleanup,且通过在.emacs.d/lisp/init-edit-utils.el中设定: (requi ...

  8. Digital Imaging Processing 数字图像处理

    8-Bit and 16-Bit Images 关于量化压缩与量化补偿 RGB Bayer Color分析 彩色CCD/CMOS的格式和计算机中的读取格式

  9. Digital Image Processing 学习笔记2

    第二章 2.1视觉感知要素 2.1.1 人眼的结构 眼睛由角膜与巩膜外壳.脉络膜和视网膜包围,晶状体由通信的纤维细胞层组成,并由附在睫状体上的纤维悬挂:视网膜上分布两类光感受器(锥状体和杆状体),他们 ...

  10. How do I convert an IIR filter into a FIR filter in digital signal processing?

    Maybe you were asking if there is some kind of design tool allowing to convert an IIR filter into an ...

随机推荐

  1. SpringCloud——自定义断言工厂

    目录 场景:用户的请求头中需要有指定的用户名和密码才能访问. 断言工厂 参考系统AfterRoutePredicateFactory写法. package com.zjw.factory; impor ...

  2. Mybatis 框架课程第四天

    目录 1 Mybatis 延迟加载策略 1.1 何为延迟加载 1.2 实现需求 1.3 使用 assocation 实现延迟加载 1.3.1 账户的持久层 DAO 接口 1.3.2 账户的持久层映射文 ...

  3. 高性能深度学习推理引擎 -- OpenPPL

    OpenPPL OpenPPL是商汤基于自研高性能算字库的开源深度学习推理平台,能够让人工智能应用高效可靠地运行在现有的CPU/GPU等计算平台上,为云端场景提供人工智能推理服务 OpenPPL基于全 ...

  4. ctf知识积累

    (1)url解码: python解码函数: from urllib.parse import unquote(quote:编码) url_code="" url_code1=unq ...

  5. CF contest 1909 Pinely Round 3 (Div. 1 + Div. 2) 题解(Vanilla的掉分赛)

    CF contest 1909 Pinely Round 3 (Div. 1 + Div. 2) Vanilla的掉分赛 绪言 Pinely Round 3 (Div. 1 + Div. 2) - C ...

  6. HNU FPGA课设项目上手指南

    1.介绍 本文章旨在帮助HNU的同学更优雅的完成数电的FPGA课设(使用DE2-115),文章将涉及完成FPGA项目需要掌握的知识,资源分享以及一些关于完成项目的经验指导.大家快快搬好小板凳,准备发车 ...

  7. Vue 学习笔记 [Part 7]

    作者:故事我忘了¢个人微信公众号:程序猿的月光宝盒 目录 一. Promise 1.0 什么是Promise 1.1. Promise的基本使用 1.2. Promise的链式调用 1.3. Prom ...

  8. ChatMoney,分析梦境的大师

    本文由 ChatMoney团队出品 作为一个爱幻想爱做白日梦的 i人,我常常就在想,什么时候能利用Al来帮助我找回一些被遗忘的.或者模糊不清的记忆? 有没有可能进入别人的梦境里瞧一瞧? 为什么世界上还 ...

  9. Extend BOL Model BT with custom table type relationship

    Link to Content's target Space : http://wiki.sdn.sap.com/wiki/display/CRM/CRM+Web+Client+UI+Framewor ...

  10. 8月18日直播预告 | Flink SQL转换Operator流程及源码解析

    ​​8月18日晚19点,袋鼠云数栈技术研发团队开发工程师--修竹,将会为大家直播分享<Flink SQL转换Operator流程及源码解析>. 课程内容主要包括以下两点: 1. Flink ...