当前位置:网站首页>往图片添加椒盐噪声或高斯噪声
往图片添加椒盐噪声或高斯噪声
2022-07-06 23:59:00 【瀛台夜雪】
往图片添加椒盐噪声或高斯噪声
椒盐噪声添加
椒盐噪声(salt & pepper noise)是数字图像的一个常见噪声,所谓椒盐,椒就是黑,盐就是白,椒盐噪声就是在图像上随机出现黑色白色的像素。椒盐噪声是一种因为信号脉冲强度引起的噪声,产生该噪声的算法也比较简单。
往灰度图添加椒盐噪声
#往灰度图像添加椒盐噪声
# src 输入图像的路径
# dst 输出图像的路径
# prob 噪声的占比
def addSaltPepperNoise_gray(src,dst,probility=0.05,method="salt_pepper"):
#将图片转换成灰度图
imarray=np.array(Image.open(src).convert('L'))
height,width=imarray.shape
for i in range(height):
for j in range(width):
#随机添加椒盐噪声
if np.random.random(1)<probility:
if np.random.random(1)<0.05:
imarray[i,j]=0
else:
imarray[i,j]=255
#将生成的椒盐噪声图像添加到
new_im=Image.fromarray(imarray)
new_im.save(dst)
return new_im
往彩色图添加椒盐噪声
def RGBAddNoise(src, dst, prob): # 同时加杂乱(RGB单噪声)RGB图噪声 prob:噪声占比
imarray = np.array(Image.open(src))
height, width, channels = imarray.shape
# prob = 0.05 #噪声占比 已经比较明显了 >0.1 严重影响画质
NoiseImg = imarray.copy()
NoiseNum = int(prob * height * width)
for i in range(NoiseNum):
rows = np.random.randint(0, height - 1)
cols = np.random.randint(0, width - 1)
channel = np.random.randint(0, 3)
if np.random.randint(0, 2) == 0:
NoiseImg[rows, cols, channel] = 0
else:
NoiseImg[rows, cols, channel] = 255
# return NoiseImg
new_im = Image.fromarray(NoiseImg)
new_im.save(dst)
return new_im
高斯噪声添加
高斯噪声是指噪声分布的概率密度函数服从高斯分布(正态分布)的一类噪声,其产生的主要原因是由于相机在拍摄时视场较暗且亮度不均匀造成的,同时相机长时间工作使得温度过高也会引起高斯噪声,另外电路元器件自身噪声和互相影响也是造成高斯噪声的重要原因之一。
往灰度图添加高斯噪声
def addGaussNoiseGray(src,dst,mean,sigma):
image=cv2.imread(src,cv2.IMREAD_GRAYSCALE)
height,width=image.shape
gauss=np.random.normal(mean,sigma,image.shape)
gauss=gauss.reshape(height,width)
noiseImage=image+gauss
noiseImage=np.clip(noiseImage,0,255)
noiseImage=noiseImage.astype('uint8')
cv2.imwrite(dst,noiseImage)
return noiseImage
往彩色图添加高斯噪声
#彩色图像中添加高斯噪声
def addGaussNoiseRgb(src,dst,mean,sigma):
image=cv2.imread(src)
height,width,channels=image.shape
#建立高斯噪声
gauss=np.random.normal(mean,sigma,(height,width,channels))
gauss=gauss.reshape(height,width,channels)
noiseImage=image+gauss
noiseImage=np.clip(noiseImage,0,255)
noiseImage=noiseImage.astype('uint8')
cv2.imwrite(dst,noiseImage)
return noiseImage
结果展示
边栏推荐
- JVM the truth you need to know
- Message queue: how to deal with message backlog?
- C nullable type
- 随机生成session_id
- Flinksql 读写pgsql
- Jhok-zbl1 leakage relay
- Taobao Commodity details page API interface, Taobao Commodity List API interface, Taobao Commodity sales API interface, Taobao app details API interface, Taobao details API interface
- Design, configuration and points for attention of network unicast (one server, multiple clients) simulation using OPNET
- Hcip seventh operation
- Egr-20uscm ground fault relay
猜你喜欢
Jhok-zbg2 leakage relay
Message queuing: how to ensure that messages are not lost
判断文件是否为DICOM文件
什么是依赖注入(DI)
Lombok plug-in
论文阅读【Sensor-Augmented Egocentric-Video Captioning with Dynamic Modal Attention】
Paper reading [MM21 pre training for video understanding challenge:video captioning with pre training techniqu]
《2022中国低/无代码市场研究及选型评估报告》发布
Realize GDB remote debugging function between different network segments
Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
随机推荐
力扣102题:二叉树的层序遍历
C#可空类型
Senior programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization, and recommends collecting
[JS component] date display.
基于NCF的多模块协同实例
软件测试面试技巧
win配置pm2开机自启node项目
4. Object mapping Mapster
架构设计的五个核心要素
论文阅读【Sensor-Augmented Egocentric-Video Captioning with Dynamic Modal Attention】
[论文阅读] A Multi-branch Hybrid Transformer Network for Corneal Endothelial Cell Segmentation
Modes of optical fiber - single mode and multimode
Message queue: how to handle repeated messages?
Hcip seventh operation
Flinksql 读写pgsql
毕业之后才知道的——知网查重原理以及降重举例
上海字节面试问题及薪资福利
Design, configuration and points for attention of network arbitrary source multicast (ASM) simulation using OPNET
WEB架构设计过程
三级菜单数据实现,实现嵌套三级菜单数据