OpenCV——彩色图像添加高斯噪声

一、高斯噪声

  高斯分布,也称正态分布,又称常态分布,记为 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 <opencv2\opencv.hpp>
#include <iostream>

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;
}

三、python代码

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、添加高斯噪声

在这里插入图片描述

在这里插入图片描述

  • 14
    点赞
  • 81
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
要给图像加高斯噪声和去噪,我们可以使用Python中的OpenCV库。首先,我们需要导入必要的库和图像。 ```python import cv2 import numpy as np # 加载图像 image = cv2.imread('image.jpg') ``` 接下来,我们可以通过使用`cv2.randn()`函数来添加高斯噪声。该函数可以生成一个具有指定均值和标准差的随机高斯数组,与原始图像尺寸相同。 ```python # 生成高斯噪声 mean = 0 std_deviation = 50 gaussian_noise = np.zeros(image.shape, np.uint8) cv2.randn(gaussian_noise, mean, std_deviation) ``` 可以调整`mean`和`std_deviation`的值来控制噪声的强度。生成的高斯噪声数组与原始图像相加。 ```python # 添加高斯噪声到图像 noisy_image = cv2.add(image, gaussian_noise) ``` 现在图像上添加高斯噪声。如果要将其去噪,可以使用OpenCV库中的`cv2.fastNlMeansDenoising()`函数。 ```python # 去噪 denoised_image = cv2.fastNlMeansDenoisingColored(noisy_image, None, 10, 10, 7, 21) ``` 在这里,我们使用了`cv2.fastNlMeansDenoisingColored()`函数来对带有高斯噪声彩色图像进行去噪。该函数采用一些参数,如去噪强度、空间窗口大小和色彩距离权重等。您可以根据需要调整这些参数。 最后,可以使用`cv2.imshow()`函数显示添加高斯噪声和去噪后的图像。 ```python # 显示结果 cv2.imshow('Original Image', image) cv2.imshow('Noisy Image', noisy_image) cv2.imshow('Denoised Image', denoised_image) cv2.waitKey(0) cv2.destroyAllWindows() ``` 通过运行以上代码,您将能够在显示窗口中看到原始图像、添加高斯噪声的图像和经过去噪处理的图像。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

点云侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值