- 一、高斯噪声
- 二、C++代码
- 三、python代码
- 四、结果展示
- 1、原始图像
- 2、添加高斯噪声
高斯分布,也称正态分布,又称常态分布,记为 N ( μ , σ 2 ) N(μ,σ^2) N(μ,σ2) ,其中 μ , σ 2 μ,σ^2 μ,σ2为分布的参数,分别为高斯分布的期望和方差。当有确定值时, p ( x ) p(x) p(x)也就确定了,特别当 μ = 0 μ=0 μ=0, σ 2 = 1 σ^2=1 σ2=1时, X X X的分布为标准正态分布。所谓高斯噪声是指它的概率密度函数服从高斯分布(即正态分布)的一类噪声。区别于椒盐噪声随机出现在图像的任意位置,高斯噪声出现在图像的所有位置。
二、C++代码#include三、python代码#include using namespace cv; using namespace std; int main() { Mat img = imread("qq.jpg"); if (img.empty()) { cout << "请确认图像文件名称是否正确" << endl; return -1; } //生成与原图像同尺寸、数据类型和通道数的矩阵 Mat img_noise = Mat::zeros(img.rows, img.cols, img.type()); imshow("lena原图", img); RNG rng; //创建一个RNG类 rng.fill(img_noise, RNG::NORMAL, 10, 20); //生成三通道的高斯分布随机数(10,20)表示均值和标准差 imshow("三通道高斯噪声", img_noise); img = img + img_noise; //在彩色图像中添加高斯噪声 imwrite("gauss_noise.png", img); imshow("img添加噪声", img); //显示添加高斯噪声后的图像 waitKey(0); return 0; }
import numpy as np
import cv2
def gasuss_noise(image, mu=0.0, sigma=0.1):
"""
添加高斯噪声
:param image: 输入的图像
:param mu: 均值
:param sigma: 标准差
:return: 含有高斯噪声的图像
"""
image = np.array(image / 255, dtype=float)
noise = np.random.normal(mu, sigma, image.shape)
gauss_noise = image + noise
if gauss_noise.min() < 0:
low_clip = -1.
else:
low_clip = 0.
gauss_noise = np.clip(gauss_noise, low_clip, 1.0)
gauss_noise = np.uint8(gauss_noise * 255)
return gauss_noise
if __name__ == '__main__':
# ----------------------读取图片-----------------------------
img = cv2.imread("qq.jpg")
# --------------------添加高斯噪声---------------------------
out2 = gasuss_noise(img, mu=0.0, sigma=0.1)
# ----------------------显示结果-----------------------------
cv2.imshow('origion_pic', img)
cv2.imshow('gauss_noise', out2)
cv2.waitKey(0)
四、结果展示
1、原始图像
2、添加高斯噪声



