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

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

相关推荐
dbdr090118 分钟前
Linux 入门到精通,真的不用背命令!零基础小白靠「场景化学习法」,3 个月拿下运维 offer,第二十六天
linux·运维·服务器·网络·python·学习
花花无缺23 分钟前
python自动化-pytest-用例发现规则和要求
后端·python
YUELEI1181 小时前
langchain 提示模版 PromptTemplate
python·langchain
东方不败之鸭梨的测试笔记1 小时前
LangChain: Models, Prompts 模型和提示词
人工智能·python·langchain
AI Echoes2 小时前
别再手工缝合API了!开源LLMOps神器LMForge,让你像搭积木一样玩转AI智能体!
人工智能·python·langchain·开源·agent
AI Echoes2 小时前
从零构建企业级LLMOps平台:LMForge——支持多模型、可视化编排、知识库与安全审核的全栈解决方案
人工智能·python·langchain·开源·agent
beijingliushao2 小时前
58-正则表达式
数据库·python·mysql·正则表达式
陈敬雷-充电了么-CEO兼CTO2 小时前
具身智能多模态感知与场景理解:融合语言模型的多模态大模型
人工智能·python·gpt·语言模型·自然语言处理·chatgpt·多模态
荔枝吻2 小时前
【AI总结】Python BERT 向量化入门指南
人工智能·python·bert
张子夜 iiii3 小时前
传统神经网络实现-----手写数字识别(MNIST)项目
人工智能·pytorch·python·深度学习·算法