图像处理:图像噪声添加

文章目录


前言

本文主要介绍几种添加图像噪声的方法,用于数据增强等操作。

以下图为例。

一、高斯噪声

高斯噪声就是给图片添加一个服从高斯分布的噪声,可以通过调节高斯分布标准差(sigma)的大小来控制添加噪声程度,sigma越大添加的噪声越多图片损坏的越厉害。(高斯噪声更像是早期荧幕上的各种小点)

python 复制代码
def gaussian_noise(image):
    h, w, c = image.shape
    mean = 0
    sigma = 25  # 标准差
    noise = np.random.normal(mean, sigma, (h, w, c)) #根据均值和标准差生成符合高斯分布的噪声
    noisy_image = np.clip(image + noise, 0, 255).astype(np.uint8)
    return noisy_image

二、椒盐噪声

椒盐噪声就是给图片添加黑白噪点,椒指的是黑色的噪点(0,0,0)盐指的是白色的噪点(255,255,255),通过设置amount来控制添加噪声的比例,值越大添加的噪声越多,图像损坏的更加严重

python 复制代码
def jiaoyan_noise(image):
    #设置添加椒盐噪声的数目比例
    s_vs_p = 0.5
    #设置添加噪声图像像素的数目
    amount = 0.04
    noisy_img = np.copy(image)
    #添加salt噪声
    num_salt = np.ceil(amount * image.size * s_vs_p)
    #设置添加噪声的坐标位置
    coords = [np.random.randint(0,i - 1, int(num_salt)) for i in image.shape]
    noisy_img[coords[0],coords[1],:] = [255,255,255]
    #添加pepper噪声
    num_pepper = np.ceil(amount * image.size * (1. - s_vs_p))
    #设置添加噪声的坐标位置
    coords = [np.random.randint(0,i - 1, int(num_pepper)) for i in image.shape]
    noisy_img[coords[0],coords[1],:] = [0,0,0]
    #保存图片
    return noisy_img

三、泊松噪声

图像中的泊松噪声是由于在成像过程中光子的随机性引起的。当光子以不规则的速率到达传感器时,就会在图像中引入泊松噪声。泊松噪声在图像中表现为亮度值的随机变化,尤其在低亮度区域更为显著。这种噪声使得图像中的细节模糊,并且可能干扰图像处理算法和分析。

python 复制代码
def generate_poisson_noise(img, scale=1.0, gray_noise=False):

    if gray_noise:
        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # round and clip image for counting vals correctly
    img = np.clip((img * 255.0).round(), 0, 255) / 255.
    vals = len(np.unique(img))
    vals = 2**np.ceil(np.log2(vals))
    out = np.float32(np.random.poisson(img * vals) / float(vals))
    noise = out - img
    if gray_noise:
        noise = np.repeat(noise[:, :, np.newaxis], 3, axis=2)
    return noise * scale


def random_generate_poisson_noise(img, scale_range=(0, 1.0), gray_prob=0):
    scale = np.random.uniform(scale_range[0], scale_range[1])
    if np.random.uniform() < gray_prob:
        gray_noise = True
    else:
        gray_noise = False
    return generate_poisson_noise(img, scale, gray_noise)


def random_add_poisson_noise(img, scale_range=(0, 1.0), gray_prob=0):
    noise = random_generate_poisson_noise(img, scale_range, gray_prob)
    out = img + noise
    return out

四、斑点噪声

斑点噪声是数字图像中常见的一种噪声类型,通常表现为图像中出现随机像素点。这种噪声可能是由于图像传感器故障、信号传输错误或图像存储过程中的错误引起的。

python 复制代码
def bandian_noise(image):
    h,w,c = image.shape
    gauss = np.random.randn(h,w,c)
    #给图片添加speckle噪声
    noisy_img = image + image * gauss
    #归一化图像的像素值
    noisy_img = np.clip(noisy_img,a_min=0,a_max=255)
    return noisy_img

五、指数噪声

在图像处理中,指数噪声可能是由于光照条件不稳定、传感器故障或信号传输错误等原因引起的

python 复制代码
def zhishu_noise(img):
    a = 10.0
    noiseExponent = np.random.exponential(scale=a, size=img.shape)
    imgExponentNoise = img + noiseExponent
    noisy_img = np.uint8(cv2.normalize(imgExponentNoise, None, 0, 255, cv2.NORM_MINMAX))  # 归一化为 [0,255]
    return noisy_img

六、均匀噪声

类似于指数噪声,只不过采样方式不同

python 复制代码
def uniform_noise(img):
    mean, sigma = 10, 100
    a = 2 * mean - np.sqrt(12 * sigma)  # a = -14.64
    b = 2 * mean + np.sqrt(12 * sigma)  # b = 54.64
    noiseUniform = np.random.uniform(a, b, img.shape)
    imgUniformNoise = img + noiseUniform
    noisy_img = np.uint8(cv2.normalize(imgUniformNoise, None, 0, 255, cv2.NORM_MINMAX))  # 归一化为 [0,255]

    return noisy_img

总结

目前主流的噪声添加就这么几种,最常用的是高斯和泊松,在数据增强中还是有很大帮助的。

相关推荐
(❁´◡`❁)Jimmy(❁´◡`❁)1 天前
4815. 【NOIP2016提高A组五校联考4】ksum
算法
Elastic 中国社区官方博客1 天前
使用 Elasticsearch 管理 agentic 记忆
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
升职佳兴1 天前
从 0 到 1:我做了一个提升 AI 对话效率的浏览器插件(架构+实现+发布)
人工智能·架构
linmoo19861 天前
Langchain4j 系列之二十二 - Embedding Models
人工智能·langchain·embedding·嵌入模型·langchain4j
三不原则1 天前
实战:基于 GitOps 实现 AI 应用的自动化部署与发布
运维·人工智能·自动化
沈浩(种子思维作者)1 天前
什么才叫量子物理学?什么是真正量子计算?
人工智能·python·flask·量子计算
张彦峰ZYF1 天前
AI 编码工具全景分析与选型决策指南——从「代码补全」到「工程级智能体」的范式跃迁
人工智能·ai 编码工具·选型决策·代码补全·工程级智能体·ai 尚不等同于工程自治
无限码力1 天前
科大讯飞秋招笔试真题 - 字符拼接 & 字典序最小的字符串拼接 & 圆心覆盖
算法·秋招·科大讯飞·科大讯飞笔试真题
Lips6111 天前
第四章 决策树
算法·决策树·机器学习
Coder_Boy_1 天前
基于SpringAI的在线考试系统-DDD(领域驱动设计)核心概念及落地架构全总结(含事件驱动协同逻辑)
java·人工智能·spring boot·微服务·架构·事件驱动·领域驱动