如何在matlab中为一幅图像加入随机噪声

如何在matlab中为一幅图像加入随机噪声,第1张

M=imread('dl011.jpg') %读取MATLAB中的名为cameraman的图像

subplot(3,3,1)

imshow(M) %显示原始图像

title('original')

P1=imnoise(M,'gaussian',0.02) %加入高斯躁声

subplot(3,3,2)

imshow(P1)%加入高斯躁声后显示图像

title('gaussian noise')

P2=imnoise(M,'salt &pepper',0.02) %加入椒盐躁声

subplot(3,3,3)

imshow(P2)%%加入椒盐躁声后显示图像

title('salt &pepper noise')

g=medfilt2(P1) %对高斯躁声中值滤波

subplot(3,3,5)

imshow(g)

title('medfilter gaussian')

h=medfilt2(P2) %对椒盐躁声中值滤波

subplot(3,3,6)

imshow(h)

title('medfilter salt &pepper noise')

l=[1 1 1 %对高斯躁声算术均值滤波

1 1 1

1 1 1]

l=l/9

k=conv2(P1,l)

subplot(3,3,8)

imshow(k,[])

title('arithmeticfilter gaussian')

%对椒盐躁声算术均值滤波

d=conv2(P2,l)

subplot(3,3,9)

imshow(d,[])

title('arithmeticfilter salt &pepper noise')

给图像添加噪声的⽅法(补充ing)1.随机修改⼀部分像素点的灰度值为指定值

def noise(img,proportion=0.05):

'''

随机的修改⼀定数量像素点的灰度值

:param img:

:param proportion: 噪声点占全部像素点的⽐例

:return:

第 1 页

'''

height,width =img.shape[:2]

num = int(height*width*proportion)#多少个像素点添加噪声

for k in range(0, num):

# get the random point

xi = int(np.random.uniform(0, img.shape[1]))

xj = int(np.random.uniform(0, img.shape[0]))

Matlab中为图片加噪声的语句是:

(1)J = imnoise(I,type)

(2)J = imnoise(I,type,parameters)

其中I为原图象的灰度矩阵,J为加噪声后图象的灰度矩阵

一般情况下用(1)中表示即可,(2)中表示是允许修改参数,而(1)中使用缺省参数

至于type可有五种,分别为'gaussian'(高斯白噪声),'localvar'(与图象灰度值有关的零均值高斯白噪声),'poisson'(泊松噪声),'salt &pepper'(椒盐噪声)和'speckle'(斑点噪声)具体(2)中参数值的设定可根据个人需要其余情况以及若还有不懂请参考Matlab帮助文件。

在此使用'salt &pepper'(椒盐噪声),并将其参数设置为0.6。其例子如下:

L = imread(‘image_ori.jpg’)

J = imnoise(L, ‘salt &pepper’, 0.6)

imshow(J)//立即d出窗口,显示加了噪声后的图片

imwrite(J, ‘image_noise.jpg’, ‘jpg’, ‘Quality’, 100)//按100%的质量存储加了噪声的图片,Quality的默认值为75.

以上程序就表示把原图像加入椒盐噪声,但注意要把图像和以上程序的M文件放在同一个子目录下。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11816635.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存