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

之前写过一篇博客,关于透明背景转换为特定颜色,当时使用了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类似,在数组(包括矩阵)的运算当中,布尔索引的方式具有更高的执行效率。

相关推荐
张彦峰ZYF4 分钟前
快速掌握Python编程基础
python
Json____17 分钟前
使用python的 FastApi框架开发图书管理系统-前后端分离项目分享
开发语言·python·fastapi·图书管理系统·图书·项目练习
安思派Anspire22 分钟前
LangGraph + MCP + Ollama:构建强大代理 AI 的关键(二)
人工智能·后端·python
站大爷IP27 分钟前
Python文件与目录比较全攻略:从基础操作到性能优化
python
ahead~2 小时前
【大模型入门】访问GPT_API实战案例
人工智能·python·gpt·大语言模型llm
大模型真好玩2 小时前
准确率飙升!GraphRAG如何利用知识图谱提升RAG答案质量(额外篇)——大规模文本数据下GraphRAG实战
人工智能·python·mcp
19892 小时前
【零基础学AI】第30讲:生成对抗网络(GAN)实战 - 手写数字生成
人工智能·python·深度学习·神经网络·机器学习·生成对抗网络·近邻算法
applebomb2 小时前
没合适的组合wheel包,就自行编译flash_attn吧
python·ubuntu·attention·flash
Chasing__Dreams3 小时前
python--杂识--18.1--pandas数据插入sqlite并进行查询
python·sqlite·pandas
彭泽布衣4 小时前
python2.7/lib-dynload/_ssl.so: undefined symbol: sk_pop_free
python·sk_pop_free