matlab中用imnoise向图像中加入特定高斯半径的噪声

本文介绍了在matlab中使用imnoise函数为图像添加特定高斯噪声的过程。通过设置均值和方差(转化为0到1之间的值),可以控制噪声的强度。示例代码展示了如何将高斯噪声应用到图像上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近需要做图像仿真,感觉用C++编写速度有点慢,也是为了省事,就用了matlab做前期的处理,后期到发布版本时,再用C++。

由于是初接触到图像仿真,有些问题没有很好的深入。这里就针对对某个图像中,加入特定高斯半径的噪声,来进行说明。如果不打算长期做图像仿真,只是略有涉及的,可以看看下面的代码。

这里用到的是imnoise函数,用到4个参数,第一个图像,第二个是类型,类型有:

‘gaussian’:Gaussian white noise with constant mean and variance

‘localvar’:Zero-mean Gaussian white noise with an intensity-dependentvariance

‘poisson’:Poisson noise

‘salt & pepper’:On and off pixels

‘speckle’:Multiplicative noise’gaussian’

我们这里加的是高斯噪声,第三个是均值,一般为0就可以。第四个是方差,这个是重点。方差,就是某个点的高斯半径,如10,则是10*10-0*0,注意的是,这里要转化到0到1,则是除以255的平方,即变成了,(10*10 - 0*0)/(255*255)。这里进行的是归一化的工作。

下面是代码。

>>clear
>>image = imread('lena.jpg');
>>figure;
>>imageNoise = imnoise(image,'gaussian',
function R = imnoise2(type, varargin) %IMNOISE2 Generates an array of random numbers with specified PDF. % R = IMNOISE2(TYPE, M, N, A, B) generates an array, R, of size % M-by-N, whose elements are random numbers of the specified TYPE % with parameters A and B. If only TYPE is included in the % input argument list, a single random number of the specified % TYPE and default parameters shown below is generated. If only % TYPE, M, and N are provided, the default parameters shown below % are used. If M = N = 1, IMNOISE2 generates a single random % number of the specified TYPE and parameters A and B. % % Valid values for TYPE and parameters A and B are: % % 'uniform' Uniform random numbers in the interval (A, B). % The default values are (0, 1). % 'gaussian' Gaussian random numbers with mean A and standard % deviation B. The default values are A = 0, B = 1. % 'salt & pepper' Salt and pepper numbers of amplitude 0 with % probability Pa = A, and amplitude 1 with % probability Pb = B. The default values are Pa = % Pb = A = B = 0.05. Note that the noise has % values 0 (with probability Pa = A) and 1 (with % probability Pb = B), so scaling is necessary if % values other than 0 and 1 are required. The noise % matrix R is assigned three values. If R(x, y) = % 0, the noise at (x, y) is pepper (black). If % R(x, y) = 1, the noise at (x, y) is salt % (white). If R(x, y) = 0.5, there is no noise % assigned to coordinates (x, y). % 'lognormal' Lognormal numbers with offset A and shape % parameter B. The defaults are A = 1 and B = % 0.25. % 'rayleigh' Rayleigh noise with parameters A and B. The % default values are A = 0 and B = 1. % 'exponent
### MATLAB 中 `imnoise` 函数添加高斯噪声MATLAB 中,可以通过调用 `imnoise` 函数并指定 `'gaussian'` 类型来向图像添加高斯噪声。该函数允许设置均值 (`m`) 和方差 (`v`) 参数,默认情况下,均值为 0,方差为 0.01[^1]。 以下是具体实现方法: #### 示例代码 ```matlab % 加载测试图像 I = imread('cameraman.tif'); % 显示原始图像 figure; subplot(1,2,1); imshow(I); title('Original Image'); % 向图像添加高斯噪声 (均值=0, 方差=0.01) J_default = imnoise(I, 'gaussian'); % 使用默认参数 m=0, v=0.01 % 自定义高斯噪声参数 (均值=-0.1, 方差=0.05) J_custom = imnoise(I, 'gaussian', -0.1, 0.05); % 显示带噪图像 subplot(1,2,2); imshow(J_custom); title('Image with Custom Gaussian Noise'); ``` 上述代码展示了两种情况下的高斯噪声应用方式: 1. 默认参数下添加高斯噪声; 2. 用户自定义均值和方差的情况下添加高斯噪声。 通过调整 `m` 和 `v` 的取值范围,可以控制噪声强度及其统计特性。 --- #### 局部方差的高斯噪声 如果希望根据图像的不同区域动态调整噪声强度,则可使用 `'localvar'` 类型。此模式会基于输入矩阵中的局部方差值决定每一点上的噪声水平[^2]。 示例代码如下: ```matlab % 创建与原图大小一致的局部方差矩阵 V V = 0.01 * ones(size(I)); % 添加具有局部方差特性的高斯噪声 K_localvar = imnoise(I, 'localvar', V); % 显示结果 figure; imshow(K_localvar); title('Image with Local Variance Gaussian Noise'); ``` 在此例子中,`V` 是一个与图像尺寸相同且所有元素均为常数的矩阵,表示全局统一的方差设定。实际应用场景中可以根据需求设计更复杂的 `V` 来反映空间变化的噪声特征。 --- #### 注意事项 - 当处理灰度图像时,直接按前述方式进行操作即可。 - 对于彩色 RGB 图像,需注意某些版本可能仅支持单通道运算;必要时应先分解成独立颜色分量再逐一分步加噪后再重组。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值