Skip to content
The Chat Archive
gpt-3.5-turbo-0301· OpenAI WildChat-4.8M (AI2)

python实现柏林噪声

  1. User

    python实现柏林噪声

  2. Assistant

    以下是使用Python实现高斯噪声的代码:

    import numpy as np
    
    def generate_noise(width, height, sigma):
        # 创建高斯噪声矩阵
        noise = np.random.normal(0, sigma, (width, height))
        # 限制噪声范围在0-255以内
        noise = np.clip(noise, 0, 255)
        # 将数据转换为整数类型
        noise = noise.astype(np.uint8)
        return noise
    

    这里我们使用NumPy中的np.random.normal函数。它接受三个参数——均值、标准差和输出数据形状。我们将均值设置为0,标准差设置为所需的噪声水平。

    我们还使用了np.clip函数以将数据限制在0-255范围内,并将结果转换为无符号整数类型。

    对于白噪声或均匀噪声,只需将np.random.normal函数替换为其他分布,例如np.random.rand或np.random.uniform。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.