两种图像透明背景转特定颜色方法的比较

之前写过一篇博客,关于透明背景转换为特定颜色,当时使用了NumPy数组采用布尔索引转换的方式,这次我们把这种转换和常规的逐像素转换的方式进行比较,看那种方法效率更高。记得以前使用Matlab的时候,显然是矩阵布尔索引的方式会有更高的效率,不知道Python是否也一样。

  1. 代码实现:
python 复制代码
def transparent2bgra_color1(img, bgra_color=(255, 255, 255, 255)):
    """
    将图像中的透明区域替换为指定的BGRA颜色。
    
    参数:
    img: 输入的BGRA格式图像,其中A通道为透明度。
    bgra_color: 替换透明区域的颜色,默认为白色(255, 255, 255, 255)。
    
    返回:
    返回替换透明区域后的图像。
    """
    # 为了避免原图像发生改变,创建一个副本进行操作
    res = img.copy()
    # 将图像中透明度为0的区域替换为指定的BGRA颜色
    res[img[:, :, 3] == 0] = bgra_color
    return res

def transparent2bgra_color2(src, bgra_color=(255, 255, 255, 255)):
    """
    将图片中的透明部分替换为指定的BGRA颜色。
    
    参数:
    src: 输入的图片,应为BGRA格式。
    bgra_color: 替换透明部分的颜色,默认为白色(255, 255, 255, 255)。
    
    返回:
    替换透明部分后的图片。
    """
    # 复制输入图片,避免修改原图
    img=src.copy()
    # 获取图片的宽度和高度
    sp=img.shape  
    width=sp[0]  
    height=sp[1]  
    # 遍历图片的每个像素点
    for yh in range(height):
        for xw in range(width):
            # 获取当前点的颜色数据
            color_d=img[xw,yh]  
            # 检查当前点的透明度,如果完全透明,则替换颜色
            if(color_d[3]==0):  
                img[xw,yh]=bgra_color  
    # 返回处理后的图片
    return img
  1. 效率对比测试

使用Benchmark,我们可以测试这两种方法的性能,代码如下:

python 复制代码
import pytest
import cv2

def transparent2bgra_color1(img, bgra_color=(255, 255, 255, 255)):
    """
    将图像中的透明区域替换为指定的BGRA颜色。
    
    参数:
    img: 输入的BGRA格式图像,其中A通道为透明度。
    bgra_color: 替换透明区域的颜色,默认为白色(255, 255, 255, 255)。
    
    返回:
    返回替换透明区域后的图像。
    """
    # 为了避免原图像发生改变,创建一个副本进行操作
    res = img.copy()
    # 将图像中透明度为0的区域替换为指定的BGRA颜色
    res[img[:, :, 3] == 0] = bgra_color
    return res

def transparent2bgra_color2(src, bgra_color=(255, 255, 255, 255)):
    """
    将图片中的透明部分替换为指定的BGRA颜色。
    
    参数:
    src: 输入的图片,应为BGRA格式。
    bgra_color: 替换透明部分的颜色,默认为白色(255, 255, 255, 255)。
    
    返回:
    替换透明部分后的图片。
    """
    # 复制输入图片,避免修改原图
    img=src.copy()
    # 获取图片的宽度和高度
    sp=img.shape  
    width=sp[0]  
    height=sp[1]  
    # 遍历图片的每个像素点
    for yh in range(height):
        for xw in range(width):
            # 获取当前点的颜色数据
            color_d=img[xw,yh]  
            # 检查当前点的透明度,如果完全透明,则替换颜色
            if(color_d[3]==0):  
                img[xw,yh]=bgra_color  
    # 返回处理后的图片
    return img

img = cv2.imread('dog.png', cv2.IMREAD_UNCHANGED)

@pytest.mark.parametrize("input_data", [img])
def test_trans_color1(benchmark, input_data):
    res = benchmark(transparent2bgra_color1, input_data)
    assert (667, 1000, 4) == res.shape

@pytest.mark.parametrize("input_data", [img])
def test_trans_color2(benchmark, input_data):
    res = benchmark(transparent2bgra_color2, input_data)
    assert (667, 1000, 4) == res.shape    
  1. 结果

显然,Python也与Matlab类似,在数组(包括矩阵)的运算当中,布尔索引的方式具有更高的执行效率。

相关推荐
独行soc7 小时前
2025年渗透测试面试题总结-18(题目+回答)
android·python·科技·面试·职场和发展·渗透测试
S01d13r8 小时前
gunicorn + flask 处理高并发请求
python·flask·gunicorn
杜子不疼.8 小时前
《Python列表和元组:从入门到花式操作指南》
开发语言·python
pan0c238 小时前
数据处理与统计分析 —— numpy入门
python·numpy
max5006008 小时前
基于桥梁三维模型的无人机检测路径规划系统设计与实现
前端·javascript·python·算法·无人机·easyui
秋氘渔9 小时前
综合案例:Python 函数知识整合 — 学生成绩管理系统
开发语言·python
AI 嗯啦10 小时前
SQL详细语法教程(三)mysql的函数知识
android·开发语言·数据库·python·sql·mysql
databook10 小时前
把数学对象画出来:Manim Mobject类库速查手册
python·数学·动效
图灵学术计算机论文辅导11 小时前
傅里叶变换+attention机制,深耕深度学习领域
人工智能·python·深度学习·计算机网络·考研·机器学习·计算机视觉
ruleslol11 小时前
python30-正则表达式
python·正则表达式