昊虹AI笔记网

 找回密码
 立即注册
搜索
查看: 231|回复: 0
收起左侧

什么叫图像的高斯噪声?并附添加高斯噪声的OpenCV代码和MATLAB代码

[复制链接]

249

主题

252

帖子

976

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
976
昊虹君 发表于 2024-6-18 14:41 | 显示全部楼层 |阅读模式
图像的高斯噪声(Gaussian noise)是一种常见的图像噪声类型,其特征在于噪声的概率分布服从高斯(正态)分布。高斯噪声通常出现在传感器或电子传输过程中,是由多个小的、独立的扰动叠加而成。其数学表示通常为:

这里,μ是均值,通常取 0(即噪声的平均值为零,不偏离原图像的亮度值),
是方差,表示噪声的强度。

高斯噪声的具体特点和影响包括:
1、均值和方差:噪声值围绕均值对称分布,方差决定了噪声的强度。方差越大,图像受干扰的程度越高。
2、全局影响:高斯噪声影响整个图像,每个像素点都可能受到影响,且噪声的值与像素值无关。
3、可叠加性:由于高斯噪声的独立性,可以简单地通过叠加多次生成的高斯噪声来模拟更复杂的噪声模型。
生成图像高斯噪声的方法通常包括在图像的每个像素值上添加随机采样的高斯噪声值。这样可以模拟出因成像设备或传输过程中的随机误差而导致的噪声。

例如,在灰度图像中,如果一个像素的原始值为I,而噪声值为N,则添加噪声后的像素值为:


其中,N 服从的高斯分布。总的来说,高斯噪声是一种重要的噪声模型,用于研究和处理图像中的随机干扰,许多图像处理和分析方法都针对这种噪声进行了优化和处理。

利用OpenCV实现添加高斯噪声的C++代码:
[C++] 纯文本查看 复制代码
#include <opencv2/opencv.hpp>

#include <iostream>

using namespace cv;
using namespace std;

// Function to add Gaussian noise
void addGaussianNoise(const Mat &src, Mat &dst, double mean, double stddev) {
Mat noise(src.size(), src.type());
randn(noise, mean, stddev); // generate Gaussian noise
dst = src + noise;
}

int main() {
// Load the image
Mat image = imread("path_to_your_image.jpg", IMREAD_GRAYSCALE);
if (image.empty()) {
cout << "Could not open or find the image" << endl;
return -1;
}

// Add Gaussian noise
Mat noisyImage;
addGaussianNoise(image, noisyImage, 0, 25); // mean = 0, stddev = 25

// Display the original and noisy images
imshow("Original Image", image);
imshow("Noisy Image", noisyImage);

// Wait for a key press indefinitely
waitKey(0);

return 0;
}


利用OpenCV实现添加高斯噪声的Pyhton代码:
[Python] 纯文本查看 复制代码
import cv2
import numpy as np

def add_gaussian_noise(image, mean=0, stddev=25):
    noise = np.zeros_like(image, dtype=np.float32)
    cv2.randn(noise, mean, stddev)  # generate Gaussian noise
    noisy_image = cv2.add(image.astype(np.float32), noise)
    return noisy_image.astype(np.uint8)

# Load the image
image = cv2.imread('path_to_your_image.jpg', cv2.IMREAD_GRAYSCALE)
if image is None:
    print("Could not open or find the image")
else:
    # Add Gaussian noise
    noisy_image = add_gaussian_noise(image, mean=0, stddev=25)

    # Display the original and noisy images
    cv2.imshow('Original Image', image)
    cv2.imshow('Noisy Image', noisy_image)

    # Wait for a key press indefinitely
    cv2.waitKey(0)
    cv2.destroyAllWindows()

在上述代码中,addGaussianNoise 和 add_gaussian_noise 函数用于在图像上添加高斯噪声。高斯噪声的均值设为0,标准差设为25(可以根据需要调整)。cv2.randn 函数用于生成高斯噪声,并将其添加到原始图像中。
请将 path_to_your_image.jpg 替换为实际图像文件的路径。


利用MATLAB实现添加高斯噪声的代码:
[Python] 纯文本查看 复制代码
% Read the image
image = imread('path_to_your_image.jpg');
if size(image, 3) == 3
    image = rgb2gray(image); % Convert to grayscale if it's a color image
end

% Define the mean and standard deviation of the Gaussian noise
mean = 0;
stddev = 25;

% Add Gaussian noise
noisyImage = double(image) + stddev * randn(size(image)) + mean;

% Clip the values to be in the valid range [0, 255]
noisyImage = uint8(max(min(noisyImage, 255), 0));

% Display the original and noisy images
figure;
subplot(1, 2, 1);
imshow(image);
title('Original Image');

subplot(1, 2, 2);
imshow(noisyImage);
title('Noisy Image');

% Save the noisy image if needed
% imwrite(noisyImage, 'noisy_image.jpg');


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|昊虹AI笔记网 ( 蜀ICP备2024076726 )

GMT+8, 2024-9-8 13:01 , Processed in 0.019345 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表