层层堆叠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. 天翼云出席DCIC2025,“翼立方”创新力拉满!

    近日,由中国通信企业协会主办的DCIC2025(第14届)数据中心产业发展大会在北京召开.大会以"共筑算力基石,护航产业生态"为主题,邀请众多知名算力企业代表以及生态合作伙伴代表, ...

  2. 典型相关分析 CCA

    最近有小伙伴在问我一个数据分析的问题, 做毕设, 实证分析. 不知道改如何处理数据. 看了下设计的量表大致是这样的, 都是 5级的里克特量表, 大致分为两波, X, Y. 小伙伴认为就只有两个变量, ...

  3. 从零到一:打造高效的金仓社区 API 集成到 MCP 服务方案

    今天在使用国产数据库金仓时,我发现每次遇到问题都习惯性地打开金仓社区进行搜索和查看相关信息.可是每次打开浏览器的操作总让我觉得有些麻烦,于是我决定不再依赖这种繁琐的过程.索性今天我把这个接口提取出来, ...

  4. Python内置数据结构操作VS sqlite3操作

    1.摘要 在同一数据库中查询某值,内置数据结构的查询速度快还是数据库sqlite3的查询速度快?针对这一问题,通过构建一个包含2500个随机数的列表,并将其插入sqlite3数据库中,利用timeit ...

  5. C# 利用反射模拟多态效果

    public class A { } public class B : A { } public class C : A { } public static class Extension { pub ...

  6. Seata源码—7.Seata TCC模式的事务处理

    大纲 1.Seata TCC分布式事务案例配置 2.Seata TCC案例服务提供者启动分析 3.@TwoPhaseBusinessAction注解扫描源码 4.Seata TCC案例分布式事务入口分 ...

  7. Django Web应用开发实战第十一章

    一.会话控制 Django内置的会话控制简称为session,可以为用户提供基础的数据存储. 数据主要存储在服务器上,并且网站上的任意站点都能使用会话数据. 当用户第一次访问网站时,网站的服务器将自动 ...

  8. 实现qt 窗口无边框拖拽

    无边框拖拽是参考Qt实战6.万能的无边框窗口(FramelessWindow) - Qt小罗 - 博客园的文章,对其代码进行修改而来. 使用的是qt6 所以有可能里面一些关于坐标的类需要修改一下类型 ...

  9. 洛谷P4551 最长异或路径 trie

    题目描述 给定一棵\(n\)个点的带权树,结点下标从\(1\)开始到\(N\).寻找树中找两个结点,求最长的异或路径. 异或路径指的是指两个结点之间唯一路径上的所有边权的异或. 输入格式 第一行一个整 ...

  10. k8s | 重启Kubernetes Pod的几种方式

    前言 在使用 docker 的过程中,我们可以使用docker restart {container_id}来重启容器,但是在 kubernetes 中并没有重启命令(没有 kubectl resta ...