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

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

相关推荐
FreakStudio41 分钟前
全网最适合入门的面向对象编程教程:50 Python函数方法与接口-接口和抽象基类
python·嵌入式·面向对象·电子diy
redcocal2 小时前
地平线秋招
python·嵌入式硬件·算法·fpga开发·求职招聘
artificiali2 小时前
Anaconda配置pytorch的基本操作
人工智能·pytorch·python
RaidenQ2 小时前
2024.9.13 Python与图像处理新国大EE5731课程大作业,索贝尔算子计算边缘,高斯核模糊边缘,Haar小波计算边缘
图像处理·python·算法·课程设计
花生了什么树~.3 小时前
python基础知识(六)--字典遍历、公共运算符、公共方法、函数、变量分类、参数分类、拆包、引用
开发语言·python
Trouvaille ~3 小时前
【Python篇】深度探索NumPy(下篇):从科学计算到机器学习的高效实战技巧
图像处理·python·机器学习·numpy·信号处理·时间序列分析·科学计算
爆更小小刘3 小时前
Python基础语法(3)下
开发语言·python
哪 吒3 小时前
华为OD机试 - 第 K 个字母在原来字符串的索引(Python/JS/C/C++ 2024 E卷 100分)
javascript·python·华为od
憨憨小白4 小时前
Python 的集合类型
开发语言·python·青少年编程·少儿编程
白杆杆红伞伞4 小时前
01_快速入门
python·pandas