最近要做一个Project,是使用TV来对添加了Gaussian和Salt&Pepper噪声的图像进行恢复,前期的任务是生成噪声污染的图像。
噪声图像的生成采用对图像进行高斯平滑,之后在随机的对图像添加椒盐噪声。
里面用到的几个opencv的方法:
- random.random_integers:产生范围内的随机整数
- cv2.GaussianBlur:对图像进行高斯滤波
源代码:
import cv2
from numpy import *
def SaltAndPepper(src,percetage):
NoiseImg=src
NoiseNum=int(percetage*src.shape[0]*src.shape[1])
for i in range(NoiseNum):
randX=random.random_integers(0,src.shape[0]-1)
randY=random.random_integers(0,src.shape[1]-1)
if random.random_integers(0,1)==0:
NoiseImg[randX,randY]=0
else:
NoiseImg[randX,randY]=255
return NoiseImg
if __name__=='__main__':
img=cv2.imread('Lena.jpg',flags=0)
gimg=cv2.GaussianBlur(img,(7,7),sigmaX=0)
NoiseImg=SaltAndPepper(gimg,0.4)
#cv2.imshow('img',gimg)
#figure()
Pers=[0.4,0.5,0.6]
for i in Pers:

本文介绍了如何使用OpenCV库在Python中对图像进行高斯平滑处理,并添加椒盐噪声。项目背景是通过TV恢复被噪声污染的图像,内容包括使用`random.random_integers`生成随机整数、`cv2.GaussianBlur`进行高斯滤波等步骤。文章展示了添加不同比例椒盐噪声后的图像效果。
最低0.47元/天 解锁文章
2万+

被折叠的 条评论
为什么被折叠?



